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.
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>
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/pgtable.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
;
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];
50 unsigned char sbus_tid
[32];
53 extern struct irqaction
*irq_action
[];
56 struct irqaction
*action
;
57 /* For SMP this needs to be extended */
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,
70 spinlock_t sun4d_imsk_lock
= SPIN_LOCK_UNLOCKED
;
73 int sun4d_get_irq_list(char *buf
)
75 int i
, j
= 0, k
= 0, len
= 0, sbusl
;
76 struct irqaction
* action
;
81 for (i
= 0 ; i
< NR_IRQS
; i
++) {
82 sbusl
= pil_to_sbus
[i
];
84 action
= *(i
+ irq_action
);
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
))
95 found_it
: len
+= sprintf(buf
+len
, "%3d: ", i
);
97 len
+= sprintf(buf
+len
, "%10u ", kstat_irqs(i
));
99 for (x
= 0; x
< smp_num_cpus
; x
++)
100 len
+= sprintf(buf
+len
, "%10u ",
101 kstat
.irqs
[cpu_logical_map(x
)][i
]);
103 len
+= sprintf(buf
+len
, "%c %s",
104 (action
->flags
& SA_INTERRUPT
) ? '+' : ' ',
106 action
= action
->next
;
108 for (; action
; action
= action
->next
) {
109 len
+= sprintf(buf
+len
, ",%s %s",
110 (action
->flags
& SA_INTERRUPT
) ? " +" : "",
116 action
= sbus_actions
[(j
<< 5) + (sbusl
<< 2) + k
].action
;
119 if (j
== nsbi
) break;
121 action
= sbus_actions
[(j
<< 5) + (sbusl
<< 2)].action
;
124 len
+= sprintf(buf
+len
, "\n");
129 void sun4d_free_irq(unsigned int irq
, void *dev_id
)
131 struct irqaction
*action
, **actionp
;
132 struct irqaction
*tmp
= NULL
;
136 actionp
= irq
+ irq_action
;
138 actionp
= &(sbus_actions
[irq
- (1 << 5)].action
);
141 printk("Trying to free free IRQ%d\n",irq
);
145 for (; action
; action
= action
->next
) {
146 if (action
->dev_id
== dev_id
)
151 printk("Trying to free free shared IRQ%d\n",irq
);
154 } else if (action
->flags
& SA_SHIRQ
) {
155 printk("Trying to free shared IRQ%d with NULL device ID\n", irq
);
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",
170 tmp
->next
= action
->next
;
172 *actionp
= action
->next
;
174 kfree_s(action
, sizeof(struct irqaction
));
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?? */
194 cc_set_iclr(1 << irq
);
197 kstat
.irqs
[cpu
][irq
]++;
199 action
= *(irq
+ irq_action
);
201 unexpected_irq(irq
, 0, regs
);
203 action
->handler(irq
, action
->dev_id
, regs
);
204 action
= action
->next
;
207 int bus_mask
= bw_get_intr_mask(sbusl
) & 0x3ffff;
209 struct sbus_action
*actionp
;
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)
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
++)
225 action
= actionp
->action
;
228 unexpected_irq(irq
, 0, regs
);
230 action
->handler(irq
, action
->dev_id
, regs
);
231 action
= action
->next
;
233 release_sbi(SBI2DEVID(sbino
), slot
);
240 unsigned int sun4d_build_irq(struct linux_sbus_device
*sdev
, int irq
)
242 int sbusl
= pil_to_sbus
[irq
];
245 return ((sdev
->my_bus
->board
+ 1) << 5) + (sbusl
<< 2) + sdev
->slot
;
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
;
257 if(irq
> 14 && irq
< (1 << 5))
264 actionp
= &(sbus_actions
[irq
- (1 << 5)].action
);
266 actionp
= irq
+ irq_action
;
270 if ((action
->flags
& SA_SHIRQ
) && (irqflags
& SA_SHIRQ
)) {
271 for (tmp
= action
; tmp
->next
; tmp
= tmp
->next
);
275 if ((action
->flags
& SA_INTERRUPT
) ^ (irqflags
& SA_INTERRUPT
)) {
276 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq
);
279 action
= NULL
; /* Or else! */
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
++];
291 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",irq
, devname
);
295 action
= (struct irqaction
*)kmalloc(sizeof(struct irqaction
),
299 restore_flags(flags
);
303 action
->handler
= handler
;
304 action
->flags
= irqflags
;
306 action
->name
= devname
;
308 action
->dev_id
= dev_id
;
316 restore_flags(flags
);
320 static void sun4d_disable_irq(unsigned int irq
)
323 int tid
= sbus_tid
[(irq
>> 5) - 1];
327 if (irq
< NR_IRQS
) return;
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
);
333 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil
[(irq
>> 2) & 7]));
337 static void sun4d_enable_irq(unsigned int irq
)
340 int tid
= sbus_tid
[(irq
>> 5) - 1];
344 if (irq
< NR_IRQS
) return;
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
);
350 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil
[(irq
>> 2) & 7]));
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);
384 sbus_tid
[sbus
->board
] = 0xff;
385 if (sbus_tid
[sbus
->board
] != 0xff)
386 sbus_serving_map
&= ~(1 << sbus_tid
[sbus
->board
]);
389 if (sbus_tid
[sbus
->board
] == 0xff) {
392 if (!sbus_serving_map
)
393 sbus_serving_map
= cpu_present_map
;
394 while (!(sbus_serving_map
& (1 << 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);
404 struct linux_sbus
*sbus
;
405 int cpuid
= cpu_logical_map(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
);
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
*))
437 extern struct prom_cpuinfo linux_cpus
[NR_CPUS
];
440 /* Map the User Timer registers. */
442 sun4d_timers
= sparc_alloc_io(CSR_BASE(boot_cpu_id
)+BW_TIMER_LIMIT
, 0,
443 PAGE_SIZE
, "user timer", 0xf, 0x0);
445 sun4d_timers
= sparc_alloc_io(CSR_BASE(0)+BW_TIMER_LIMIT
, 0,
446 PAGE_SIZE
, "user timer", 0xf, 0x0);
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
,
455 (SA_INTERRUPT
| SA_STATIC_ALLOC
),
458 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ
);
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);
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
);
495 void __init
sun4d_init_sbi_irq(void)
497 struct linux_sbus
*sbus
;
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
) {
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
;
512 /* Get rid of pending irqs from PROM */
513 mask
= acquire_sbi(sbus
->devid
, 0xffffffff);
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];
526 sprintf(buff
, "%d", irq
);
528 sprintf(buff
, "%d,%x", sbus_to_pil
[(irq
>> 2) & 7], irq
);
532 void __init
sun4d_init_IRQ(void)
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
;
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
);
548 /* Cannot enable interrupts until OBP ticker is disabled. */