1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/systm.h> 30 #include <sys/archsystm.h> 31 #include <sys/t_lock.h> 32 #include <sys/uadmin.h> 33 #include <sys/panic.h> 34 #include <sys/reboot.h> 35 #include <sys/autoconf.h> 36 #include <sys/machsystm.h> 37 #include <sys/promif.h> 38 #include <sys/membar.h> 39 #include <vm/hat_sfmmu.h> 40 #include <sys/cpu_module.h> 41 #include <sys/cpu_sgnblk_defs.h> 42 #include <sys/intreg.h> 43 #include <sys/consdev.h> 44 #include <sys/kdi_impl.h> 45 #include <sys/traptrace.h> 46 #include <sys/hypervisor_api.h> 47 #include <sys/vmsystm.h> 48 #include <sys/dtrace.h> 49 #include <sys/xc_impl.h> 50 #include <sys/callb.h> 51 #include <sys/mdesc.h> 52 #include <sys/mach_descrip.h> 53 #include <sys/wdt.h> 54 #include <sys/soft_state.h> 55 #include <sys/promimpl.h> 56 #include <sys/hsvc.h> 57 58 /* 59 * hvdump_buf_va is a pointer to the currently-configured hvdump_buf. 60 * A value of NULL indicates that this area is not configured. 61 * hvdump_buf_sz is tunable but will be clamped to HVDUMP_SIZE_MAX. 62 */ 63 64 caddr_t hvdump_buf_va; 65 uint64_t hvdump_buf_sz = HVDUMP_SIZE_DEFAULT; 66 static uint64_t hvdump_buf_pa; 67 68 u_longlong_t panic_tick; 69 70 extern u_longlong_t gettick(); 71 static void reboot_machine(char *); 72 static void update_hvdump_buffer(void); 73 74 /* 75 * For xt_sync synchronization. 76 */ 77 extern uint64_t xc_tick_limit; 78 extern uint64_t xc_tick_jump_limit; 79 80 /* 81 * We keep our own copies, used for cache flushing, because we can be called 82 * before cpu_fiximpl(). 83 */ 84 static int kdi_dcache_size; 85 static int kdi_dcache_linesize; 86 static int kdi_icache_size; 87 static int kdi_icache_linesize; 88 89 /* 90 * Assembly support for generic modules in sun4v/ml/mach_xc.s 91 */ 92 extern void init_mondo_nocheck(xcfunc_t *func, uint64_t arg1, uint64_t arg2); 93 extern void kdi_flush_idcache(int, int, int, int); 94 extern uint64_t get_cpuaddr(uint64_t, uint64_t); 95 96 /* 97 * Machine dependent code to reboot. 98 * "mdep" is interpreted as a character pointer; if non-null, it is a pointer 99 * to a string to be used as the argument string when rebooting. 100 * 101 * "invoke_cb" is a boolean. It is set to true when mdboot() can safely 102 * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when 103 * we are in a normal shutdown sequence (interrupts are not blocked, the 104 * system is not panic'ing or being suspended). 105 */ 106 /*ARGSUSED*/ 107 void 108 mdboot(int cmd, int fcn, char *bootstr, boolean_t invoke_cb) 109 { 110 extern void pm_cfb_check_and_powerup(void); 111 112 /* 113 * XXX - rconsvp is set to NULL to ensure that output messages 114 * are sent to the underlying "hardware" device using the 115 * monitor's printf routine since we are in the process of 116 * either rebooting or halting the machine. 117 */ 118 rconsvp = NULL; 119 120 /* 121 * At a high interrupt level we can't: 122 * 1) bring up the console 123 * or 124 * 2) wait for pending interrupts prior to redistribution 125 * to the current CPU 126 * 127 * so we do them now. 128 */ 129 pm_cfb_check_and_powerup(); 130 131 /* make sure there are no more changes to the device tree */ 132 devtree_freeze(); 133 134 if (invoke_cb) 135 (void) callb_execute_class(CB_CL_MDBOOT, NULL); 136 137 /* 138 * Clear any unresolved UEs from memory. 139 */ 140 page_retire_mdboot(); 141 142 /* 143 * stop other cpus which also raise our priority. since there is only 144 * one active cpu after this, and our priority will be too high 145 * for us to be preempted, we're essentially single threaded 146 * from here on out. 147 */ 148 stop_other_cpus(); 149 150 /* 151 * try and reset leaf devices. reset_leaves() should only 152 * be called when there are no other threads that could be 153 * accessing devices 154 */ 155 reset_leaves(); 156 157 watchdog_clear(); 158 159 if (fcn == AD_HALT) { 160 mach_set_soft_state(SIS_TRANSITION, 161 &SOLARIS_SOFT_STATE_HALT_MSG); 162 halt((char *)NULL); 163 } else if (fcn == AD_POWEROFF) { 164 mach_set_soft_state(SIS_TRANSITION, 165 &SOLARIS_SOFT_STATE_POWER_MSG); 166 power_down(NULL); 167 } else { 168 if (bootstr == NULL) { 169 switch (fcn) { 170 171 case AD_BOOT: 172 bootstr = ""; 173 break; 174 175 case AD_IBOOT: 176 bootstr = "-a"; 177 break; 178 179 case AD_SBOOT: 180 bootstr = "-s"; 181 break; 182 183 case AD_SIBOOT: 184 bootstr = "-sa"; 185 break; 186 default: 187 cmn_err(CE_WARN, 188 "mdboot: invalid function %d", fcn); 189 bootstr = ""; 190 break; 191 } 192 } 193 mach_set_soft_state(SIS_TRANSITION, 194 &SOLARIS_SOFT_STATE_REBOOT_MSG); 195 reboot_machine(bootstr); 196 } 197 /* MAYBE REACHED */ 198 } 199 200 /* mdpreboot - may be called prior to mdboot while root fs still mounted */ 201 /*ARGSUSED*/ 202 void 203 mdpreboot(int cmd, int fcn, char *bootstr) 204 { 205 } 206 207 /* 208 * Halt the machine and then reboot with the device 209 * and arguments specified in bootstr. 210 */ 211 static void 212 reboot_machine(char *bootstr) 213 { 214 flush_windows(); 215 stop_other_cpus(); /* send stop signal to other CPUs */ 216 prom_printf("rebooting...\n"); 217 /* 218 * For platforms that use CPU signatures, we 219 * need to set the signature block to OS and 220 * the state to exiting for all the processors. 221 */ 222 CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_REBOOT, -1); 223 prom_reboot(bootstr); 224 /*NOTREACHED*/ 225 } 226 227 /* 228 * We use the x-trap mechanism and idle_stop_xcall() to stop the other CPUs. 229 * Once in panic_idle() they raise spl, record their location, and spin. 230 */ 231 static void 232 panic_idle(void) 233 { 234 (void) spl7(); 235 236 debug_flush_windows(); 237 (void) setjmp(&curthread->t_pcb); 238 239 CPU->cpu_m.in_prom = 1; 240 membar_stld(); 241 242 for (;;); 243 } 244 245 /* 246 * Force the other CPUs to trap into panic_idle(), and then remove them 247 * from the cpu_ready_set so they will no longer receive cross-calls. 248 */ 249 /*ARGSUSED*/ 250 void 251 panic_stopcpus(cpu_t *cp, kthread_t *t, int spl) 252 { 253 cpuset_t cps; 254 int i; 255 256 (void) splzs(); 257 CPUSET_ALL_BUT(cps, cp->cpu_id); 258 xt_some(cps, (xcfunc_t *)idle_stop_xcall, (uint64_t)&panic_idle, NULL); 259 260 for (i = 0; i < NCPU; i++) { 261 if (i != cp->cpu_id && CPU_XCALL_READY(i)) { 262 int ntries = 0x10000; 263 264 while (!cpu[i]->cpu_m.in_prom && ntries) { 265 DELAY(50); 266 ntries--; 267 } 268 269 if (!cpu[i]->cpu_m.in_prom) 270 printf("panic: failed to stop cpu%d\n", i); 271 272 cpu[i]->cpu_flags &= ~CPU_READY; 273 cpu[i]->cpu_flags |= CPU_QUIESCED; 274 CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id); 275 } 276 } 277 } 278 279 /* 280 * Platform callback following each entry to panicsys(). If we've panicked at 281 * level 14, we examine t_panic_trap to see if a fatal trap occurred. If so, 282 * we disable further %tick_cmpr interrupts. If not, an explicit call to panic 283 * was made and so we re-enqueue an interrupt request structure to allow 284 * further level 14 interrupts to be processed once we lower PIL. This allows 285 * us to handle panics from the deadman() CY_HIGH_LEVEL cyclic. 286 */ 287 void 288 panic_enter_hw(int spl) 289 { 290 if (!panic_tick) { 291 panic_tick = gettick(); 292 if (mach_htraptrace_enable) { 293 uint64_t prev_freeze; 294 295 /* there are no possible error codes for this hcall */ 296 (void) hv_ttrace_freeze((uint64_t)TRAP_TFREEZE_ALL, 297 &prev_freeze); 298 } 299 #ifdef TRAPTRACE 300 TRAPTRACE_FREEZE; 301 #endif 302 } 303 304 mach_set_soft_state(SIS_TRANSITION, &SOLARIS_SOFT_STATE_PANIC_MSG); 305 306 if (spl == ipltospl(PIL_14)) { 307 uint_t opstate = disable_vec_intr(); 308 309 if (curthread->t_panic_trap != NULL) { 310 tickcmpr_disable(); 311 intr_dequeue_req(PIL_14, cbe_level14_inum); 312 } else { 313 if (!tickcmpr_disabled()) 314 intr_enqueue_req(PIL_14, cbe_level14_inum); 315 /* 316 * Clear SOFTINT<14>, SOFTINT<0> (TICK_INT) 317 * and SOFTINT<16> (STICK_INT) to indicate 318 * that the current level 14 has been serviced. 319 */ 320 wr_clr_softint((1 << PIL_14) | 321 TICK_INT_MASK | STICK_INT_MASK); 322 } 323 324 enable_vec_intr(opstate); 325 } 326 } 327 328 /* 329 * Miscellaneous hardware-specific code to execute after panicstr is set 330 * by the panic code: we also print and record PTL1 panic information here. 331 */ 332 /*ARGSUSED*/ 333 void 334 panic_quiesce_hw(panic_data_t *pdp) 335 { 336 extern uint_t getpstate(void); 337 extern void setpstate(uint_t); 338 339 /* 340 * Turn off TRAPTRACE and save the current %tick value in panic_tick. 341 */ 342 if (!panic_tick) { 343 panic_tick = gettick(); 344 if (mach_htraptrace_enable) { 345 uint64_t prev_freeze; 346 347 /* there are no possible error codes for this hcall */ 348 (void) hv_ttrace_freeze((uint64_t)TRAP_TFREEZE_ALL, 349 &prev_freeze); 350 } 351 #ifdef TRAPTRACE 352 TRAPTRACE_FREEZE; 353 #endif 354 } 355 /* 356 * For Platforms that use CPU signatures, we 357 * need to set the signature block to OS, the state to 358 * exiting, and the substate to panic for all the processors. 359 */ 360 CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_PANIC, -1); 361 362 update_hvdump_buffer(); 363 364 /* 365 * Disable further ECC errors from the bus nexus. 366 */ 367 (void) bus_func_invoke(BF_TYPE_ERRDIS); 368 369 /* 370 * Redirect all interrupts to the current CPU. 371 */ 372 intr_redist_all_cpus_shutdown(); 373 374 /* 375 * This call exists solely to support dumps to network 376 * devices after sync from OBP. 377 * 378 * If we came here via the sync callback, then on some 379 * platforms, interrupts may have arrived while we were 380 * stopped in OBP. OBP will arrange for those interrupts to 381 * be redelivered if you say "go", but not if you invoke a 382 * client callback like 'sync'. For some dump devices 383 * (network swap devices), we need interrupts to be 384 * delivered in order to dump, so we have to call the bus 385 * nexus driver to reset the interrupt state machines. 386 */ 387 (void) bus_func_invoke(BF_TYPE_RESINTR); 388 389 setpstate(getpstate() | PSTATE_IE); 390 } 391 392 /* 393 * Platforms that use CPU signatures need to set the signature block to OS and 394 * the state to exiting for all CPUs. PANIC_CONT indicates that we're about to 395 * write the crash dump, which tells the SSP/SMS to begin a timeout routine to 396 * reboot the machine if the dump never completes. 397 */ 398 /*ARGSUSED*/ 399 void 400 panic_dump_hw(int spl) 401 { 402 CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1); 403 } 404 405 /* 406 * for ptl1_panic 407 */ 408 void 409 ptl1_init_cpu(struct cpu *cpu) 410 { 411 ptl1_state_t *pstate = &cpu->cpu_m.ptl1_state; 412 413 /*CONSTCOND*/ 414 if (sizeof (struct cpu) + PTL1_SSIZE > CPU_ALLOC_SIZE) { 415 panic("ptl1_init_cpu: not enough space left for ptl1_panic " 416 "stack, sizeof (struct cpu) = %lu", 417 (unsigned long)sizeof (struct cpu)); 418 } 419 420 pstate->ptl1_stktop = (uintptr_t)cpu + CPU_ALLOC_SIZE; 421 cpu_pa[cpu->cpu_id] = va_to_pa(cpu); 422 } 423 424 void 425 ptl1_panic_handler(ptl1_state_t *pstate) 426 { 427 static const char *ptl1_reasons[] = { 428 #ifdef PTL1_PANIC_DEBUG 429 "trap for debug purpose", /* PTL1_BAD_DEBUG */ 430 #else 431 "unknown trap", /* PTL1_BAD_DEBUG */ 432 #endif 433 "register window trap", /* PTL1_BAD_WTRAP */ 434 "kernel MMU miss", /* PTL1_BAD_KMISS */ 435 "kernel protection fault", /* PTL1_BAD_KPROT_FAULT */ 436 "ISM MMU miss", /* PTL1_BAD_ISM */ 437 "kernel MMU trap", /* PTL1_BAD_MMUTRAP */ 438 "kernel trap handler state", /* PTL1_BAD_TRAP */ 439 "floating point trap", /* PTL1_BAD_FPTRAP */ 440 #ifdef DEBUG 441 "pointer to intr_vec", /* PTL1_BAD_INTR_VEC */ 442 #else 443 "unknown trap", /* PTL1_BAD_INTR_VEC */ 444 #endif 445 #ifdef TRAPTRACE 446 "TRACE_PTR state", /* PTL1_BAD_TRACE_PTR */ 447 #else 448 "unknown trap", /* PTL1_BAD_TRACE_PTR */ 449 #endif 450 "stack overflow", /* PTL1_BAD_STACK */ 451 "DTrace flags", /* PTL1_BAD_DTRACE_FLAGS */ 452 "attempt to steal locked ctx", /* PTL1_BAD_CTX_STEAL */ 453 "CPU ECC error loop", /* PTL1_BAD_ECC */ 454 "unexpected error from hypervisor call", /* PTL1_BAD_HCALL */ 455 "unexpected global level(%gl)", /* PTL1_BAD_GL */ 456 "Watchdog Reset", /* PTL1_BAD_WATCHDOG */ 457 "unexpected RED mode trap", /* PTL1_BAD_RED */ 458 "return value EINVAL from hcall: "\ 459 "UNMAP_PERM_ADDR", /* PTL1_BAD_HCALL_UNMAP_PERM_EINVAL */ 460 "return value ENOMAP from hcall: "\ 461 "UNMAP_PERM_ADDR", /* PTL1_BAD_HCALL_UNMAP_PERM_ENOMAP */ 462 }; 463 464 uint_t reason = pstate->ptl1_regs.ptl1_gregs[0].ptl1_g1; 465 uint_t tl = pstate->ptl1_regs.ptl1_trap_regs[0].ptl1_tl; 466 struct trap_info ti = { 0 }; 467 468 /* 469 * Use trap_info for a place holder to call panic_savetrap() and 470 * panic_showtrap() to save and print out ptl1_panic information. 471 */ 472 if (curthread->t_panic_trap == NULL) 473 curthread->t_panic_trap = &ti; 474 475 if (reason < sizeof (ptl1_reasons) / sizeof (ptl1_reasons[0])) 476 panic("bad %s at TL %u", ptl1_reasons[reason], tl); 477 else 478 panic("ptl1_panic reason 0x%x at TL %u", reason, tl); 479 } 480 481 void 482 clear_watchdog_on_exit(void) 483 { 484 prom_printf("Debugging requested; hardware watchdog suspended.\n"); 485 (void) watchdog_suspend(); 486 } 487 488 /* 489 * Restore the watchdog timer when returning from a debugger 490 * after a panic or L1-A and resume watchdog pat. 491 */ 492 void 493 restore_watchdog_on_entry() 494 { 495 watchdog_resume(); 496 } 497 498 int 499 kdi_watchdog_disable(void) 500 { 501 watchdog_suspend(); 502 503 return (0); 504 } 505 506 void 507 kdi_watchdog_restore(void) 508 { 509 watchdog_resume(); 510 } 511 512 void 513 mach_dump_buffer_init(void) 514 { 515 uint64_t ret, minsize = 0; 516 517 if (hvdump_buf_sz > HVDUMP_SIZE_MAX) 518 hvdump_buf_sz = HVDUMP_SIZE_MAX; 519 520 hvdump_buf_va = contig_mem_alloc_align(hvdump_buf_sz, PAGESIZE); 521 if (hvdump_buf_va == NULL) 522 return; 523 524 hvdump_buf_pa = va_to_pa(hvdump_buf_va); 525 526 ret = hv_dump_buf_update(hvdump_buf_pa, hvdump_buf_sz, 527 &minsize); 528 529 if (ret != H_EOK) { 530 contig_mem_free(hvdump_buf_va, hvdump_buf_sz); 531 hvdump_buf_va = NULL; 532 cmn_err(CE_NOTE, "!Error in setting up hvstate" 533 "dump buffer. Error = 0x%lx, size = 0x%lx," 534 "buf_pa = 0x%lx", ret, hvdump_buf_sz, 535 hvdump_buf_pa); 536 537 if (ret == H_EINVAL) { 538 cmn_err(CE_NOTE, "!Buffer size too small." 539 "Available buffer size = 0x%lx," 540 "Minimum buffer size required = 0x%lx", 541 hvdump_buf_sz, minsize); 542 } 543 } 544 } 545 546 547 static void 548 update_hvdump_buffer(void) 549 { 550 uint64_t ret, dummy_val; 551 552 if (hvdump_buf_va == NULL) 553 return; 554 555 ret = hv_dump_buf_update(hvdump_buf_pa, hvdump_buf_sz, 556 &dummy_val); 557 if (ret != H_EOK) { 558 cmn_err(CE_NOTE, "!Cannot update hvstate dump" 559 "buffer. Error = 0x%lx", ret); 560 } 561 } 562 563 564 static int 565 getintprop(pnode_t node, char *name, int deflt) 566 { 567 int value; 568 569 switch (prom_getproplen(node, name)) { 570 case 0: 571 value = 1; /* boolean properties */ 572 break; 573 574 case sizeof (int): 575 (void) prom_getprop(node, name, (caddr_t)&value); 576 break; 577 578 default: 579 value = deflt; 580 break; 581 } 582 583 return (value); 584 } 585 586 /* 587 * Called by setcpudelay 588 */ 589 void 590 cpu_init_tick_freq(void) 591 { 592 md_t *mdp; 593 mde_cookie_t rootnode; 594 int listsz; 595 mde_cookie_t *listp = NULL; 596 int num_nodes; 597 uint64_t stick_prop; 598 599 if (broken_md_flag) { 600 sys_tick_freq = cpunodes[CPU->cpu_id].clock_freq; 601 return; 602 } 603 604 if ((mdp = md_get_handle()) == NULL) 605 panic("stick_frequency property not found in MD"); 606 607 rootnode = md_root_node(mdp); 608 ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE); 609 610 num_nodes = md_node_count(mdp); 611 612 ASSERT(num_nodes > 0); 613 listsz = num_nodes * sizeof (mde_cookie_t); 614 listp = (mde_cookie_t *)prom_alloc((caddr_t)0, listsz, 0); 615 616 if (listp == NULL) 617 panic("cannot allocate list for MD properties"); 618 619 num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "platform"), 620 md_find_name(mdp, "fwd"), listp); 621 622 ASSERT(num_nodes == 1); 623 624 if (md_get_prop_val(mdp, *listp, "stick-frequency", &stick_prop) != 0) 625 panic("stick_frequency property not found in MD"); 626 627 sys_tick_freq = stick_prop; 628 629 prom_free((caddr_t)listp, listsz); 630 (void) md_fini_handle(mdp); 631 } 632 633 int shipit(int n, uint64_t cpu_list_ra); 634 extern uint64_t xc_tick_limit; 635 extern uint64_t xc_tick_jump_limit; 636 637 #ifdef DEBUG 638 #define SEND_MONDO_STATS 1 639 #endif 640 641 #ifdef SEND_MONDO_STATS 642 uint32_t x_one_stimes[64]; 643 uint32_t x_one_ltimes[16]; 644 uint32_t x_set_stimes[64]; 645 uint32_t x_set_ltimes[16]; 646 uint32_t x_set_cpus[NCPU]; 647 #endif 648 649 void 650 send_one_mondo(int cpuid) 651 { 652 int retries, stat; 653 uint64_t starttick, endtick, tick, lasttick; 654 struct machcpu *mcpup = &(CPU->cpu_m); 655 656 CPU_STATS_ADDQ(CPU, sys, xcalls, 1); 657 starttick = lasttick = gettick(); 658 mcpup->cpu_list[0] = (uint16_t)cpuid; 659 stat = shipit(1, mcpup->cpu_list_ra); 660 endtick = starttick + xc_tick_limit; 661 retries = 0; 662 while (stat != H_EOK) { 663 if (stat != H_EWOULDBLOCK) { 664 if (panic_quiesce) 665 return; 666 if (stat == H_ECPUERROR) 667 cmn_err(CE_PANIC, "send_one_mondo: " 668 "cpuid: 0x%x has been marked in " 669 "error", cpuid); 670 else 671 cmn_err(CE_PANIC, "send_one_mondo: " 672 "unexpected hypervisor error 0x%x " 673 "while sending a mondo to cpuid: " 674 "0x%x", stat, cpuid); 675 } 676 tick = gettick(); 677 /* 678 * If there is a big jump between the current tick 679 * count and lasttick, we have probably hit a break 680 * point. Adjust endtick accordingly to avoid panic. 681 */ 682 if (tick > (lasttick + xc_tick_jump_limit)) 683 endtick += (tick - lasttick); 684 lasttick = tick; 685 if (tick > endtick) { 686 if (panic_quiesce) 687 return; 688 cmn_err(CE_PANIC, "send mondo timeout " 689 "(target 0x%x) [retries: 0x%x hvstat: 0x%x]", 690 cpuid, retries, stat); 691 } 692 drv_usecwait(1); 693 stat = shipit(1, mcpup->cpu_list_ra); 694 retries++; 695 } 696 #ifdef SEND_MONDO_STATS 697 { 698 uint64_t n = gettick() - starttick; 699 if (n < 8192) 700 x_one_stimes[n >> 7]++; 701 else if (n < 15*8192) 702 x_one_ltimes[n >> 13]++; 703 else 704 x_one_ltimes[0xf]++; 705 } 706 #endif 707 } 708 709 void 710 send_mondo_set(cpuset_t set) 711 { 712 uint64_t starttick, endtick, tick, lasttick; 713 uint_t largestid, smallestid; 714 int i, j; 715 int ncpuids = 0; 716 int shipped = 0; 717 int retries = 0; 718 struct machcpu *mcpup = &(CPU->cpu_m); 719 720 ASSERT(!CPUSET_ISNULL(set)); 721 CPUSET_BOUNDS(set, smallestid, largestid); 722 if (smallestid == CPUSET_NOTINSET) { 723 return; 724 } 725 726 starttick = lasttick = gettick(); 727 endtick = starttick + xc_tick_limit; 728 729 /* 730 * Assemble CPU list for HV argument. We already know 731 * smallestid and largestid are members of set. 732 */ 733 mcpup->cpu_list[ncpuids++] = (uint16_t)smallestid; 734 if (largestid != smallestid) { 735 for (i = smallestid+1; i <= largestid-1; i++) { 736 if (CPU_IN_SET(set, i)) { 737 mcpup->cpu_list[ncpuids++] = (uint16_t)i; 738 } 739 } 740 mcpup->cpu_list[ncpuids++] = (uint16_t)largestid; 741 } 742 743 do { 744 int stat; 745 746 stat = shipit(ncpuids, mcpup->cpu_list_ra); 747 if (stat == H_EOK) { 748 shipped += ncpuids; 749 break; 750 } 751 752 /* 753 * Either not all CPU mondos were sent, or an 754 * error occurred. CPUs that were sent mondos 755 * have their CPU IDs overwritten in cpu_list. 756 * Reset cpu_list so that it only holds those 757 * CPU IDs that still need to be sent. 758 */ 759 for (i = 0, j = 0; i < ncpuids; i++) { 760 if (mcpup->cpu_list[i] == HV_SEND_MONDO_ENTRYDONE) { 761 shipped++; 762 } else { 763 mcpup->cpu_list[j++] = mcpup->cpu_list[i]; 764 } 765 } 766 ncpuids = j; 767 768 /* 769 * Now handle possible errors returned 770 * from hypervisor. 771 */ 772 if (stat == H_ECPUERROR) { 773 int errorcpus; 774 775 if (!panic_quiesce) 776 cmn_err(CE_CONT, "send_mondo_set: cpuid(s) "); 777 778 /* 779 * Remove any CPUs in the error state from 780 * cpu_list. At this point cpu_list only 781 * contains the CPU IDs for mondos not 782 * succesfully sent. 783 */ 784 for (i = 0, errorcpus = 0; i < ncpuids; i++) { 785 uint64_t state = CPU_STATE_INVALID; 786 uint16_t id = mcpup->cpu_list[i]; 787 788 (void) hv_cpu_state(id, &state); 789 if (state == CPU_STATE_ERROR) { 790 if (!panic_quiesce) 791 cmn_err(CE_CONT, "0x%x ", id); 792 errorcpus++; 793 } else if (errorcpus > 0) { 794 mcpup->cpu_list[i - errorcpus] = 795 mcpup->cpu_list[i]; 796 } 797 } 798 ncpuids -= errorcpus; 799 800 if (!panic_quiesce) { 801 if (errorcpus == 0) { 802 cmn_err(CE_CONT, "<none> have been " 803 "marked in error\n"); 804 cmn_err(CE_PANIC, "send_mondo_set: " 805 "hypervisor returned " 806 "H_ECPUERROR but no CPU in " 807 "cpu_list in error state"); 808 } else { 809 cmn_err(CE_CONT, "have been marked in " 810 "error\n"); 811 cmn_err(CE_PANIC, "send_mondo_set: " 812 "CPU(s) in error state"); 813 } 814 } 815 } else if (stat != H_EWOULDBLOCK) { 816 if (panic_quiesce) 817 return; 818 /* 819 * For all other errors, panic. 820 */ 821 cmn_err(CE_CONT, "send_mondo_set: unexpected " 822 "hypervisor error 0x%x while sending a " 823 "mondo to cpuid(s):", stat); 824 for (i = 0; i < ncpuids; i++) { 825 cmn_err(CE_CONT, " 0x%x", mcpup->cpu_list[i]); 826 } 827 cmn_err(CE_CONT, "\n"); 828 cmn_err(CE_PANIC, "send_mondo_set: unexpected " 829 "hypervisor error"); 830 } 831 832 tick = gettick(); 833 /* 834 * If there is a big jump between the current tick 835 * count and lasttick, we have probably hit a break 836 * point. Adjust endtick accordingly to avoid panic. 837 */ 838 if (tick > (lasttick + xc_tick_jump_limit)) 839 endtick += (tick - lasttick); 840 lasttick = tick; 841 if (tick > endtick) { 842 if (panic_quiesce) 843 return; 844 cmn_err(CE_CONT, "send mondo timeout " 845 "[retries: 0x%x] cpuids: ", retries); 846 for (i = 0; i < ncpuids; i++) 847 cmn_err(CE_CONT, " 0x%x", mcpup->cpu_list[i]); 848 cmn_err(CE_CONT, "\n"); 849 cmn_err(CE_PANIC, "send_mondo_set: timeout"); 850 } 851 852 while (gettick() < (tick + sys_clock_mhz)) 853 ; 854 retries++; 855 } while (ncpuids > 0); 856 857 CPU_STATS_ADDQ(CPU, sys, xcalls, shipped); 858 859 #ifdef SEND_MONDO_STATS 860 { 861 uint64_t n = gettick() - starttick; 862 if (n < 8192) 863 x_set_stimes[n >> 7]++; 864 else if (n < 15*8192) 865 x_set_ltimes[n >> 13]++; 866 else 867 x_set_ltimes[0xf]++; 868 } 869 x_set_cpus[shipped]++; 870 #endif 871 } 872 873 void 874 syncfpu(void) 875 { 876 } 877 878 void 879 cpu_flush_ecache(void) 880 { 881 } 882 883 void 884 sticksync_slave(void) 885 {} 886 887 void 888 sticksync_master(void) 889 {} 890 891 void 892 cpu_init_cache_scrub(void) 893 { 894 mach_set_soft_state(SIS_NORMAL, &SOLARIS_SOFT_STATE_RUN_MSG); 895 } 896 897 int 898 dtrace_blksuword32_err(uintptr_t addr, uint32_t *data) 899 { 900 int ret, watched; 901 902 watched = watch_disable_addr((void *)addr, 4, S_WRITE); 903 ret = dtrace_blksuword32(addr, data, 0); 904 if (watched) 905 watch_enable_addr((void *)addr, 4, S_WRITE); 906 907 return (ret); 908 } 909 910 int 911 dtrace_blksuword32(uintptr_t addr, uint32_t *data, int tryagain) 912 { 913 if (suword32((void *)addr, *data) == -1) 914 return (tryagain ? dtrace_blksuword32_err(addr, data) : -1); 915 dtrace_flush_sec(addr); 916 917 return (0); 918 } 919 920 /*ARGSUSED*/ 921 void 922 cpu_faulted_enter(struct cpu *cp) 923 { 924 } 925 926 /*ARGSUSED*/ 927 void 928 cpu_faulted_exit(struct cpu *cp) 929 { 930 } 931 932 static int 933 kdi_cpu_ready_iter(int (*cb)(int, void *), void *arg) 934 { 935 int rc, i; 936 937 for (rc = 0, i = 0; i < NCPU; i++) { 938 if (CPU_IN_SET(cpu_ready_set, i)) 939 rc += cb(i, arg); 940 } 941 942 return (rc); 943 } 944 945 /* 946 * Sends a cross-call to a specified processor. The caller assumes 947 * responsibility for repetition of cross-calls, as appropriate (MARSA for 948 * debugging). 949 */ 950 static int 951 kdi_xc_one(int cpuid, void (*func)(uintptr_t, uintptr_t), uintptr_t arg1, 952 uintptr_t arg2) 953 { 954 int stat; 955 struct machcpu *mcpup; 956 uint64_t cpuaddr_reg = 0, cpuaddr_scr = 0; 957 958 mcpup = &(((cpu_t *)get_cpuaddr(cpuaddr_reg, cpuaddr_scr))->cpu_m); 959 960 /* 961 * if (idsr_busy()) 962 * return (KDI_XC_RES_ERR); 963 */ 964 965 init_mondo_nocheck((xcfunc_t *)func, arg1, arg2); 966 967 mcpup->cpu_list[0] = (uint16_t)cpuid; 968 stat = shipit(1, mcpup->cpu_list_ra); 969 970 if (stat == 0) 971 return (KDI_XC_RES_OK); 972 else 973 return (KDI_XC_RES_NACK); 974 } 975 976 static void 977 kdi_tickwait(clock_t nticks) 978 { 979 clock_t endtick = gettick() + nticks; 980 981 while (gettick() < endtick); 982 } 983 984 static void 985 kdi_cpu_init(int dcache_size, int dcache_linesize, int icache_size, 986 int icache_linesize) 987 { 988 kdi_dcache_size = dcache_size; 989 kdi_dcache_linesize = dcache_linesize; 990 kdi_icache_size = icache_size; 991 kdi_icache_linesize = icache_linesize; 992 } 993 994 /* used directly by kdi_read/write_phys */ 995 void 996 kdi_flush_caches(void) 997 { 998 /* Not required on sun4v architecture. */ 999 } 1000 1001 /*ARGSUSED*/ 1002 int 1003 kdi_get_stick(uint64_t *stickp) 1004 { 1005 return (-1); 1006 } 1007 1008 void 1009 cpu_kdi_init(kdi_t *kdi) 1010 { 1011 kdi->kdi_flush_caches = kdi_flush_caches; 1012 kdi->mkdi_cpu_init = kdi_cpu_init; 1013 kdi->mkdi_cpu_ready_iter = kdi_cpu_ready_iter; 1014 kdi->mkdi_xc_one = kdi_xc_one; 1015 kdi->mkdi_tickwait = kdi_tickwait; 1016 kdi->mkdi_get_stick = kdi_get_stick; 1017 } 1018 1019 uint64_t soft_state_message_ra[SOLARIS_SOFT_STATE_MSG_CNT]; 1020 static uint64_t soft_state_saved_state = (uint64_t)-1; 1021 static int soft_state_initialized = 0; 1022 static uint64_t soft_state_sup_minor; /* Supported minor number */ 1023 static hsvc_info_t soft_state_hsvc = { 1024 HSVC_REV_1, NULL, HSVC_GROUP_SOFT_STATE, 1, 0, NULL }; 1025 1026 1027 static void 1028 sun4v_system_claim(void) 1029 { 1030 watchdog_suspend(); 1031 /* 1032 * For "mdb -K", set soft state to debugging 1033 */ 1034 if (soft_state_saved_state == -1) { 1035 mach_get_soft_state(&soft_state_saved_state, 1036 &SOLARIS_SOFT_STATE_SAVED_MSG); 1037 } 1038 /* 1039 * check again as the read above may or may not have worked and if 1040 * it didn't then soft state will still be -1 1041 */ 1042 if (soft_state_saved_state != -1) { 1043 mach_set_soft_state(SIS_TRANSITION, 1044 &SOLARIS_SOFT_STATE_DEBUG_MSG); 1045 } 1046 } 1047 1048 static void 1049 sun4v_system_release(void) 1050 { 1051 watchdog_resume(); 1052 /* 1053 * For "mdb -K", set soft_state state back to original state on exit 1054 */ 1055 if (soft_state_saved_state != -1) { 1056 mach_set_soft_state(soft_state_saved_state, 1057 &SOLARIS_SOFT_STATE_SAVED_MSG); 1058 soft_state_saved_state = -1; 1059 } 1060 } 1061 1062 void 1063 plat_kdi_init(kdi_t *kdi) 1064 { 1065 kdi->pkdi_system_claim = sun4v_system_claim; 1066 kdi->pkdi_system_release = sun4v_system_release; 1067 } 1068 1069 /* 1070 * Routine to return memory information associated 1071 * with a physical address and syndrome. 1072 */ 1073 /* ARGSUSED */ 1074 int 1075 cpu_get_mem_info(uint64_t synd, uint64_t afar, 1076 uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep, 1077 int *segsp, int *banksp, int *mcidp) 1078 { 1079 return (ENOTSUP); 1080 } 1081 1082 /* 1083 * This routine returns the size of the kernel's FRU name buffer. 1084 */ 1085 size_t 1086 cpu_get_name_bufsize() 1087 { 1088 return (UNUM_NAMLEN); 1089 } 1090 1091 /* 1092 * This routine is a more generic interface to cpu_get_mem_unum(), 1093 * that may be used by other modules (e.g. mm). 1094 */ 1095 /* ARGSUSED */ 1096 int 1097 cpu_get_mem_name(uint64_t synd, uint64_t *afsr, uint64_t afar, 1098 char *buf, int buflen, int *lenp) 1099 { 1100 return (ENOTSUP); 1101 } 1102 1103 /* ARGSUSED */ 1104 int 1105 cpu_get_mem_sid(char *unum, char *buf, int buflen, int *lenp) 1106 { 1107 return (ENOTSUP); 1108 } 1109 1110 /* ARGSUSED */ 1111 int 1112 cpu_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp) 1113 { 1114 return (ENOTSUP); 1115 } 1116 1117 /* 1118 * xt_sync - wait for previous x-traps to finish 1119 */ 1120 void 1121 xt_sync(cpuset_t cpuset) 1122 { 1123 union { 1124 uint8_t volatile byte[NCPU]; 1125 uint64_t volatile xword[NCPU / 8]; 1126 } cpu_sync; 1127 uint64_t starttick, endtick, tick, lasttick; 1128 uint_t largestid, smallestid; 1129 int i; 1130 1131 kpreempt_disable(); 1132 CPUSET_DEL(cpuset, CPU->cpu_id); 1133 CPUSET_AND(cpuset, cpu_ready_set); 1134 1135 CPUSET_BOUNDS(cpuset, smallestid, largestid); 1136 if (smallestid == CPUSET_NOTINSET) 1137 goto out; 1138 1139 /* 1140 * Sun4v uses a queue for receiving mondos. Successful 1141 * transmission of a mondo only indicates that the mondo 1142 * has been written into the queue. 1143 * 1144 * We use an array of bytes to let each cpu to signal back 1145 * to the cross trap sender that the cross trap has been 1146 * executed. Set the byte to 1 before sending the cross trap 1147 * and wait until other cpus reset it to 0. 1148 */ 1149 bzero((void *)&cpu_sync, NCPU); 1150 cpu_sync.byte[smallestid] = 1; 1151 if (largestid != smallestid) { 1152 for (i = (smallestid + 1); i <= (largestid - 1); i++) 1153 if (CPU_IN_SET(cpuset, i)) 1154 cpu_sync.byte[i] = 1; 1155 cpu_sync.byte[largestid] = 1; 1156 } 1157 1158 xt_some(cpuset, (xcfunc_t *)xt_sync_tl1, 1159 (uint64_t)cpu_sync.byte, 0); 1160 1161 starttick = lasttick = gettick(); 1162 endtick = starttick + xc_tick_limit; 1163 1164 for (i = (smallestid / 8); i <= (largestid / 8); i++) { 1165 while (cpu_sync.xword[i] != 0) { 1166 tick = gettick(); 1167 /* 1168 * If there is a big jump between the current tick 1169 * count and lasttick, we have probably hit a break 1170 * point. Adjust endtick accordingly to avoid panic. 1171 */ 1172 if (tick > (lasttick + xc_tick_jump_limit)) { 1173 endtick += (tick - lasttick); 1174 } 1175 lasttick = tick; 1176 if (tick > endtick) { 1177 if (panic_quiesce) 1178 goto out; 1179 cmn_err(CE_CONT, "Cross trap sync timeout " 1180 "at cpu_sync.xword[%d]: 0x%lx\n", 1181 i, cpu_sync.xword[i]); 1182 cmn_err(CE_PANIC, "xt_sync: timeout"); 1183 } 1184 } 1185 } 1186 1187 out: 1188 kpreempt_enable(); 1189 } 1190 1191 /* 1192 * Recalculate the values of the cross-call timeout variables based 1193 * on the value of the 'inter-cpu-latency' property of the platform node. 1194 * The property sets the number of nanosec to wait for a cross-call 1195 * to be acknowledged. Other timeout variables are derived from it. 1196 * 1197 * N.B. This implementation is aware of the internals of xc_init() 1198 * and updates many of the same variables. 1199 */ 1200 void 1201 recalc_xc_timeouts(void) 1202 { 1203 typedef union { 1204 uint64_t whole; 1205 struct { 1206 uint_t high; 1207 uint_t low; 1208 } half; 1209 } u_number; 1210 1211 /* See x_call.c for descriptions of these extern variables. */ 1212 extern uint64_t xc_tick_limit_scale; 1213 extern uint64_t xc_mondo_time_limit; 1214 extern uint64_t xc_func_time_limit; 1215 extern uint64_t xc_scale; 1216 extern uint64_t xc_mondo_multiplier; 1217 extern uint_t nsec_shift; 1218 1219 /* Temp versions of the target variables */ 1220 uint64_t tick_limit; 1221 uint64_t tick_jump_limit; 1222 uint64_t mondo_time_limit; 1223 uint64_t func_time_limit; 1224 uint64_t scale; 1225 1226 uint64_t latency; /* nanoseconds */ 1227 uint64_t maxfreq; 1228 uint64_t tick_limit_save = xc_tick_limit; 1229 uint_t tick_scale; 1230 uint64_t top; 1231 uint64_t bottom; 1232 u_number tk; 1233 1234 md_t *mdp; 1235 int nrnode; 1236 mde_cookie_t *platlist; 1237 1238 /* 1239 * Look up the 'inter-cpu-latency' (optional) property in the 1240 * platform node of the MD. The units are nanoseconds. 1241 */ 1242 if ((mdp = md_get_handle()) == NULL) { 1243 cmn_err(CE_WARN, "recalc_xc_timeouts: " 1244 "Unable to initialize machine description"); 1245 return; 1246 } 1247 1248 nrnode = md_alloc_scan_dag(mdp, 1249 md_root_node(mdp), "platform", "fwd", &platlist); 1250 1251 ASSERT(nrnode == 1); 1252 if (nrnode < 1) { 1253 cmn_err(CE_WARN, "recalc_xc_timeouts: platform node missing"); 1254 goto done; 1255 } 1256 1257 if (md_get_prop_val(mdp, platlist[0], 1258 "inter-cpu-latency", &latency) == -1) 1259 goto done; 1260 1261 /* 1262 * clock.h defines an assembly-language macro 1263 * (NATIVE_TIME_TO_NSEC_SCALE) to convert from %stick 1264 * units to nanoseconds. Since the inter-cpu-latency 1265 * units are nanoseconds and the xc_* variables require 1266 * %stick units, we need the inverse of that function. 1267 * The trick is to perform the calculation without 1268 * floating point, but also without integer truncation 1269 * or overflow. To understand the calculation below, 1270 * please read the discussion of the macro in clock.h. 1271 * Since this new code will be invoked infrequently, 1272 * we can afford to implement it in C. 1273 * 1274 * tick_scale is the reciprocal of nsec_scale which is 1275 * calculated at startup in setcpudelay(). The calc 1276 * of tick_limit parallels that of NATIVE_TIME_TO_NSEC_SCALE 1277 * except we use tick_scale instead of nsec_scale and 1278 * C instead of assembler. 1279 */ 1280 tick_scale = (uint_t)(((u_longlong_t)sys_tick_freq 1281 << (32 - nsec_shift)) / NANOSEC); 1282 1283 tk.whole = latency; 1284 top = ((uint64_t)tk.half.high << 4) * tick_scale; 1285 bottom = (((uint64_t)tk.half.low << 4) * (uint64_t)tick_scale) >> 32; 1286 tick_limit = top + bottom; 1287 1288 1289 /* 1290 * xc_init() calculated 'maxfreq' by looking at all the cpus, 1291 * and used it to derive some of the timeout variables that we 1292 * recalculate below. We can back into the original value by 1293 * using the inverse of one of those calculations. 1294 */ 1295 maxfreq = xc_mondo_time_limit / xc_scale; 1296 1297 /* 1298 * Don't allow the new timeout (xc_tick_limit) to fall below 1299 * the system tick frequency (stick). Allowing the timeout 1300 * to be set more tightly than this empirically determined 1301 * value may cause panics. 1302 */ 1303 tick_limit = tick_limit < sys_tick_freq ? sys_tick_freq : tick_limit; 1304 1305 tick_jump_limit = tick_limit / 32; 1306 tick_limit *= xc_tick_limit_scale; 1307 1308 /* 1309 * Recalculate xc_scale since it is used in a callback function 1310 * (xc_func_timeout_adj) to adjust two of the timeouts dynamically. 1311 * Make the change in xc_scale proportional to the change in 1312 * xc_tick_limit. 1313 */ 1314 scale = (xc_scale * tick_limit + sys_tick_freq / 2) / tick_limit_save; 1315 if (scale == 0) 1316 scale = 1; 1317 1318 mondo_time_limit = maxfreq * scale; 1319 func_time_limit = mondo_time_limit * xc_mondo_multiplier; 1320 1321 /* 1322 * Don't modify the timeouts if nothing has changed. Else, 1323 * stuff the variables with the freshly calculated (temp) 1324 * variables. This minimizes the window where the set of 1325 * values could be inconsistent. 1326 */ 1327 if (tick_limit != xc_tick_limit) { 1328 xc_tick_limit = tick_limit; 1329 xc_tick_jump_limit = tick_jump_limit; 1330 xc_scale = scale; 1331 xc_mondo_time_limit = mondo_time_limit; 1332 xc_func_time_limit = func_time_limit; 1333 /* 1334 * Force the new values to be used for future cross 1335 * calls. This is necessary only when we increase 1336 * the timeouts. 1337 */ 1338 if (tick_limit > tick_limit_save) { 1339 cpuset_t cpuset = cpu_ready_set; 1340 1341 xt_sync(cpuset); 1342 } 1343 } 1344 1345 done: 1346 if (nrnode > 0) 1347 md_free_scan_dag(mdp, &platlist); 1348 (void) md_fini_handle(mdp); 1349 } 1350 1351 void 1352 mach_soft_state_init(void) 1353 { 1354 int i; 1355 uint64_t ra; 1356 1357 /* 1358 * Try to register soft_state api. If it fails, soft_state api has not 1359 * been implemented in the firmware, so do not bother to setup 1360 * soft_state in the kernel. 1361 */ 1362 if ((i = hsvc_register(&soft_state_hsvc, &soft_state_sup_minor)) != 0) { 1363 return; 1364 } 1365 for (i = 0; i < SOLARIS_SOFT_STATE_MSG_CNT; i++) { 1366 ASSERT(strlen((const char *)(void *) 1367 soft_state_message_strings + i) < SSM_SIZE); 1368 if ((ra = va_to_pa((void *)(soft_state_message_strings + i))) == 1369 -1ll) { 1370 return; 1371 } 1372 soft_state_message_ra[i] = ra; 1373 } 1374 /* 1375 * Tell OBP that we are supporting Guest State 1376 */ 1377 prom_sun4v_soft_state_supported(); 1378 soft_state_initialized = 1; 1379 } 1380 1381 void 1382 mach_set_soft_state(uint64_t state, uint64_t *string_ra) 1383 { 1384 uint64_t rc; 1385 1386 if (soft_state_initialized && *string_ra) { 1387 rc = hv_soft_state_set(state, *string_ra); 1388 1389 if (rc != H_EOK) { 1390 cmn_err(CE_WARN, "hv_soft_state_set returned %ld\n", 1391 rc); 1392 } 1393 } 1394 } 1395 1396 void 1397 mach_get_soft_state(uint64_t *state, uint64_t *string_ra) 1398 { 1399 int rc; 1400 1401 if (soft_state_initialized && *string_ra) { 1402 rc = hv_soft_state_get(*string_ra, state); 1403 if (rc != H_EOK) { 1404 cmn_err(CE_WARN, "hv_soft_state_get returned %d\n", 1405 rc); 1406 *state = -1; 1407 } 1408 } 1409 } 1410