2 * SS1000/SC2000 interrupt handling.
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Heavily based on arch/sparc/kernel/irq.c.
8 #include <linux/kernel_stat.h>
9 #include <linux/seq_file.h>
11 #include <asm/timer.h>
12 #include <asm/traps.h>
16 #include <asm/cacheflush.h>
21 /* Sun4d interrupts fall roughly into two categories. SBUS and
22 * cpu local. CPU local interrupts cover the timer interrupts
23 * and whatnot, and we encode those as normal PILs between
26 * SBUS interrupts are encoded integers including the board number
27 * (plus one), the SBUS level, and the SBUS slot number. Sun4D
28 * IRQ dispatch is done by:
30 * 1) Reading the BW local interrupt table in order to get the bus
33 * This table is indexed by SBUS interrupt level which can be
34 * derived from the PIL we got interrupted on.
36 * 2) For each bus showing interrupt pending from #1, read the
37 * SBI interrupt state register. This will indicate which slots
38 * have interrupts pending for that SBUS interrupt level.
41 struct sun4d_timer_regs {
44 u32 l10_limit_noclear;
49 static struct sun4d_timer_regs __iomem *sun4d_timers;
53 #define MAX_STATIC_ALLOC 4
54 static unsigned char sbus_tid[32];
56 static struct irqaction *irq_action[NR_IRQS];
58 static struct sbus_action {
59 struct irqaction *action;
60 /* For SMP this needs to be extended */
63 static int pil_to_sbus[] = {
82 static int sbus_to_pil[] = {
95 /* Exported for sun4d_smp.c */
96 DEFINE_SPINLOCK(sun4d_imsk_lock);
98 int show_sun4d_interrupts(struct seq_file *p, void *v)
100 int i = *(loff_t *) v, j = 0, k = 0, sbusl;
101 struct irqaction *action;
107 spin_lock_irqsave(&irq_action_lock, flags);
109 sbusl = pil_to_sbus[i];
111 action = *(i + irq_action);
115 for (j = 0; j < nsbi; j++) {
116 for (k = 0; k < 4; k++)
117 action = sbus_actions[(j << 5) + (sbusl << 2) + k].action;
123 found_it: seq_printf(p, "%3d: ", i);
125 seq_printf(p, "%10u ", kstat_irqs(i));
127 for_each_online_cpu(x)
128 seq_printf(p, "%10u ",
129 kstat_cpu(cpu_logical_map(x)).irqs[i]);
131 seq_printf(p, "%c %s",
132 (action->flags & IRQF_DISABLED) ? '+' : ' ',
134 action = action->next;
136 for (; action; action = action->next) {
137 seq_printf(p, ",%s %s",
138 (action->flags & IRQF_DISABLED) ? " +" : "",
145 action = sbus_actions[(j << 5) + (sbusl << 2) + k].action;
151 action = sbus_actions[(j << 5) + (sbusl << 2)].action;
157 spin_unlock_irqrestore(&irq_action_lock, flags);
161 void sun4d_free_irq(unsigned int irq, void *dev_id)
163 struct irqaction *action, **actionp;
164 struct irqaction *tmp = NULL;
167 spin_lock_irqsave(&irq_action_lock, flags);
169 actionp = irq + irq_action;
171 actionp = &(sbus_actions[irq - (1 << 5)].action);
174 printk(KERN_ERR "Trying to free free IRQ%d\n", irq);
178 for (; action; action = action->next) {
179 if (action->dev_id == dev_id)
184 printk(KERN_ERR "Trying to free free shared IRQ%d\n",
188 } else if (action->flags & IRQF_SHARED) {
189 printk(KERN_ERR "Trying to free shared IRQ%d with NULL device ID\n",
193 if (action->flags & SA_STATIC_ALLOC) {
195 * This interrupt is marked as specially allocated
196 * so it is a bad idea to free it.
198 printk(KERN_ERR "Attempt to free statically allocated IRQ%d (%s)\n",
204 tmp->next = action->next;
206 *actionp = action->next;
208 spin_unlock_irqrestore(&irq_action_lock, flags);
210 synchronize_irq(irq);
212 spin_lock_irqsave(&irq_action_lock, flags);
220 spin_unlock_irqrestore(&irq_action_lock, flags);
223 void sun4d_handler_irq(int pil, struct pt_regs *regs)
225 struct pt_regs *old_regs;
226 struct irqaction *action;
227 int cpu = smp_processor_id();
228 /* SBUS IRQ level (1 - 7) */
229 int sbusl = pil_to_sbus[pil];
231 /* FIXME: Is this necessary?? */
234 cc_set_iclr(1 << pil);
236 old_regs = set_irq_regs(regs);
238 kstat_cpu(cpu).irqs[pil]++;
240 action = *(pil + irq_action);
242 unexpected_irq(pil, NULL, regs);
244 action->handler(pil, action->dev_id);
245 action = action->next;
248 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
250 struct sbus_action *actionp;
252 int sbil = (sbusl << 2);
254 bw_clear_intr_mask(sbusl, bus_mask);
256 /* Loop for each pending SBI */
257 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
259 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
260 mask &= (0xf << sbil);
261 actionp = sbus_actions + (sbino << 5) + (sbil);
262 /* Loop for each pending SBI slot */
263 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
266 action = actionp->action;
269 unexpected_irq(pil, NULL, regs);
271 action->handler(pil, action->dev_id);
272 action = action->next;
274 release_sbi(SBI2DEVID(sbino), slot);
279 set_irq_regs(old_regs);
282 int sun4d_request_irq(unsigned int irq,
283 irq_handler_t handler,
284 unsigned long irqflags, const char *devname, void *dev_id)
286 struct irqaction *action, *tmp = NULL, **actionp;
290 if (irq > 14 && irq < (1 << 5)) {
300 spin_lock_irqsave(&irq_action_lock, flags);
303 actionp = &(sbus_actions[irq - (1 << 5)].action);
305 actionp = irq + irq_action;
309 if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) {
310 for (tmp = action; tmp->next; tmp = tmp->next)
311 /* find last entry - tmp used below */;
316 if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) {
317 printk(KERN_ERR "Attempt to mix fast and slow interrupts on IRQ%d denied\n",
322 action = NULL; /* Or else! */
325 /* If this is flagged as statically allocated then we use our
326 * private struct which is never freed.
328 if (irqflags & SA_STATIC_ALLOC) {
329 if (static_irq_count < MAX_STATIC_ALLOC)
330 action = &static_irqaction[static_irq_count++];
332 printk(KERN_ERR "Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",
337 action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
344 action->handler = handler;
345 action->flags = irqflags;
346 action->name = devname;
348 action->dev_id = dev_id;
359 spin_unlock_irqrestore(&irq_action_lock, flags);
364 static void sun4d_disable_irq(unsigned int irq)
366 int tid = sbus_tid[(irq >> 5) - 1];
372 spin_lock_irqsave(&sun4d_imsk_lock, flags);
373 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
374 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
377 static void sun4d_enable_irq(unsigned int irq)
379 int tid = sbus_tid[(irq >> 5) - 1];
385 spin_lock_irqsave(&sun4d_imsk_lock, flags);
386 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
387 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
391 static void sun4d_set_cpu_int(int cpu, int level)
393 sun4d_send_ipi(cpu, level);
396 static void sun4d_clear_ipi(int cpu, int level)
400 static void sun4d_set_udt(int cpu)
404 /* Setup IRQ distribution scheme. */
405 void __init sun4d_distribute_irqs(void)
407 struct device_node *dp;
409 int cpuid = cpu_logical_map(1);
412 cpuid = cpu_logical_map(0);
413 for_each_node_by_name(dp, "sbi") {
414 int devid = of_getintprop_default(dp, "device-id", 0);
415 int board = of_getintprop_default(dp, "board#", 0);
416 sbus_tid[board] = cpuid;
417 set_sbi_tid(devid, cpuid << 3);
419 printk(KERN_ERR "All sbus IRQs directed to CPU%d\n", cpuid);
423 static void sun4d_clear_clock_irq(void)
425 sbus_readl(&sun4d_timers->l10_timer_limit);
428 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
430 bw_set_prof_limit(cpu, limit);
433 static void __init sun4d_load_profile_irqs(void)
437 while (!cpu_find_by_instance(cpu, NULL, &mid)) {
438 sun4d_load_profile_irq(mid >> 3, 0);
443 unsigned int sun4d_build_device_irq(struct platform_device *op,
444 unsigned int real_irq)
446 static int pil_to_sbus[] = {
447 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
449 struct device_node *dp = op->dev.of_node;
450 struct device_node *io_unit, *sbi = dp->parent;
451 const struct linux_prom_registers *regs;
456 if (!strcmp(sbi->name, "sbi"))
464 regs = of_get_property(dp, "reg", NULL);
468 slot = regs->which_io;
471 * If SBI's parent is not io-unit or the io-unit lacks
472 * a "board#" property, something is very wrong.
474 if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
475 printk("%s: Error, parent is not io-unit.\n", sbi->full_name);
478 io_unit = sbi->parent;
479 board = of_getintprop_default(io_unit, "board#", -1);
481 printk("%s: Error, lacks board# property.\n", io_unit->full_name);
485 sbusl = pil_to_sbus[real_irq];
487 return (((board + 1) << 5) + (sbusl << 2) + slot);
493 static void __init sun4d_fixup_trap_table(void)
497 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
499 /* Adjust so that we jump directly to smp4d_ticker */
500 lvl14_save[2] += smp4d_ticker - real_irq_entry;
502 /* For SMP we use the level 14 ticker, however the bootup code
503 * has copied the firmware's level 14 vector into the boot cpu's
504 * trap table, we must fix this now or we get squashed.
506 local_irq_save(flags);
507 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
508 trap_table->inst_one = lvl14_save[0];
509 trap_table->inst_two = lvl14_save[1];
510 trap_table->inst_three = lvl14_save[2];
511 trap_table->inst_four = lvl14_save[3];
512 local_flush_cache_all();
513 local_irq_restore(flags);
517 static void __init sun4d_init_timers(irq_handler_t counter_fn)
519 struct device_node *dp;
524 dp = of_find_node_by_name(NULL, "cpu-unit");
526 prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
530 /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
531 * registers via any cpu's mapping. The first 'reg' property is the
534 reg = of_get_property(dp, "reg", NULL);
537 prom_printf("sun4d_init_timers: No reg property\n");
542 res.end = reg[2] - 1;
543 res.flags = reg[0] & 0xff;
544 sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
545 sizeof(struct sun4d_timer_regs), "user timer");
547 prom_printf("sun4d_init_timers: Can't map timer regs\n");
551 sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit);
553 master_l10_counter = &sun4d_timers->l10_cur_count;
555 err = request_irq(TIMER_IRQ, counter_fn,
556 (IRQF_DISABLED | SA_STATIC_ALLOC),
559 prom_printf("sun4d_init_timers: request_irq() failed with %d\n",
563 sun4d_load_profile_irqs();
564 sun4d_fixup_trap_table();
567 void __init sun4d_init_sbi_irq(void)
569 struct device_node *dp;
573 target_cpu = boot_cpu_id;
577 for_each_node_by_name(dp, "sbi")
579 sbus_actions = kzalloc(nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
581 prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
584 for_each_node_by_name(dp, "sbi") {
585 int devid = of_getintprop_default(dp, "device-id", 0);
586 int board = of_getintprop_default(dp, "board#", 0);
589 set_sbi_tid(devid, target_cpu << 3);
590 sbus_tid[board] = target_cpu;
592 /* Get rid of pending irqs from PROM */
593 mask = acquire_sbi(devid, 0xffffffff);
595 printk(KERN_ERR "Clearing pending IRQs %08x on SBI %d\n",
597 release_sbi(devid, mask);
602 void __init sun4d_init_IRQ(void)
606 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
607 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
608 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
609 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
611 sparc_irq_config.init_timers = sun4d_init_timers;
612 sparc_irq_config.build_device_irq = sun4d_build_device_irq;
615 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
616 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
617 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
619 /* Cannot enable interrupts until OBP ticker is disabled. */