1 /* $Id: sun4d_irq.c,v 1.28 2001/07/17 16:17:33 anton 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.
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/slab.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>
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>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.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;
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];
52 unsigned char sbus_tid[32];
55 extern struct irqaction *irq_action[];
58 struct irqaction *action;
59 /* For SMP this needs to be extended */
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,
72 spinlock_t sun4d_imsk_lock = SPIN_LOCK_UNLOCKED;
75 int sun4d_get_irq_list(char *buf)
77 int i, j = 0, k = 0, len = 0, sbusl;
78 struct irqaction * action;
83 for (i = 0 ; i < NR_IRQS ; i++) {
84 sbusl = pil_to_sbus[i];
86 action = *(i + irq_action);
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))
97 found_it: len += sprintf(buf+len, "%3d: ", i);
99 len += sprintf(buf+len, "%10u ", kstat_irqs(i));
101 for (x = 0; x < smp_num_cpus; x++)
102 len += sprintf(buf+len, "%10u ",
103 kstat.irqs[cpu_logical_map(x)][i]);
105 len += sprintf(buf+len, "%c %s",
106 (action->flags & SA_INTERRUPT) ? '+' : ' ',
108 action = action->next;
110 for (; action; action = action->next) {
111 len += sprintf(buf+len, ",%s %s",
112 (action->flags & SA_INTERRUPT) ? " +" : "",
118 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
121 if (j == nsbi) break;
123 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
126 len += sprintf(buf+len, "\n");
131 void sun4d_free_irq(unsigned int irq, void *dev_id)
133 struct irqaction *action, **actionp;
134 struct irqaction *tmp = NULL;
138 actionp = irq + irq_action;
140 actionp = &(sbus_actions[irq - (1 << 5)].action);
143 printk("Trying to free free IRQ%d\n",irq);
147 for (; action; action = action->next) {
148 if (action->dev_id == dev_id)
153 printk("Trying to free free shared IRQ%d\n",irq);
156 } else if (action->flags & SA_SHIRQ) {
157 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
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",
172 tmp->next = action->next;
174 *actionp = action->next;
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?? */
196 cc_set_iclr(1 << irq);
199 kstat.irqs[cpu][irq]++;
201 action = *(irq + irq_action);
203 unexpected_irq(irq, 0, regs);
205 action->handler(irq, action->dev_id, regs);
206 action = action->next;
209 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
211 struct sbus_action *actionp;
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)
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++)
227 action = actionp->action;
230 unexpected_irq(irq, 0, regs);
232 action->handler(irq, action->dev_id, regs);
233 action = action->next;
235 release_sbi(SBI2DEVID(sbino), slot);
240 if (softirq_pending(cpu))
244 unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq)
246 int sbusl = pil_to_sbus[irq];
249 return ((sdev->bus->board + 1) << 5) + (sbusl << 2) + sdev->slot;
254 unsigned int sun4d_sbint_to_irq(struct sbus_dev *sdev, unsigned int sbint)
256 if (sbint >= sizeof(sbus_to_pil)) {
257 printk(KERN_ERR "%s: bogus SBINT %d\n", sdev->prom_name, sbint);
260 return sun4d_build_irq(sdev, sbus_to_pil[sbint]);
263 int sun4d_request_irq(unsigned int irq,
264 void (*handler)(int, void *, struct pt_regs *),
265 unsigned long irqflags, const char * devname, void *dev_id)
267 struct irqaction *action, *tmp = NULL, **actionp;
270 if(irq > 14 && irq < (1 << 5))
277 actionp = &(sbus_actions[irq - (1 << 5)].action);
279 actionp = irq + irq_action;
283 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
284 for (tmp = action; tmp->next; tmp = tmp->next);
288 if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
289 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
292 action = NULL; /* Or else! */
297 /* If this is flagged as statically allocated then we use our
298 * private struct which is never freed.
300 if (irqflags & SA_STATIC_ALLOC) {
301 if (static_irq_count < MAX_STATIC_ALLOC)
302 action = &static_irqaction[static_irq_count++];
304 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",irq, devname);
308 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
312 restore_flags(flags);
316 action->handler = handler;
317 action->flags = irqflags;
319 action->name = devname;
321 action->dev_id = dev_id;
329 restore_flags(flags);
333 static void sun4d_disable_irq(unsigned int irq)
336 int tid = sbus_tid[(irq >> 5) - 1];
340 if (irq < NR_IRQS) return;
342 spin_lock_irqsave(&sun4d_imsk_lock, flags);
343 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
344 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
346 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
350 static void sun4d_enable_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]));
368 static void sun4d_set_cpu_int(int cpu, int level)
370 sun4d_send_ipi(cpu, level);
373 static void sun4d_clear_ipi(int cpu, int level)
377 static void sun4d_set_udt(int cpu)
381 /* Setup IRQ distribution scheme. */
382 void __init sun4d_distribute_irqs(void)
384 #ifdef DISTRIBUTE_IRQS
385 struct sbus_bus *sbus;
386 unsigned long sbus_serving_map;
388 sbus_serving_map = cpu_present_map;
389 for_each_sbus(sbus) {
390 if ((sbus->board * 2) == boot_cpu_id && (cpu_present_map & (1 << (sbus->board * 2 + 1))))
391 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
392 else if (cpu_present_map & (1 << (sbus->board * 2)))
393 sbus_tid[sbus->board] = (sbus->board * 2);
394 else if (cpu_present_map & (1 << (sbus->board * 2 + 1)))
395 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
397 sbus_tid[sbus->board] = 0xff;
398 if (sbus_tid[sbus->board] != 0xff)
399 sbus_serving_map &= ~(1 << sbus_tid[sbus->board]);
402 if (sbus_tid[sbus->board] == 0xff) {
405 if (!sbus_serving_map)
406 sbus_serving_map = cpu_present_map;
407 while (!(sbus_serving_map & (1 << i)))
409 sbus_tid[sbus->board] = i;
410 sbus_serving_map &= ~(1 << i);
412 for_each_sbus(sbus) {
413 printk("sbus%d IRQs directed to CPU%d\n", sbus->board, sbus_tid[sbus->board]);
414 set_sbi_tid(sbus->devid, sbus_tid[sbus->board] << 3);
417 struct sbus_bus *sbus;
418 int cpuid = cpu_logical_map(1);
421 cpuid = cpu_logical_map(0);
422 for_each_sbus(sbus) {
423 sbus_tid[sbus->board] = cpuid;
424 set_sbi_tid(sbus->devid, cpuid << 3);
426 printk("All sbus IRQs directed to CPU%d\n", cpuid);
431 static void sun4d_clear_clock_irq(void)
433 volatile unsigned int clear_intr;
434 clear_intr = sun4d_timers->l10_timer_limit;
437 static void sun4d_clear_profile_irq(int cpu)
439 bw_get_prof_limit(cpu);
442 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
444 bw_set_prof_limit(cpu, limit);
447 static void __init sun4d_init_timers(void (*counter_fn)(int, void *, struct pt_regs *))
450 extern struct prom_cpuinfo linux_cpus[NR_CPUS];
454 /* Map the User Timer registers. */
455 memset(&r, 0, sizeof(r));
457 r.start = CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT;
459 r.start = CSR_BASE(0)+BW_TIMER_LIMIT;
462 sun4d_timers = (struct sun4d_timer_regs *) sbus_ioremap(&r, 0,
463 PAGE_SIZE, "user timer");
465 sun4d_timers->l10_timer_limit = (((1000000/HZ) + 1) << 10);
466 master_l10_counter = &sun4d_timers->l10_cur_count;
467 master_l10_limit = &sun4d_timers->l10_timer_limit;
469 irq = request_irq(TIMER_IRQ,
471 (SA_INTERRUPT | SA_STATIC_ALLOC),
474 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
478 /* Enable user timer free run for CPU 0 in BW */
479 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
481 for(cpu = 0; cpu < linux_num_cpus; cpu++)
482 sun4d_load_profile_irq((linux_cpus[cpu].mid >> 3), 0);
487 extern unsigned long lvl14_save[4];
488 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
489 extern unsigned int real_irq_entry[], smp4d_ticker[];
490 extern unsigned int patchme_maybe_smp_msg[];
492 /* Adjust so that we jump directly to smp4d_ticker */
493 lvl14_save[2] += smp4d_ticker - real_irq_entry;
495 /* For SMP we use the level 14 ticker, however the bootup code
496 * has copied the firmwares level 14 vector into boot cpu's
497 * trap table, we must fix this now or we get squashed.
499 __save_and_cli(flags);
500 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
501 trap_table->inst_one = lvl14_save[0];
502 trap_table->inst_two = lvl14_save[1];
503 trap_table->inst_three = lvl14_save[2];
504 trap_table->inst_four = lvl14_save[3];
505 local_flush_cache_all();
506 __restore_flags(flags);
511 void __init sun4d_init_sbi_irq(void)
513 struct sbus_bus *sbus;
519 sbus_actions = (struct sbus_action *)kmalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
520 memset (sbus_actions, 0, (nsbi * 8 * 4 * sizeof(struct sbus_action)));
521 for_each_sbus(sbus) {
523 extern unsigned char boot_cpu_id;
525 set_sbi_tid(sbus->devid, boot_cpu_id << 3);
526 sbus_tid[sbus->board] = boot_cpu_id;
528 /* Get rid of pending irqs from PROM */
529 mask = acquire_sbi(sbus->devid, 0xffffffff);
531 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, sbus->board);
532 release_sbi(sbus->devid, mask);
537 static char *sun4d_irq_itoa(unsigned int irq)
539 static char buff[16];
542 sprintf(buff, "%d", irq);
544 sprintf(buff, "%d,%x", sbus_to_pil[(irq >> 2) & 7], irq);
548 void __init sun4d_init_IRQ(void)
552 BTFIXUPSET_CALL(sbint_to_irq, sun4d_sbint_to_irq, BTFIXUPCALL_NORM);
553 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
554 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
555 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
556 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
557 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
558 BTFIXUPSET_CALL(__irq_itoa, sun4d_irq_itoa, BTFIXUPCALL_NORM);
559 sparc_init_timers = sun4d_init_timers;
561 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
562 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
563 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
565 /* Cannot enable interrupts until OBP ticker is disabled. */