1 /* $Id: sun4d_irq.c,v 1.29 2001/12/11 04:55:51 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/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>
42 /* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
43 /* #define DISTRIBUTE_IRQS */
45 struct sun4d_timer_regs
*sun4d_timers
;
48 #define MAX_STATIC_ALLOC 4
49 extern struct irqaction static_irqaction
[MAX_STATIC_ALLOC
];
50 extern int static_irq_count
;
51 unsigned char cpu_leds
[32];
53 unsigned char sbus_tid
[32];
56 static struct irqaction
*irq_action
[NR_IRQS
];
57 extern spinlock_t irq_action_lock
;
60 struct irqaction
*action
;
61 /* For SMP this needs to be extended */
64 static int pil_to_sbus
[] = {
65 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
68 static int sbus_to_pil
[] = {
69 0, 2, 3, 5, 7, 9, 11, 13,
74 DEFINE_SPINLOCK(sun4d_imsk_lock
);
77 int show_sun4d_interrupts(struct seq_file
*p
, void *v
)
79 int i
= *(loff_t
*) v
, j
= 0, k
= 0, sbusl
;
80 struct irqaction
* action
;
86 spin_lock_irqsave(&irq_action_lock
, flags
);
88 sbusl
= pil_to_sbus
[i
];
90 action
= *(i
+ irq_action
);
94 for (j
= 0; j
< nsbi
; j
++) {
95 for (k
= 0; k
< 4; k
++)
96 if ((action
= sbus_actions
[(j
<< 5) + (sbusl
<< 2) + k
].action
))
101 found_it
: seq_printf(p
, "%3d: ", i
);
103 seq_printf(p
, "%10u ", kstat_irqs(i
));
105 for_each_online_cpu(x
)
106 seq_printf(p
, "%10u ",
107 kstat_cpu(cpu_logical_map(x
)).irqs
[i
]);
109 seq_printf(p
, "%c %s",
110 (action
->flags
& IRQF_DISABLED
) ? '+' : ' ',
112 action
= action
->next
;
114 for (; action
; action
= action
->next
) {
115 seq_printf(p
, ",%s %s",
116 (action
->flags
& IRQF_DISABLED
) ? " +" : "",
122 action
= sbus_actions
[(j
<< 5) + (sbusl
<< 2) + k
].action
;
125 if (j
== nsbi
) break;
127 action
= sbus_actions
[(j
<< 5) + (sbusl
<< 2)].action
;
133 spin_unlock_irqrestore(&irq_action_lock
, flags
);
137 void sun4d_free_irq(unsigned int irq
, void *dev_id
)
139 struct irqaction
*action
, **actionp
;
140 struct irqaction
*tmp
= NULL
;
143 spin_lock_irqsave(&irq_action_lock
, flags
);
145 actionp
= irq
+ irq_action
;
147 actionp
= &(sbus_actions
[irq
- (1 << 5)].action
);
150 printk("Trying to free free IRQ%d\n",irq
);
154 for (; action
; action
= action
->next
) {
155 if (action
->dev_id
== dev_id
)
160 printk("Trying to free free shared IRQ%d\n",irq
);
163 } else if (action
->flags
& IRQF_SHARED
) {
164 printk("Trying to free shared IRQ%d with NULL device ID\n", irq
);
167 if (action
->flags
& SA_STATIC_ALLOC
)
169 /* This interrupt is marked as specially allocated
170 * so it is a bad idea to free it.
172 printk("Attempt to free statically allocated IRQ%d (%s)\n",
178 tmp
->next
= action
->next
;
180 *actionp
= action
->next
;
182 spin_unlock_irqrestore(&irq_action_lock
, flags
);
184 synchronize_irq(irq
);
186 spin_lock_irqsave(&irq_action_lock
, flags
);
194 spin_unlock_irqrestore(&irq_action_lock
, flags
);
197 extern void unexpected_irq(int, void *, struct pt_regs
*);
199 void sun4d_handler_irq(int irq
, struct pt_regs
* regs
)
201 struct pt_regs
*old_regs
;
202 struct irqaction
* action
;
203 int cpu
= smp_processor_id();
204 /* SBUS IRQ level (1 - 7) */
205 int sbusl
= pil_to_sbus
[irq
];
207 /* FIXME: Is this necessary?? */
210 cc_set_iclr(1 << irq
);
212 old_regs
= set_irq_regs(regs
);
214 kstat_cpu(cpu
).irqs
[irq
]++;
216 action
= *(irq
+ irq_action
);
218 unexpected_irq(irq
, NULL
, regs
);
220 action
->handler(irq
, action
->dev_id
);
221 action
= action
->next
;
224 int bus_mask
= bw_get_intr_mask(sbusl
) & 0x3ffff;
226 struct sbus_action
*actionp
;
228 int sbil
= (sbusl
<< 2);
230 bw_clear_intr_mask(sbusl
, bus_mask
);
232 /* Loop for each pending SBI */
233 for (sbino
= 0; bus_mask
; sbino
++, bus_mask
>>= 1)
235 mask
= acquire_sbi(SBI2DEVID(sbino
), 0xf << sbil
);
236 mask
&= (0xf << sbil
);
237 actionp
= sbus_actions
+ (sbino
<< 5) + (sbil
);
238 /* Loop for each pending SBI slot */
239 for (slot
= (1 << sbil
); mask
; slot
<<= 1, actionp
++)
242 action
= actionp
->action
;
245 unexpected_irq(irq
, NULL
, regs
);
247 action
->handler(irq
, action
->dev_id
);
248 action
= action
->next
;
250 release_sbi(SBI2DEVID(sbino
), slot
);
255 set_irq_regs(old_regs
);
258 unsigned int sun4d_build_irq(struct sbus_dev
*sdev
, int irq
)
260 int sbusl
= pil_to_sbus
[irq
];
263 return ((sdev
->bus
->board
+ 1) << 5) + (sbusl
<< 2) + sdev
->slot
;
268 unsigned int sun4d_sbint_to_irq(struct sbus_dev
*sdev
, unsigned int sbint
)
270 if (sbint
>= sizeof(sbus_to_pil
)) {
271 printk(KERN_ERR
"%s: bogus SBINT %d\n", sdev
->prom_name
, sbint
);
274 return sun4d_build_irq(sdev
, sbus_to_pil
[sbint
]);
277 int sun4d_request_irq(unsigned int irq
,
278 irq_handler_t handler
,
279 unsigned long irqflags
, const char * devname
, void *dev_id
)
281 struct irqaction
*action
, *tmp
= NULL
, **actionp
;
285 if(irq
> 14 && irq
< (1 << 5)) {
295 spin_lock_irqsave(&irq_action_lock
, flags
);
298 actionp
= &(sbus_actions
[irq
- (1 << 5)].action
);
300 actionp
= irq
+ irq_action
;
304 if ((action
->flags
& IRQF_SHARED
) && (irqflags
& IRQF_SHARED
)) {
305 for (tmp
= action
; tmp
->next
; tmp
= tmp
->next
);
310 if ((action
->flags
& IRQF_DISABLED
) ^ (irqflags
& IRQF_DISABLED
)) {
311 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq
);
315 action
= NULL
; /* Or else! */
318 /* If this is flagged as statically allocated then we use our
319 * private struct which is never freed.
321 if (irqflags
& SA_STATIC_ALLOC
) {
322 if (static_irq_count
< MAX_STATIC_ALLOC
)
323 action
= &static_irqaction
[static_irq_count
++];
325 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq
, devname
);
329 action
= kmalloc(sizeof(struct irqaction
),
337 action
->handler
= handler
;
338 action
->flags
= irqflags
;
339 cpus_clear(action
->mask
);
340 action
->name
= devname
;
342 action
->dev_id
= dev_id
;
353 spin_unlock_irqrestore(&irq_action_lock
, flags
);
358 static void sun4d_disable_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]));
375 static void sun4d_enable_irq(unsigned int irq
)
378 int tid
= sbus_tid
[(irq
>> 5) - 1];
382 if (irq
< NR_IRQS
) return;
384 spin_lock_irqsave(&sun4d_imsk_lock
, flags
);
385 cc_set_imsk_other(tid
, cc_get_imsk_other(tid
) & ~(1 << sbus_to_pil
[(irq
>> 2) & 7]));
386 spin_unlock_irqrestore(&sun4d_imsk_lock
, flags
);
388 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil
[(irq
>> 2) & 7]));
393 static void sun4d_set_cpu_int(int cpu
, int level
)
395 sun4d_send_ipi(cpu
, level
);
398 static void sun4d_clear_ipi(int cpu
, int level
)
402 static void sun4d_set_udt(int cpu
)
406 /* Setup IRQ distribution scheme. */
407 void __init
sun4d_distribute_irqs(void)
409 #ifdef DISTRIBUTE_IRQS
410 struct sbus_bus
*sbus
;
411 unsigned long sbus_serving_map
;
413 sbus_serving_map
= cpu_present_map
;
414 for_each_sbus(sbus
) {
415 if ((sbus
->board
* 2) == boot_cpu_id
&& (cpu_present_map
& (1 << (sbus
->board
* 2 + 1))))
416 sbus_tid
[sbus
->board
] = (sbus
->board
* 2 + 1);
417 else if (cpu_present_map
& (1 << (sbus
->board
* 2)))
418 sbus_tid
[sbus
->board
] = (sbus
->board
* 2);
419 else if (cpu_present_map
& (1 << (sbus
->board
* 2 + 1)))
420 sbus_tid
[sbus
->board
] = (sbus
->board
* 2 + 1);
422 sbus_tid
[sbus
->board
] = 0xff;
423 if (sbus_tid
[sbus
->board
] != 0xff)
424 sbus_serving_map
&= ~(1 << sbus_tid
[sbus
->board
]);
427 if (sbus_tid
[sbus
->board
] == 0xff) {
430 if (!sbus_serving_map
)
431 sbus_serving_map
= cpu_present_map
;
432 while (!(sbus_serving_map
& (1 << i
)))
434 sbus_tid
[sbus
->board
] = i
;
435 sbus_serving_map
&= ~(1 << i
);
437 for_each_sbus(sbus
) {
438 printk("sbus%d IRQs directed to CPU%d\n", sbus
->board
, sbus_tid
[sbus
->board
]);
439 set_sbi_tid(sbus
->devid
, sbus_tid
[sbus
->board
] << 3);
442 struct sbus_bus
*sbus
;
443 int cpuid
= cpu_logical_map(1);
446 cpuid
= cpu_logical_map(0);
447 for_each_sbus(sbus
) {
448 sbus_tid
[sbus
->board
] = cpuid
;
449 set_sbi_tid(sbus
->devid
, cpuid
<< 3);
451 printk("All sbus IRQs directed to CPU%d\n", cpuid
);
456 static void sun4d_clear_clock_irq(void)
458 volatile unsigned int clear_intr
;
459 clear_intr
= sun4d_timers
->l10_timer_limit
;
462 static void sun4d_clear_profile_irq(int cpu
)
464 bw_get_prof_limit(cpu
);
467 static void sun4d_load_profile_irq(int cpu
, unsigned int limit
)
469 bw_set_prof_limit(cpu
, limit
);
472 static void __init
sun4d_init_timers(irq_handler_t counter_fn
)
479 /* Map the User Timer registers. */
480 memset(&r
, 0, sizeof(r
));
482 r
.start
= CSR_BASE(boot_cpu_id
)+BW_TIMER_LIMIT
;
484 r
.start
= CSR_BASE(0)+BW_TIMER_LIMIT
;
487 sun4d_timers
= (struct sun4d_timer_regs
*) sbus_ioremap(&r
, 0,
488 PAGE_SIZE
, "user timer");
490 sun4d_timers
->l10_timer_limit
= (((1000000/HZ
) + 1) << 10);
491 master_l10_counter
= &sun4d_timers
->l10_cur_count
;
492 master_l10_limit
= &sun4d_timers
->l10_timer_limit
;
494 irq
= request_irq(TIMER_IRQ
,
496 (IRQF_DISABLED
| SA_STATIC_ALLOC
),
499 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ
);
503 /* Enable user timer free run for CPU 0 in BW */
504 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
507 while (!cpu_find_by_instance(cpu
, NULL
, &mid
)) {
508 sun4d_load_profile_irq(mid
>> 3, 0);
515 extern unsigned long lvl14_save
[4];
516 struct tt_entry
*trap_table
= &sparc_ttable
[SP_TRAP_IRQ1
+ (14 - 1)];
517 extern unsigned int real_irq_entry
[], smp4d_ticker
[];
518 extern unsigned int patchme_maybe_smp_msg
[];
520 /* Adjust so that we jump directly to smp4d_ticker */
521 lvl14_save
[2] += smp4d_ticker
- real_irq_entry
;
523 /* For SMP we use the level 14 ticker, however the bootup code
524 * has copied the firmware's level 14 vector into the boot cpu's
525 * trap table, we must fix this now or we get squashed.
527 local_irq_save(flags
);
528 patchme_maybe_smp_msg
[0] = 0x01000000; /* NOP out the branch */
529 trap_table
->inst_one
= lvl14_save
[0];
530 trap_table
->inst_two
= lvl14_save
[1];
531 trap_table
->inst_three
= lvl14_save
[2];
532 trap_table
->inst_four
= lvl14_save
[3];
533 local_flush_cache_all();
534 local_irq_restore(flags
);
539 void __init
sun4d_init_sbi_irq(void)
541 struct sbus_bus
*sbus
;
547 sbus_actions
= kzalloc (nsbi
* 8 * 4 * sizeof(struct sbus_action
), GFP_ATOMIC
);
549 prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
552 for_each_sbus(sbus
) {
554 extern unsigned char boot_cpu_id
;
556 set_sbi_tid(sbus
->devid
, boot_cpu_id
<< 3);
557 sbus_tid
[sbus
->board
] = boot_cpu_id
;
559 /* Get rid of pending irqs from PROM */
560 mask
= acquire_sbi(sbus
->devid
, 0xffffffff);
562 printk ("Clearing pending IRQs %08x on SBI %d\n", mask
, sbus
->board
);
563 release_sbi(sbus
->devid
, mask
);
568 void __init
sun4d_init_IRQ(void)
572 BTFIXUPSET_CALL(sbint_to_irq
, sun4d_sbint_to_irq
, BTFIXUPCALL_NORM
);
573 BTFIXUPSET_CALL(enable_irq
, sun4d_enable_irq
, BTFIXUPCALL_NORM
);
574 BTFIXUPSET_CALL(disable_irq
, sun4d_disable_irq
, BTFIXUPCALL_NORM
);
575 BTFIXUPSET_CALL(clear_clock_irq
, sun4d_clear_clock_irq
, BTFIXUPCALL_NORM
);
576 BTFIXUPSET_CALL(clear_profile_irq
, sun4d_clear_profile_irq
, BTFIXUPCALL_NORM
);
577 BTFIXUPSET_CALL(load_profile_irq
, sun4d_load_profile_irq
, BTFIXUPCALL_NORM
);
578 sparc_init_timers
= sun4d_init_timers
;
580 BTFIXUPSET_CALL(set_cpu_int
, sun4d_set_cpu_int
, BTFIXUPCALL_NORM
);
581 BTFIXUPSET_CALL(clear_cpu_int
, sun4d_clear_ipi
, BTFIXUPCALL_NOP
);
582 BTFIXUPSET_CALL(set_irq_udt
, sun4d_set_udt
, BTFIXUPCALL_NOP
);
584 /* Cannot enable interrupts until OBP ticker is disabled. */