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