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 <linux/of_device.h>
25 #include <asm/ptrace.h>
26 #include <asm/processor.h>
27 #include <asm/system.h>
30 #include <asm/vaddrs.h>
31 #include <asm/timer.h>
32 #include <asm/openprom.h>
33 #include <asm/oplib.h>
34 #include <asm/traps.h>
37 #include <asm/pgalloc.h>
38 #include <asm/pgtable.h>
40 #include <asm/cacheflush.h>
41 #include <asm/irq_regs.h>
45 /* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
46 /* #define DISTRIBUTE_IRQS */
48 struct sun4d_timer_regs {
51 u32 l10_limit_noclear;
56 static struct sun4d_timer_regs __iomem *sun4d_timers;
60 #define MAX_STATIC_ALLOC 4
61 extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
62 extern int static_irq_count;
64 static unsigned char sbus_tid[32];
67 static struct irqaction *irq_action[NR_IRQS];
68 extern spinlock_t irq_action_lock;
70 static struct sbus_action {
71 struct irqaction *action;
72 /* For SMP this needs to be extended */
75 static int pil_to_sbus[] = {
76 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
79 static int sbus_to_pil[] = {
80 0, 2, 3, 5, 7, 9, 11, 13,
85 DEFINE_SPINLOCK(sun4d_imsk_lock);
88 int show_sun4d_interrupts(struct seq_file *p, void *v)
90 int i = *(loff_t *) v, j = 0, k = 0, sbusl;
91 struct irqaction * action;
97 spin_lock_irqsave(&irq_action_lock, flags);
99 sbusl = pil_to_sbus[i];
101 action = *(i + irq_action);
105 for (j = 0; j < nsbi; j++) {
106 for (k = 0; k < 4; k++)
107 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
112 found_it: seq_printf(p, "%3d: ", i);
114 seq_printf(p, "%10u ", kstat_irqs(i));
116 for_each_online_cpu(x)
117 seq_printf(p, "%10u ",
118 kstat_cpu(cpu_logical_map(x)).irqs[i]);
120 seq_printf(p, "%c %s",
121 (action->flags & IRQF_DISABLED) ? '+' : ' ',
123 action = action->next;
125 for (; action; action = action->next) {
126 seq_printf(p, ",%s %s",
127 (action->flags & IRQF_DISABLED) ? " +" : "",
133 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
136 if (j == nsbi) break;
138 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
144 spin_unlock_irqrestore(&irq_action_lock, flags);
148 void sun4d_free_irq(unsigned int irq, void *dev_id)
150 struct irqaction *action, **actionp;
151 struct irqaction *tmp = NULL;
154 spin_lock_irqsave(&irq_action_lock, flags);
156 actionp = irq + irq_action;
158 actionp = &(sbus_actions[irq - (1 << 5)].action);
161 printk("Trying to free free IRQ%d\n",irq);
165 for (; action; action = action->next) {
166 if (action->dev_id == dev_id)
171 printk("Trying to free free shared IRQ%d\n",irq);
174 } else if (action->flags & IRQF_SHARED) {
175 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
178 if (action->flags & SA_STATIC_ALLOC)
180 /* This interrupt is marked as specially allocated
181 * so it is a bad idea to free it.
183 printk("Attempt to free statically allocated IRQ%d (%s)\n",
189 tmp->next = action->next;
191 *actionp = action->next;
193 spin_unlock_irqrestore(&irq_action_lock, flags);
195 synchronize_irq(irq);
197 spin_lock_irqsave(&irq_action_lock, flags);
205 spin_unlock_irqrestore(&irq_action_lock, flags);
208 extern void unexpected_irq(int, void *, struct pt_regs *);
210 void sun4d_handler_irq(int irq, struct pt_regs * regs)
212 struct pt_regs *old_regs;
213 struct irqaction * action;
214 int cpu = smp_processor_id();
215 /* SBUS IRQ level (1 - 7) */
216 int sbusl = pil_to_sbus[irq];
218 /* FIXME: Is this necessary?? */
221 cc_set_iclr(1 << irq);
223 old_regs = set_irq_regs(regs);
225 kstat_cpu(cpu).irqs[irq]++;
227 action = *(irq + irq_action);
229 unexpected_irq(irq, NULL, regs);
231 action->handler(irq, action->dev_id);
232 action = action->next;
235 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
237 struct sbus_action *actionp;
239 int sbil = (sbusl << 2);
241 bw_clear_intr_mask(sbusl, bus_mask);
243 /* Loop for each pending SBI */
244 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
246 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
247 mask &= (0xf << sbil);
248 actionp = sbus_actions + (sbino << 5) + (sbil);
249 /* Loop for each pending SBI slot */
250 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
253 action = actionp->action;
256 unexpected_irq(irq, NULL, regs);
258 action->handler(irq, action->dev_id);
259 action = action->next;
261 release_sbi(SBI2DEVID(sbino), slot);
266 set_irq_regs(old_regs);
269 int sun4d_request_irq(unsigned int irq,
270 irq_handler_t handler,
271 unsigned long irqflags, const char * devname, void *dev_id)
273 struct irqaction *action, *tmp = NULL, **actionp;
277 if(irq > 14 && irq < (1 << 5)) {
287 spin_lock_irqsave(&irq_action_lock, flags);
290 actionp = &(sbus_actions[irq - (1 << 5)].action);
292 actionp = irq + irq_action;
296 if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) {
297 for (tmp = action; tmp->next; tmp = tmp->next);
302 if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) {
303 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
307 action = NULL; /* Or else! */
310 /* If this is flagged as statically allocated then we use our
311 * private struct which is never freed.
313 if (irqflags & SA_STATIC_ALLOC) {
314 if (static_irq_count < MAX_STATIC_ALLOC)
315 action = &static_irqaction[static_irq_count++];
317 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
321 action = kmalloc(sizeof(struct irqaction),
329 action->handler = handler;
330 action->flags = irqflags;
331 cpus_clear(action->mask);
332 action->name = devname;
334 action->dev_id = dev_id;
345 spin_unlock_irqrestore(&irq_action_lock, flags);
350 static void sun4d_disable_irq(unsigned int irq)
353 int tid = sbus_tid[(irq >> 5) - 1];
357 if (irq < NR_IRQS) return;
359 spin_lock_irqsave(&sun4d_imsk_lock, flags);
360 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
361 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
363 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
367 static void sun4d_enable_irq(unsigned int irq)
370 int tid = sbus_tid[(irq >> 5) - 1];
374 if (irq < NR_IRQS) return;
376 spin_lock_irqsave(&sun4d_imsk_lock, flags);
377 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
378 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
380 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
385 static void sun4d_set_cpu_int(int cpu, int level)
387 sun4d_send_ipi(cpu, level);
390 static void sun4d_clear_ipi(int cpu, int level)
394 static void sun4d_set_udt(int cpu)
398 /* Setup IRQ distribution scheme. */
399 void __init sun4d_distribute_irqs(void)
401 struct device_node *dp;
403 #ifdef DISTRIBUTE_IRQS
404 cpumask_t sbus_serving_map;
406 sbus_serving_map = cpu_present_map;
407 for_each_node_by_name(dp, "sbi") {
408 int board = of_getintprop_default(dp, "board#", 0);
410 if ((board * 2) == boot_cpu_id && cpu_isset(board * 2 + 1, cpu_present_map))
411 sbus_tid[board] = (board * 2 + 1);
412 else if (cpu_isset(board * 2, cpu_present_map))
413 sbus_tid[board] = (board * 2);
414 else if (cpu_isset(board * 2 + 1, cpu_present_map))
415 sbus_tid[board] = (board * 2 + 1);
417 sbus_tid[board] = 0xff;
418 if (sbus_tid[board] != 0xff)
419 cpu_clear(sbus_tid[board], sbus_serving_map);
421 for_each_node_by_name(dp, "sbi") {
422 int board = of_getintprop_default(dp, "board#", 0);
423 if (sbus_tid[board] == 0xff) {
426 if (cpus_empty(sbus_serving_map))
427 sbus_serving_map = cpu_present_map;
428 while (cpu_isset(i, sbus_serving_map))
431 cpu_clear(i, sbus_serving_map);
434 for_each_node_by_name(dp, "sbi") {
435 int devid = of_getintprop_default(dp, "device-id", 0);
436 int board = of_getintprop_default(dp, "board#", 0);
437 printk("sbus%d IRQs directed to CPU%d\n", board, sbus_tid[board]);
438 set_sbi_tid(devid, sbus_tid[board] << 3);
441 int cpuid = cpu_logical_map(1);
444 cpuid = cpu_logical_map(0);
445 for_each_node_by_name(dp, "sbi") {
446 int devid = of_getintprop_default(dp, "device-id", 0);
447 int board = of_getintprop_default(dp, "board#", 0);
448 sbus_tid[board] = cpuid;
449 set_sbi_tid(devid, cpuid << 3);
451 printk("All sbus IRQs directed to CPU%d\n", cpuid);
456 static void sun4d_clear_clock_irq(void)
458 sbus_readl(&sun4d_timers->l10_timer_limit);
461 static void sun4d_clear_profile_irq(int cpu)
463 bw_get_prof_limit(cpu);
466 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
468 bw_set_prof_limit(cpu, limit);
471 static void __init sun4d_load_profile_irqs(void)
475 while (!cpu_find_by_instance(cpu, NULL, &mid)) {
476 sun4d_load_profile_irq(mid >> 3, 0);
481 static void __init sun4d_fixup_trap_table(void)
485 extern unsigned long lvl14_save[4];
486 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
487 extern unsigned int real_irq_entry[], smp4d_ticker[];
488 extern unsigned int patchme_maybe_smp_msg[];
490 /* Adjust so that we jump directly to smp4d_ticker */
491 lvl14_save[2] += smp4d_ticker - real_irq_entry;
493 /* For SMP we use the level 14 ticker, however the bootup code
494 * has copied the firmware's level 14 vector into the boot cpu's
495 * trap table, we must fix this now or we get squashed.
497 local_irq_save(flags);
498 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
499 trap_table->inst_one = lvl14_save[0];
500 trap_table->inst_two = lvl14_save[1];
501 trap_table->inst_three = lvl14_save[2];
502 trap_table->inst_four = lvl14_save[3];
503 local_flush_cache_all();
504 local_irq_restore(flags);
508 static void __init sun4d_init_timers(irq_handler_t counter_fn)
510 struct device_node *dp;
515 dp = of_find_node_by_name(NULL, "cpu-unit");
517 prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
521 /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
522 * registers via any cpu's mapping. The first 'reg' property is the
525 reg = of_get_property(dp, "reg", NULL);
527 prom_printf("sun4d_init_timers: No reg property\n");
532 res.end = reg[2] - 1;
533 res.flags = reg[0] & 0xff;
534 sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
535 sizeof(struct sun4d_timer_regs), "user timer");
537 prom_printf("sun4d_init_timers: Can't map timer regs\n");
541 sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit);
543 master_l10_counter = &sun4d_timers->l10_cur_count;
544 master_l10_limit = &sun4d_timers->l10_timer_limit;
546 err = request_irq(TIMER_IRQ, counter_fn,
547 (IRQF_DISABLED | SA_STATIC_ALLOC),
550 prom_printf("sun4d_init_timers: request_irq() failed with %d\n", err);
553 sun4d_load_profile_irqs();
554 sun4d_fixup_trap_table();
557 void __init sun4d_init_sbi_irq(void)
559 struct device_node *dp;
562 for_each_node_by_name(dp, "sbi")
564 sbus_actions = kzalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
566 prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
569 for_each_node_by_name(dp, "sbi") {
570 int devid = of_getintprop_default(dp, "device-id", 0);
571 int board = of_getintprop_default(dp, "board#", 0);
576 extern unsigned char boot_cpu_id;
578 set_sbi_tid(devid, boot_cpu_id << 3);
579 sbus_tid[board] = boot_cpu_id;
582 /* Get rid of pending irqs from PROM */
583 mask = acquire_sbi(devid, 0xffffffff);
585 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, board);
586 release_sbi(devid, mask);
591 void __init sun4d_init_IRQ(void)
595 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
596 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
597 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
598 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
599 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
600 sparc_init_timers = sun4d_init_timers;
602 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
603 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
604 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
606 /* Cannot enable interrupts until OBP ticker is disabled. */