1/* 2 * arch/sparc/kernel/sun4d_irq.c: 3 * SS1000/SC2000 interrupt handling. 4 * 5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 6 * Heavily based on arch/sparc/kernel/irq.c. 7 */ 8 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> 22#include <linux/of.h> 23#include <linux/of_device.h> 24 25#include <asm/ptrace.h> 26#include <asm/processor.h> 27#include <asm/system.h> 28#include <asm/psr.h> 29#include <asm/smp.h> 30#include <asm/vaddrs.h> 31#include <asm/timer.h> 32#include <asm/openprom.h> 33#include <asm/oplib.h> 34#include <asm/traps.h> 35#include <asm/irq.h> 36#include <asm/io.h> 37#include <asm/pgalloc.h> 38#include <asm/pgtable.h> 39#include <asm/sbi.h> 40#include <asm/cacheflush.h> 41#include <asm/irq_regs.h> 42 43#include "kernel.h" 44#include "irq.h" 45 46/* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */ 47/* #define DISTRIBUTE_IRQS */ 48 49struct sun4d_timer_regs { 50 u32 l10_timer_limit; 51 u32 l10_cur_countx; 52 u32 l10_limit_noclear; 53 u32 ctrl; 54 u32 l10_cur_count; 55}; 56 57static struct sun4d_timer_regs __iomem *sun4d_timers; 58 59#define TIMER_IRQ 10 60 61#define MAX_STATIC_ALLOC 4 62extern int static_irq_count; 63static unsigned char sbus_tid[32]; 64 65static struct irqaction *irq_action[NR_IRQS]; 66extern spinlock_t irq_action_lock; 67 68static struct sbus_action { 69 struct irqaction *action; 70 /* For SMP this needs to be extended */ 71} *sbus_actions; 72 73static int pil_to_sbus[] = { 74 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, 75}; 76 77static int sbus_to_pil[] = { 78 0, 2, 3, 5, 7, 9, 11, 13, 79}; 80 81static int nsbi; 82 83/* Exported for sun4d_smp.c */ 84DEFINE_SPINLOCK(sun4d_imsk_lock); 85 86int show_sun4d_interrupts(struct seq_file *p, void *v) 87{ 88 int i = *(loff_t *) v, j = 0, k = 0, sbusl; 89 struct irqaction * action; 90 unsigned long flags; 91#ifdef CONFIG_SMP 92 int x; 93#endif 94 95 spin_lock_irqsave(&irq_action_lock, flags); 96 if (i < NR_IRQS) { 97 sbusl = pil_to_sbus[i]; 98 if (!sbusl) { 99 action = *(i + irq_action); 100 if (!action) 101 goto out_unlock; 102 } else { 103 for (j = 0; j < nsbi; j++) { 104 for (k = 0; k < 4; k++) 105 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action)) 106 goto found_it; 107 } 108 goto out_unlock; 109 } 110found_it: seq_printf(p, "%3d: ", i); 111#ifndef CONFIG_SMP 112 seq_printf(p, "%10u ", kstat_irqs(i)); 113#else 114 for_each_online_cpu(x) 115 seq_printf(p, "%10u ", 116 kstat_cpu(cpu_logical_map(x)).irqs[i]); 117#endif 118 seq_printf(p, "%c %s", 119 (action->flags & IRQF_DISABLED) ? '+' : ' ', 120 action->name); 121 action = action->next; 122 for (;;) { 123 for (; action; action = action->next) { 124 seq_printf(p, ",%s %s", 125 (action->flags & IRQF_DISABLED) ? " +" : "", 126 action->name); 127 } 128 if (!sbusl) break; 129 k++; 130 if (k < 4) 131 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action; 132 else { 133 j++; 134 if (j == nsbi) break; 135 k = 0; 136 action = sbus_actions [(j << 5) + (sbusl << 2)].action; 137 } 138 } 139 seq_putc(p, '\n'); 140 } 141out_unlock: 142 spin_unlock_irqrestore(&irq_action_lock, flags); 143 return 0; 144} 145 146void sun4d_free_irq(unsigned int irq, void *dev_id) 147{ 148 struct irqaction *action, **actionp; 149 struct irqaction *tmp = NULL; 150 unsigned long flags; 151 152 spin_lock_irqsave(&irq_action_lock, flags); 153 if (irq < 15) 154 actionp = irq + irq_action; 155 else 156 actionp = &(sbus_actions[irq - (1 << 5)].action); 157 action = *actionp; 158 if (!action) { 159 printk("Trying to free free IRQ%d\n",irq); 160 goto out_unlock; 161 } 162 if (dev_id) { 163 for (; action; action = action->next) { 164 if (action->dev_id == dev_id) 165 break; 166 tmp = action; 167 } 168 if (!action) { 169 printk("Trying to free free shared IRQ%d\n",irq); 170 goto out_unlock; 171 } 172 } else if (action->flags & IRQF_SHARED) { 173 printk("Trying to free shared IRQ%d with NULL device ID\n", irq); 174 goto out_unlock; 175 } 176 if (action->flags & SA_STATIC_ALLOC) 177 { 178 /* This interrupt is marked as specially allocated 179 * so it is a bad idea to free it. 180 */ 181 printk("Attempt to free statically allocated IRQ%d (%s)\n", 182 irq, action->name); 183 goto out_unlock; 184 } 185 186 if (tmp) 187 tmp->next = action->next; 188 else 189 *actionp = action->next; 190 191 spin_unlock_irqrestore(&irq_action_lock, flags); 192 193 synchronize_irq(irq); 194 195 spin_lock_irqsave(&irq_action_lock, flags); 196 197 kfree(action); 198 199 if (!(*actionp)) 200 __disable_irq(irq); 201 202out_unlock: 203 spin_unlock_irqrestore(&irq_action_lock, flags); 204} 205 206extern void unexpected_irq(int, void *, struct pt_regs *); 207 208void sun4d_handler_irq(int irq, struct pt_regs * regs) 209{ 210 struct pt_regs *old_regs; 211 struct irqaction * action; 212 int cpu = smp_processor_id(); 213 /* SBUS IRQ level (1 - 7) */ 214 int sbusl = pil_to_sbus[irq]; 215 216 cc_get_ipen(); 217 218 cc_set_iclr(1 << irq); 219 220 old_regs = set_irq_regs(regs); 221 irq_enter(); 222 kstat_cpu(cpu).irqs[irq]++; 223 if (!sbusl) { 224 action = *(irq + irq_action); 225 if (!action) 226 unexpected_irq(irq, NULL, regs); 227 do { 228 action->handler(irq, action->dev_id); 229 action = action->next; 230 } while (action); 231 } else { 232 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff; 233 int sbino; 234 struct sbus_action *actionp; 235 unsigned mask, slot; 236 int sbil = (sbusl << 2); 237 238 bw_clear_intr_mask(sbusl, bus_mask); 239 240 /* Loop for each pending SBI */ 241 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1) 242 if (bus_mask & 1) { 243 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil); 244 mask &= (0xf << sbil); 245 actionp = sbus_actions + (sbino << 5) + (sbil); 246 /* Loop for each pending SBI slot */ 247 for (slot = (1 << sbil); mask; slot <<= 1, actionp++) 248 if (mask & slot) { 249 mask &= ~slot; 250 action = actionp->action; 251 252 if (!action) 253 unexpected_irq(irq, NULL, regs); 254 do { 255 action->handler(irq, action->dev_id); 256 action = action->next; 257 } while (action); 258 release_sbi(SBI2DEVID(sbino), slot); 259 } 260 } 261 } 262 irq_exit(); 263 set_irq_regs(old_regs); 264} 265 266int sun4d_request_irq(unsigned int irq, 267 irq_handler_t handler, 268 unsigned long irqflags, const char * devname, void *dev_id) 269{ 270 struct irqaction *action, *tmp = NULL, **actionp; 271 unsigned long flags; 272 int ret; 273 274 if(irq > 14 && irq < (1 << 5)) { 275 ret = -EINVAL; 276 goto out; 277 } 278 279 if (!handler) { 280 ret = -EINVAL; 281 goto out; 282 } 283 284 spin_lock_irqsave(&irq_action_lock, flags); 285 286 if (irq >= (1 << 5)) 287 actionp = &(sbus_actions[irq - (1 << 5)].action); 288 else 289 actionp = irq + irq_action; 290 action = *actionp; 291 292 if (action) { 293 if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) { 294 for (tmp = action; tmp->next; tmp = tmp->next); 295 } else { 296 ret = -EBUSY; 297 goto out_unlock; 298 } 299 if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) { 300 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq); 301 ret = -EBUSY; 302 goto out_unlock; 303 } 304 action = NULL; /* Or else! */ 305 } 306 307 /* If this is flagged as statically allocated then we use our 308 * private struct which is never freed. 309 */ 310 if (irqflags & SA_STATIC_ALLOC) { 311 if (static_irq_count < MAX_STATIC_ALLOC) 312 action = &static_irqaction[static_irq_count++]; 313 else 314 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname); 315 } 316 317 if (action == NULL) 318 action = kmalloc(sizeof(struct irqaction), 319 GFP_ATOMIC); 320 321 if (!action) { 322 ret = -ENOMEM; 323 goto out_unlock; 324 } 325 326 action->handler = handler; 327 action->flags = irqflags; 328 action->name = devname; 329 action->next = NULL; 330 action->dev_id = dev_id; 331 332 if (tmp) 333 tmp->next = action; 334 else 335 *actionp = action; 336 337 __enable_irq(irq); 338 339 ret = 0; 340out_unlock: 341 spin_unlock_irqrestore(&irq_action_lock, flags); 342out: 343 return ret; 344} 345 346static void sun4d_disable_irq(unsigned int irq) 347{ 348 int tid = sbus_tid[(irq >> 5) - 1]; 349 unsigned long flags; 350 351 if (irq < NR_IRQS) 352 return; 353 354 spin_lock_irqsave(&sun4d_imsk_lock, flags); 355 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7])); 356 spin_unlock_irqrestore(&sun4d_imsk_lock, flags); 357} 358 359static void sun4d_enable_irq(unsigned int irq) 360{ 361 int tid = sbus_tid[(irq >> 5) - 1]; 362 unsigned long flags; 363 364 if (irq < NR_IRQS) 365 return; 366 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); 370} 371 372#ifdef CONFIG_SMP 373static void sun4d_set_cpu_int(int cpu, int level) 374{ 375 sun4d_send_ipi(cpu, level); 376} 377 378static void sun4d_clear_ipi(int cpu, int level) 379{ 380} 381 382static void sun4d_set_udt(int cpu) 383{ 384} 385 386/* Setup IRQ distribution scheme. */ 387void __init sun4d_distribute_irqs(void) 388{ 389 struct device_node *dp; 390 391#ifdef DISTRIBUTE_IRQS 392 cpumask_t sbus_serving_map; 393 394 sbus_serving_map = cpu_present_map; 395 for_each_node_by_name(dp, "sbi") { 396 int board = of_getintprop_default(dp, "board#", 0); 397 398 if ((board * 2) == boot_cpu_id && cpu_isset(board * 2 + 1, cpu_present_map)) 399 sbus_tid[board] = (board * 2 + 1); 400 else if (cpu_isset(board * 2, cpu_present_map)) 401 sbus_tid[board] = (board * 2); 402 else if (cpu_isset(board * 2 + 1, cpu_present_map)) 403 sbus_tid[board] = (board * 2 + 1); 404 else 405 sbus_tid[board] = 0xff; 406 if (sbus_tid[board] != 0xff) 407 cpu_clear(sbus_tid[board], sbus_serving_map); 408 } 409 for_each_node_by_name(dp, "sbi") { 410 int board = of_getintprop_default(dp, "board#", 0); 411 if (sbus_tid[board] == 0xff) { 412 int i = 31; 413 414 if (cpus_empty(sbus_serving_map)) 415 sbus_serving_map = cpu_present_map; 416 while (cpu_isset(i, sbus_serving_map)) 417 i--; 418 sbus_tid[board] = i; 419 cpu_clear(i, sbus_serving_map); 420 } 421 } 422 for_each_node_by_name(dp, "sbi") { 423 int devid = of_getintprop_default(dp, "device-id", 0); 424 int board = of_getintprop_default(dp, "board#", 0); 425 printk("sbus%d IRQs directed to CPU%d\n", board, sbus_tid[board]); 426 set_sbi_tid(devid, sbus_tid[board] << 3); 427 } 428#else 429 int cpuid = cpu_logical_map(1); 430 431 if (cpuid == -1) 432 cpuid = cpu_logical_map(0); 433 for_each_node_by_name(dp, "sbi") { 434 int devid = of_getintprop_default(dp, "device-id", 0); 435 int board = of_getintprop_default(dp, "board#", 0); 436 sbus_tid[board] = cpuid; 437 set_sbi_tid(devid, cpuid << 3); 438 } 439 printk("All sbus IRQs directed to CPU%d\n", cpuid); 440#endif 441} 442#endif 443 444static void sun4d_clear_clock_irq(void) 445{ 446 sbus_readl(&sun4d_timers->l10_timer_limit); 447} 448 449static void sun4d_load_profile_irq(int cpu, unsigned int limit) 450{ 451 bw_set_prof_limit(cpu, limit); 452} 453 454static void __init sun4d_load_profile_irqs(void) 455{ 456 int cpu = 0, mid; 457 458 while (!cpu_find_by_instance(cpu, NULL, &mid)) { 459 sun4d_load_profile_irq(mid >> 3, 0); 460 cpu++; 461 } 462} 463 464static void __init sun4d_fixup_trap_table(void) 465{ 466#ifdef CONFIG_SMP 467 unsigned long flags; 468 extern unsigned long lvl14_save[4]; 469 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)]; 470 extern unsigned int real_irq_entry[], smp4d_ticker[]; 471 extern unsigned int patchme_maybe_smp_msg[]; 472 473 /* Adjust so that we jump directly to smp4d_ticker */ 474 lvl14_save[2] += smp4d_ticker - real_irq_entry; 475 476 /* For SMP we use the level 14 ticker, however the bootup code 477 * has copied the firmware's level 14 vector into the boot cpu's 478 * trap table, we must fix this now or we get squashed. 479 */ 480 local_irq_save(flags); 481 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */ 482 trap_table->inst_one = lvl14_save[0]; 483 trap_table->inst_two = lvl14_save[1]; 484 trap_table->inst_three = lvl14_save[2]; 485 trap_table->inst_four = lvl14_save[3]; 486 local_flush_cache_all(); 487 local_irq_restore(flags); 488#endif 489} 490 491static void __init sun4d_init_timers(irq_handler_t counter_fn) 492{ 493 struct device_node *dp; 494 struct resource res; 495 const u32 *reg; 496 int err; 497 498 dp = of_find_node_by_name(NULL, "cpu-unit"); 499 if (!dp) { 500 prom_printf("sun4d_init_timers: Unable to find cpu-unit\n"); 501 prom_halt(); 502 } 503 504 /* Which cpu-unit we use is arbitrary, we can view the bootbus timer 505 * registers via any cpu's mapping. The first 'reg' property is the 506 * bootbus. 507 */ 508 reg = of_get_property(dp, "reg", NULL); 509 of_node_put(dp); 510 if (!reg) { 511 prom_printf("sun4d_init_timers: No reg property\n"); 512 prom_halt(); 513 } 514 515 res.start = reg[1]; 516 res.end = reg[2] - 1; 517 res.flags = reg[0] & 0xff; 518 sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT, 519 sizeof(struct sun4d_timer_regs), "user timer"); 520 if (!sun4d_timers) { 521 prom_printf("sun4d_init_timers: Can't map timer regs\n"); 522 prom_halt(); 523 } 524 525 sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit); 526 527 master_l10_counter = &sun4d_timers->l10_cur_count; 528 529 err = request_irq(TIMER_IRQ, counter_fn, 530 (IRQF_DISABLED | SA_STATIC_ALLOC), 531 "timer", NULL); 532 if (err) { 533 prom_printf("sun4d_init_timers: request_irq() failed with %d\n", err); 534 prom_halt(); 535 } 536 sun4d_load_profile_irqs(); 537 sun4d_fixup_trap_table(); 538} 539 540void __init sun4d_init_sbi_irq(void) 541{ 542 struct device_node *dp; 543 int target_cpu = 0; 544 545#ifdef CONFIG_SMP 546 target_cpu = boot_cpu_id; 547#endif 548 549 nsbi = 0; 550 for_each_node_by_name(dp, "sbi") 551 nsbi++; 552 sbus_actions = kzalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC); 553 if (!sbus_actions) { 554 prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n"); 555 prom_halt(); 556 } 557 for_each_node_by_name(dp, "sbi") { 558 int devid = of_getintprop_default(dp, "device-id", 0); 559 int board = of_getintprop_default(dp, "board#", 0); 560 unsigned int mask; 561 562 set_sbi_tid(devid, target_cpu << 3); 563 sbus_tid[board] = target_cpu; 564 565 /* Get rid of pending irqs from PROM */ 566 mask = acquire_sbi(devid, 0xffffffff); 567 if (mask) { 568 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, board); 569 release_sbi(devid, mask); 570 } 571 } 572} 573 574void __init sun4d_init_IRQ(void) 575{ 576 local_irq_disable(); 577 578 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM); 579 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM); 580 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM); 581 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM); 582 sparc_init_timers = sun4d_init_timers; 583#ifdef CONFIG_SMP 584 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM); 585 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP); 586 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP); 587#endif 588 /* Cannot enable interrupts until OBP ticker is disabled. */ 589} 590