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.
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>
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
);
242 unsigned int sun4d_build_irq(struct sbus_dev
*sdev
, int irq
)
244 int sbusl
= pil_to_sbus
[irq
];
247 return ((sdev
->bus
->board
+ 1) << 5) + (sbusl
<< 2) + sdev
->slot
;
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
;
259 if(irq
> 14 && irq
< (1 << 5))
266 actionp
= &(sbus_actions
[irq
- (1 << 5)].action
);
268 actionp
= irq
+ irq_action
;
272 if ((action
->flags
& SA_SHIRQ
) && (irqflags
& SA_SHIRQ
)) {
273 for (tmp
= action
; tmp
->next
; tmp
= tmp
->next
);
277 if ((action
->flags
& SA_INTERRUPT
) ^ (irqflags
& SA_INTERRUPT
)) {
278 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq
);
281 action
= NULL
; /* Or else! */
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
++];
293 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",irq
, devname
);
297 action
= (struct irqaction
*)kmalloc(sizeof(struct irqaction
),
301 restore_flags(flags
);
305 action
->handler
= handler
;
306 action
->flags
= irqflags
;
308 action
->name
= devname
;
310 action
->dev_id
= dev_id
;
318 restore_flags(flags
);
322 static void sun4d_disable_irq(unsigned int irq
)
325 int tid
= sbus_tid
[(irq
>> 5) - 1];
329 if (irq
< NR_IRQS
) return;
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
);
335 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil
[(irq
>> 2) & 7]));
339 static void sun4d_enable_irq(unsigned int irq
)
342 int tid
= sbus_tid
[(irq
>> 5) - 1];
346 if (irq
< NR_IRQS
) return;
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
);
352 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil
[(irq
>> 2) & 7]));
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);
386 sbus_tid
[sbus
->board
] = 0xff;
387 if (sbus_tid
[sbus
->board
] != 0xff)
388 sbus_serving_map
&= ~(1 << sbus_tid
[sbus
->board
]);
391 if (sbus_tid
[sbus
->board
] == 0xff) {
394 if (!sbus_serving_map
)
395 sbus_serving_map
= cpu_present_map
;
396 while (!(sbus_serving_map
& (1 << 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);
406 struct sbus_bus
*sbus
;
407 int cpuid
= cpu_logical_map(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
);
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
*))
439 extern struct prom_cpuinfo linux_cpus
[NR_CPUS
];
443 /* Map the User Timer registers. */
444 memset(&r
, 0, sizeof(r
));
446 r
.start
= CSR_BASE(boot_cpu_id
)+BW_TIMER_LIMIT
;
448 r
.start
= CSR_BASE(0)+BW_TIMER_LIMIT
;
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
,
460 (SA_INTERRUPT
| SA_STATIC_ALLOC
),
463 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ
);
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);
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
);
500 void __init
sun4d_init_sbi_irq(void)
502 struct sbus_bus
*sbus
;
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
) {
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
;
517 /* Get rid of pending irqs from PROM */
518 mask
= acquire_sbi(sbus
->devid
, 0xffffffff);
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];
531 sprintf(buff
, "%d", irq
);
533 sprintf(buff
, "%d,%x", sbus_to_pil
[(irq
>> 2) & 7], irq
);
537 void __init
sun4d_init_IRQ(void)
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
;
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
);
553 /* Cannot enable interrupts until OBP ticker is disabled. */