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