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