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