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