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.
9 #include <linux/errno.h>
10 #include <linux/linkage.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/ptrace.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/smp.h>
20 #include <linux/spinlock.h>
21 #include <linux/seq_file.h>
23 #include <asm/ptrace.h>
24 #include <asm/processor.h>
25 #include <asm/system.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>
35 #include <asm/pgalloc.h>
36 #include <asm/pgtable.h>
39 #include <asm/cacheflush.h>
40 #include <asm/irq_regs.h>
44 /* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
45 /* #define DISTRIBUTE_IRQS */
47 struct sun4d_timer_regs *sun4d_timers;
50 #define MAX_STATIC_ALLOC 4
51 extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
52 extern int static_irq_count;
53 unsigned char cpu_leds[32];
55 static unsigned char sbus_tid[32];
58 static struct irqaction *irq_action[NR_IRQS];
59 extern spinlock_t irq_action_lock;
61 static struct sbus_action {
62 struct irqaction *action;
63 /* For SMP this needs to be extended */
66 static int pil_to_sbus[] = {
67 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
70 static int sbus_to_pil[] = {
71 0, 2, 3, 5, 7, 9, 11, 13,
76 DEFINE_SPINLOCK(sun4d_imsk_lock);
79 int show_sun4d_interrupts(struct seq_file *p, void *v)
81 int i = *(loff_t *) v, j = 0, k = 0, sbusl;
82 struct irqaction * action;
88 spin_lock_irqsave(&irq_action_lock, flags);
90 sbusl = pil_to_sbus[i];
92 action = *(i + irq_action);
96 for (j = 0; j < nsbi; j++) {
97 for (k = 0; k < 4; k++)
98 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
103 found_it: seq_printf(p, "%3d: ", i);
105 seq_printf(p, "%10u ", kstat_irqs(i));
107 for_each_online_cpu(x)
108 seq_printf(p, "%10u ",
109 kstat_cpu(cpu_logical_map(x)).irqs[i]);
111 seq_printf(p, "%c %s",
112 (action->flags & IRQF_DISABLED) ? '+' : ' ',
114 action = action->next;
116 for (; action; action = action->next) {
117 seq_printf(p, ",%s %s",
118 (action->flags & IRQF_DISABLED) ? " +" : "",
124 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
127 if (j == nsbi) break;
129 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
135 spin_unlock_irqrestore(&irq_action_lock, flags);
139 void sun4d_free_irq(unsigned int irq, void *dev_id)
141 struct irqaction *action, **actionp;
142 struct irqaction *tmp = NULL;
145 spin_lock_irqsave(&irq_action_lock, flags);
147 actionp = irq + irq_action;
149 actionp = &(sbus_actions[irq - (1 << 5)].action);
152 printk("Trying to free free IRQ%d\n",irq);
156 for (; action; action = action->next) {
157 if (action->dev_id == dev_id)
162 printk("Trying to free free shared IRQ%d\n",irq);
165 } else if (action->flags & IRQF_SHARED) {
166 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
169 if (action->flags & SA_STATIC_ALLOC)
171 /* This interrupt is marked as specially allocated
172 * so it is a bad idea to free it.
174 printk("Attempt to free statically allocated IRQ%d (%s)\n",
180 tmp->next = action->next;
182 *actionp = action->next;
184 spin_unlock_irqrestore(&irq_action_lock, flags);
186 synchronize_irq(irq);
188 spin_lock_irqsave(&irq_action_lock, flags);
196 spin_unlock_irqrestore(&irq_action_lock, flags);
199 extern void unexpected_irq(int, void *, struct pt_regs *);
201 void sun4d_handler_irq(int irq, struct pt_regs * regs)
203 struct pt_regs *old_regs;
204 struct irqaction * action;
205 int cpu = smp_processor_id();
206 /* SBUS IRQ level (1 - 7) */
207 int sbusl = pil_to_sbus[irq];
209 /* FIXME: Is this necessary?? */
212 cc_set_iclr(1 << irq);
214 old_regs = set_irq_regs(regs);
216 kstat_cpu(cpu).irqs[irq]++;
218 action = *(irq + irq_action);
220 unexpected_irq(irq, NULL, regs);
222 action->handler(irq, action->dev_id);
223 action = action->next;
226 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
228 struct sbus_action *actionp;
230 int sbil = (sbusl << 2);
232 bw_clear_intr_mask(sbusl, bus_mask);
234 /* Loop for each pending SBI */
235 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
237 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
238 mask &= (0xf << sbil);
239 actionp = sbus_actions + (sbino << 5) + (sbil);
240 /* Loop for each pending SBI slot */
241 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
244 action = actionp->action;
247 unexpected_irq(irq, NULL, regs);
249 action->handler(irq, action->dev_id);
250 action = action->next;
252 release_sbi(SBI2DEVID(sbino), slot);
257 set_irq_regs(old_regs);
260 int sun4d_request_irq(unsigned int irq,
261 irq_handler_t handler,
262 unsigned long irqflags, const char * devname, void *dev_id)
264 struct irqaction *action, *tmp = NULL, **actionp;
268 if(irq > 14 && irq < (1 << 5)) {
278 spin_lock_irqsave(&irq_action_lock, flags);
281 actionp = &(sbus_actions[irq - (1 << 5)].action);
283 actionp = irq + irq_action;
287 if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) {
288 for (tmp = action; tmp->next; tmp = tmp->next);
293 if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) {
294 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
298 action = NULL; /* Or else! */
301 /* If this is flagged as statically allocated then we use our
302 * private struct which is never freed.
304 if (irqflags & SA_STATIC_ALLOC) {
305 if (static_irq_count < MAX_STATIC_ALLOC)
306 action = &static_irqaction[static_irq_count++];
308 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
312 action = kmalloc(sizeof(struct irqaction),
320 action->handler = handler;
321 action->flags = irqflags;
322 cpus_clear(action->mask);
323 action->name = devname;
325 action->dev_id = dev_id;
336 spin_unlock_irqrestore(&irq_action_lock, flags);
341 static void sun4d_disable_irq(unsigned int irq)
344 int tid = sbus_tid[(irq >> 5) - 1];
348 if (irq < NR_IRQS) return;
350 spin_lock_irqsave(&sun4d_imsk_lock, flags);
351 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
352 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
354 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
358 static void sun4d_enable_irq(unsigned int irq)
361 int tid = sbus_tid[(irq >> 5) - 1];
365 if (irq < NR_IRQS) return;
367 spin_lock_irqsave(&sun4d_imsk_lock, flags);
368 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
369 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
371 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
376 static void sun4d_set_cpu_int(int cpu, int level)
378 sun4d_send_ipi(cpu, level);
381 static void sun4d_clear_ipi(int cpu, int level)
385 static void sun4d_set_udt(int cpu)
389 /* Setup IRQ distribution scheme. */
390 void __init sun4d_distribute_irqs(void)
392 struct device_node *dp;
394 #ifdef DISTRIBUTE_IRQS
395 cpumask_t sbus_serving_map;
397 sbus_serving_map = cpu_present_map;
398 for_each_node_by_name(dp, "sbi") {
399 int board = of_getintprop_default(dp, "board#", 0);
401 if ((board * 2) == boot_cpu_id && cpu_isset(board * 2 + 1, cpu_present_map))
402 sbus_tid[board] = (board * 2 + 1);
403 else if (cpu_isset(board * 2, cpu_present_map))
404 sbus_tid[board] = (board * 2);
405 else if (cpu_isset(board * 2 + 1, cpu_present_map))
406 sbus_tid[board] = (board * 2 + 1);
408 sbus_tid[board] = 0xff;
409 if (sbus_tid[board] != 0xff)
410 cpu_clear(sbus_tid[board], sbus_serving_map);
412 for_each_node_by_name(dp, "sbi") {
413 int board = of_getintprop_default(dp, "board#", 0);
414 if (sbus_tid[board] == 0xff) {
417 if (cpus_empty(sbus_serving_map))
418 sbus_serving_map = cpu_present_map;
419 while (cpu_isset(i, sbus_serving_map))
422 cpu_clear(i, sbus_serving_map);
425 for_each_node_by_name(dp, "sbi") {
426 int devid = of_getintprop_default(dp, "device-id", 0);
427 int board = of_getintprop_default(dp, "board#", 0);
428 printk("sbus%d IRQs directed to CPU%d\n", board, sbus_tid[board]);
429 set_sbi_tid(devid, sbus_tid[board] << 3);
432 int cpuid = cpu_logical_map(1);
435 cpuid = cpu_logical_map(0);
436 for_each_node_by_name(dp, "sbi") {
437 int devid = of_getintprop_default(dp, "device-id", 0);
438 int board = of_getintprop_default(dp, "board#", 0);
439 sbus_tid[board] = cpuid;
440 set_sbi_tid(devid, cpuid << 3);
442 printk("All sbus IRQs directed to CPU%d\n", cpuid);
447 static void sun4d_clear_clock_irq(void)
449 volatile unsigned int clear_intr;
450 clear_intr = sun4d_timers->l10_timer_limit;
453 static void sun4d_clear_profile_irq(int cpu)
455 bw_get_prof_limit(cpu);
458 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
460 bw_set_prof_limit(cpu, limit);
463 static void __init sun4d_init_timers(irq_handler_t counter_fn)
470 /* Map the User Timer registers. */
471 memset(&r, 0, sizeof(r));
473 r.start = CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT;
475 r.start = CSR_BASE(0)+BW_TIMER_LIMIT;
478 sun4d_timers = (struct sun4d_timer_regs *) sbus_ioremap(&r, 0,
479 PAGE_SIZE, "user timer");
481 sun4d_timers->l10_timer_limit = (((1000000/HZ) + 1) << 10);
482 master_l10_counter = &sun4d_timers->l10_cur_count;
483 master_l10_limit = &sun4d_timers->l10_timer_limit;
485 irq = request_irq(TIMER_IRQ,
487 (IRQF_DISABLED | SA_STATIC_ALLOC),
490 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
494 /* Enable user timer free run for CPU 0 in BW */
495 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
498 while (!cpu_find_by_instance(cpu, NULL, &mid)) {
499 sun4d_load_profile_irq(mid >> 3, 0);
506 extern unsigned long lvl14_save[4];
507 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
508 extern unsigned int real_irq_entry[], smp4d_ticker[];
509 extern unsigned int patchme_maybe_smp_msg[];
511 /* Adjust so that we jump directly to smp4d_ticker */
512 lvl14_save[2] += smp4d_ticker - real_irq_entry;
514 /* For SMP we use the level 14 ticker, however the bootup code
515 * has copied the firmware's level 14 vector into the boot cpu's
516 * trap table, we must fix this now or we get squashed.
518 local_irq_save(flags);
519 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
520 trap_table->inst_one = lvl14_save[0];
521 trap_table->inst_two = lvl14_save[1];
522 trap_table->inst_three = lvl14_save[2];
523 trap_table->inst_four = lvl14_save[3];
524 local_flush_cache_all();
525 local_irq_restore(flags);
530 void __init sun4d_init_sbi_irq(void)
532 struct device_node *dp;
535 for_each_node_by_name(dp, "sbi")
537 sbus_actions = kzalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
539 prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
542 for_each_node_by_name(dp, "sbi") {
543 int devid = of_getintprop_default(dp, "device-id", 0);
544 int board = of_getintprop_default(dp, "board#", 0);
549 extern unsigned char boot_cpu_id;
551 set_sbi_tid(devid, boot_cpu_id << 3);
552 sbus_tid[board] = boot_cpu_id;
555 /* Get rid of pending irqs from PROM */
556 mask = acquire_sbi(devid, 0xffffffff);
558 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, board);
559 release_sbi(devid, mask);
564 void __init sun4d_init_IRQ(void)
568 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
569 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
570 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
571 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
572 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
573 sparc_init_timers = sun4d_init_timers;
575 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
576 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
577 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
579 /* Cannot enable interrupts until OBP ticker is disabled. */