xref: /titanic_41/usr/src/uts/sun4v/os/mach_cpu_states.c (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/archsystm.h>
32 #include <sys/t_lock.h>
33 #include <sys/uadmin.h>
34 #include <sys/panic.h>
35 #include <sys/reboot.h>
36 #include <sys/autoconf.h>
37 #include <sys/machsystm.h>
38 #include <sys/promif.h>
39 #include <sys/membar.h>
40 #include <vm/hat_sfmmu.h>
41 #include <sys/cpu_module.h>
42 #include <sys/cpu_sgnblk_defs.h>
43 #include <sys/intreg.h>
44 #include <sys/consdev.h>
45 #include <sys/kdi_impl.h>
46 #include <sys/traptrace.h>
47 #include <sys/hypervisor_api.h>
48 #include <sys/vmsystm.h>
49 #include <sys/dtrace.h>
50 #include <sys/xc_impl.h>
51 #include <sys/callb.h>
52 
53 /*
54  * hvdump_buf_va is a pointer to the currently-configured hvdump_buf.
55  * A value of NULL indicates that this area is not configured.
56  * hvdump_buf_sz is tunable but will be clamped to HVDUMP_SIZE_MAX.
57  */
58 
59 caddr_t hvdump_buf_va;
60 uint64_t hvdump_buf_sz = HVDUMP_SIZE_DEFAULT;
61 static uint64_t hvdump_buf_pa;
62 
63 u_longlong_t panic_tick;
64 
65 extern u_longlong_t gettick();
66 static void reboot_machine(char *);
67 static void update_hvdump_buffer(void);
68 
69 /*
70  * For xt_sync synchronization.
71  */
72 extern uint64_t xc_tick_limit;
73 extern uint64_t xc_tick_jump_limit;
74 
75 /*
76  * We keep our own copies, used for cache flushing, because we can be called
77  * before cpu_fiximpl().
78  */
79 static int kdi_dcache_size;
80 static int kdi_dcache_linesize;
81 static int kdi_icache_size;
82 static int kdi_icache_linesize;
83 
84 /*
85  * Assembly support for generic modules in sun4v/ml/mach_xc.s
86  */
87 extern void init_mondo_nocheck(xcfunc_t *func, uint64_t arg1, uint64_t arg2);
88 extern void kdi_flush_idcache(int, int, int, int);
89 extern uint64_t get_cpuaddr(uint64_t, uint64_t);
90 
91 /*
92  * Machine dependent code to reboot.
93  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
94  * to a string to be used as the argument string when rebooting.
95  *
96  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
97  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
98  * we are in a normal shutdown sequence (interrupts are not blocked, the
99  * system is not panic'ing or being suspended).
100  */
101 /*ARGSUSED*/
102 void
103 mdboot(int cmd, int fcn, char *bootstr, boolean_t invoke_cb)
104 {
105 	extern void pm_cfb_check_and_powerup(void);
106 
107 	/*
108 	 * XXX - rconsvp is set to NULL to ensure that output messages
109 	 * are sent to the underlying "hardware" device using the
110 	 * monitor's printf routine since we are in the process of
111 	 * either rebooting or halting the machine.
112 	 */
113 	rconsvp = NULL;
114 
115 	/*
116 	 * At a high interrupt level we can't:
117 	 *	1) bring up the console
118 	 * or
119 	 *	2) wait for pending interrupts prior to redistribution
120 	 *	   to the current CPU
121 	 *
122 	 * so we do them now.
123 	 */
124 	pm_cfb_check_and_powerup();
125 
126 	/* make sure there are no more changes to the device tree */
127 	devtree_freeze();
128 
129 	if (invoke_cb)
130 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
131 
132 	/*
133 	 * Clear any unresolved UEs from memory.
134 	 */
135 	if (memsegs != NULL)
136 		page_retire_hunt(page_retire_mdboot_cb);
137 
138 	/*
139 	 * stop other cpus which also raise our priority. since there is only
140 	 * one active cpu after this, and our priority will be too high
141 	 * for us to be preempted, we're essentially single threaded
142 	 * from here on out.
143 	 */
144 	stop_other_cpus();
145 
146 	/*
147 	 * try and reset leaf devices.  reset_leaves() should only
148 	 * be called when there are no other threads that could be
149 	 * accessing devices
150 	 */
151 	reset_leaves();
152 
153 	if (fcn == AD_HALT) {
154 		halt((char *)NULL);
155 	} else if (fcn == AD_POWEROFF) {
156 		power_down(NULL);
157 	} else {
158 		if (bootstr == NULL) {
159 			switch (fcn) {
160 
161 			case AD_BOOT:
162 				bootstr = "";
163 				break;
164 
165 			case AD_IBOOT:
166 				bootstr = "-a";
167 				break;
168 
169 			case AD_SBOOT:
170 				bootstr = "-s";
171 				break;
172 
173 			case AD_SIBOOT:
174 				bootstr = "-sa";
175 				break;
176 			default:
177 				cmn_err(CE_WARN,
178 				    "mdboot: invalid function %d", fcn);
179 				bootstr = "";
180 				break;
181 			}
182 		}
183 		reboot_machine(bootstr);
184 	}
185 	/* MAYBE REACHED */
186 }
187 
188 /* mdpreboot - may be called prior to mdboot while root fs still mounted */
189 /*ARGSUSED*/
190 void
191 mdpreboot(int cmd, int fcn, char *bootstr)
192 {
193 }
194 
195 /*
196  * Halt the machine and then reboot with the device
197  * and arguments specified in bootstr.
198  */
199 static void
200 reboot_machine(char *bootstr)
201 {
202 	flush_windows();
203 	stop_other_cpus();		/* send stop signal to other CPUs */
204 	prom_printf("rebooting...\n");
205 	/*
206 	 * For platforms that use CPU signatures, we
207 	 * need to set the signature block to OS and
208 	 * the state to exiting for all the processors.
209 	 */
210 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_REBOOT, -1);
211 	prom_reboot(bootstr);
212 	/*NOTREACHED*/
213 }
214 
215 /*
216  * We use the x-trap mechanism and idle_stop_xcall() to stop the other CPUs.
217  * Once in panic_idle() they raise spl, record their location, and spin.
218  */
219 static void
220 panic_idle(void)
221 {
222 	(void) spl7();
223 
224 	debug_flush_windows();
225 	(void) setjmp(&curthread->t_pcb);
226 
227 	CPU->cpu_m.in_prom = 1;
228 	membar_stld();
229 
230 	for (;;);
231 }
232 
233 /*
234  * Force the other CPUs to trap into panic_idle(), and then remove them
235  * from the cpu_ready_set so they will no longer receive cross-calls.
236  */
237 /*ARGSUSED*/
238 void
239 panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
240 {
241 	cpuset_t cps;
242 	int i;
243 
244 	(void) splzs();
245 	CPUSET_ALL_BUT(cps, cp->cpu_id);
246 	xt_some(cps, (xcfunc_t *)idle_stop_xcall, (uint64_t)&panic_idle, NULL);
247 
248 	for (i = 0; i < NCPU; i++) {
249 		if (i != cp->cpu_id && CPU_XCALL_READY(i)) {
250 			int ntries = 0x10000;
251 
252 			while (!cpu[i]->cpu_m.in_prom && ntries) {
253 				DELAY(50);
254 				ntries--;
255 			}
256 
257 			if (!cpu[i]->cpu_m.in_prom)
258 				printf("panic: failed to stop cpu%d\n", i);
259 
260 			cpu[i]->cpu_flags &= ~CPU_READY;
261 			cpu[i]->cpu_flags |= CPU_QUIESCED;
262 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
263 		}
264 	}
265 }
266 
267 /*
268  * Platform callback following each entry to panicsys().  If we've panicked at
269  * level 14, we examine t_panic_trap to see if a fatal trap occurred.  If so,
270  * we disable further %tick_cmpr interrupts.  If not, an explicit call to panic
271  * was made and so we re-enqueue an interrupt request structure to allow
272  * further level 14 interrupts to be processed once we lower PIL.  This allows
273  * us to handle panics from the deadman() CY_HIGH_LEVEL cyclic.
274  */
275 void
276 panic_enter_hw(int spl)
277 {
278 	if (!panic_tick) {
279 		panic_tick = gettick();
280 		if (mach_htraptrace_enable) {
281 			uint64_t prev_freeze;
282 
283 			/*  there are no possible error codes for this hcall */
284 			(void) hv_ttrace_freeze((uint64_t)TRAP_TFREEZE_ALL,
285 			    &prev_freeze);
286 		}
287 #ifdef TRAPTRACE
288 		TRAPTRACE_FREEZE;
289 #endif
290 	}
291 	if (spl == ipltospl(PIL_14)) {
292 		uint_t opstate = disable_vec_intr();
293 
294 		if (curthread->t_panic_trap != NULL) {
295 			tickcmpr_disable();
296 			intr_dequeue_req(PIL_14, cbe_level14_inum);
297 		} else {
298 			if (!tickcmpr_disabled())
299 				intr_enqueue_req(PIL_14, cbe_level14_inum);
300 			/*
301 			 * Clear SOFTINT<14>, SOFTINT<0> (TICK_INT)
302 			 * and SOFTINT<16> (STICK_INT) to indicate
303 			 * that the current level 14 has been serviced.
304 			 */
305 			wr_clr_softint((1 << PIL_14) |
306 				TICK_INT_MASK | STICK_INT_MASK);
307 		}
308 
309 		enable_vec_intr(opstate);
310 	}
311 }
312 
313 /*
314  * Miscellaneous hardware-specific code to execute after panicstr is set
315  * by the panic code: we also print and record PTL1 panic information here.
316  */
317 /*ARGSUSED*/
318 void
319 panic_quiesce_hw(panic_data_t *pdp)
320 {
321 	extern uint_t getpstate(void);
322 	extern void setpstate(uint_t);
323 
324 	/*
325 	 * Turn off TRAPTRACE and save the current %tick value in panic_tick.
326 	 */
327 	if (!panic_tick) {
328 		panic_tick = gettick();
329 		if (mach_htraptrace_enable) {
330 			uint64_t prev_freeze;
331 
332 			/*  there are no possible error codes for this hcall */
333 			(void) hv_ttrace_freeze((uint64_t)TRAP_TFREEZE_ALL,
334 			    &prev_freeze);
335 		}
336 #ifdef TRAPTRACE
337 		TRAPTRACE_FREEZE;
338 #endif
339 	}
340 	/*
341 	 * For Platforms that use CPU signatures, we
342 	 * need to set the signature block to OS, the state to
343 	 * exiting, and the substate to panic for all the processors.
344 	 */
345 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_PANIC, -1);
346 
347 	update_hvdump_buffer();
348 
349 	/*
350 	 * Disable further ECC errors from the bus nexus.
351 	 */
352 	(void) bus_func_invoke(BF_TYPE_ERRDIS);
353 
354 	/*
355 	 * Redirect all interrupts to the current CPU.
356 	 */
357 	intr_redist_all_cpus_shutdown();
358 
359 	/*
360 	 * This call exists solely to support dumps to network
361 	 * devices after sync from OBP.
362 	 *
363 	 * If we came here via the sync callback, then on some
364 	 * platforms, interrupts may have arrived while we were
365 	 * stopped in OBP.  OBP will arrange for those interrupts to
366 	 * be redelivered if you say "go", but not if you invoke a
367 	 * client callback like 'sync'.	 For some dump devices
368 	 * (network swap devices), we need interrupts to be
369 	 * delivered in order to dump, so we have to call the bus
370 	 * nexus driver to reset the interrupt state machines.
371 	 */
372 	(void) bus_func_invoke(BF_TYPE_RESINTR);
373 
374 	setpstate(getpstate() | PSTATE_IE);
375 }
376 
377 /*
378  * Platforms that use CPU signatures need to set the signature block to OS and
379  * the state to exiting for all CPUs. PANIC_CONT indicates that we're about to
380  * write the crash dump, which tells the SSP/SMS to begin a timeout routine to
381  * reboot the machine if the dump never completes.
382  */
383 /*ARGSUSED*/
384 void
385 panic_dump_hw(int spl)
386 {
387 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
388 }
389 
390 /*
391  * for ptl1_panic
392  */
393 void
394 ptl1_init_cpu(struct cpu *cpu)
395 {
396 	ptl1_state_t *pstate = &cpu->cpu_m.ptl1_state;
397 
398 	/*CONSTCOND*/
399 	if (sizeof (struct cpu) + PTL1_SSIZE > CPU_ALLOC_SIZE) {
400 		panic("ptl1_init_cpu: not enough space left for ptl1_panic "
401 		    "stack, sizeof (struct cpu) = %lu",
402 		    (unsigned long)sizeof (struct cpu));
403 	}
404 
405 	pstate->ptl1_stktop = (uintptr_t)cpu + CPU_ALLOC_SIZE;
406 	cpu_pa[cpu->cpu_id] = va_to_pa(cpu);
407 }
408 
409 void
410 ptl1_panic_handler(ptl1_state_t *pstate)
411 {
412 	static const char *ptl1_reasons[] = {
413 #ifdef	PTL1_PANIC_DEBUG
414 		"trap for debug purpose",	/* PTL1_BAD_DEBUG */
415 #else
416 		"unknown trap",			/* PTL1_BAD_DEBUG */
417 #endif
418 		"register window trap",		/* PTL1_BAD_WTRAP */
419 		"kernel MMU miss",		/* PTL1_BAD_KMISS */
420 		"kernel protection fault",	/* PTL1_BAD_KPROT_FAULT */
421 		"ISM MMU miss",			/* PTL1_BAD_ISM */
422 		"kernel MMU trap",		/* PTL1_BAD_MMUTRAP */
423 		"kernel trap handler state",	/* PTL1_BAD_TRAP */
424 		"floating point trap",		/* PTL1_BAD_FPTRAP */
425 #ifdef	DEBUG
426 		"pointer to intr_req",		/* PTL1_BAD_INTR_REQ */
427 #else
428 		"unknown trap",			/* PTL1_BAD_INTR_REQ */
429 #endif
430 #ifdef	TRAPTRACE
431 		"TRACE_PTR state",		/* PTL1_BAD_TRACE_PTR */
432 #else
433 		"unknown trap",			/* PTL1_BAD_TRACE_PTR */
434 #endif
435 		"stack overflow",		/* PTL1_BAD_STACK */
436 		"DTrace flags",			/* PTL1_BAD_DTRACE_FLAGS */
437 		"attempt to steal locked ctx",  /* PTL1_BAD_CTX_STEAL */
438 		"CPU ECC error loop",		/* PTL1_BAD_ECC */
439 		"unexpected error from hypervisor call", /* PTL1_BAD_HCALL */
440 		"unexpected global level(%gl)", /* PTL1_BAD_GL */
441 	};
442 
443 	uint_t reason = pstate->ptl1_regs.ptl1_gregs[0].ptl1_g1;
444 	uint_t tl = pstate->ptl1_regs.ptl1_trap_regs[0].ptl1_tl;
445 	struct trap_info ti = { 0 };
446 
447 	/*
448 	 * Use trap_info for a place holder to call panic_savetrap() and
449 	 * panic_showtrap() to save and print out ptl1_panic information.
450 	 */
451 	if (curthread->t_panic_trap == NULL)
452 		curthread->t_panic_trap = &ti;
453 
454 	if (reason < sizeof (ptl1_reasons) / sizeof (ptl1_reasons[0]))
455 		panic("bad %s at TL %u", ptl1_reasons[reason], tl);
456 	else
457 		panic("ptl1_panic reason 0x%x at TL %u", reason, tl);
458 }
459 
460 void
461 clear_watchdog_on_exit(void)
462 {
463 }
464 
465 void
466 clear_watchdog_timer(void)
467 {
468 }
469 
470 int
471 kdi_watchdog_disable(void)
472 {
473 	return (0);	/* sun4v has no watchdog */
474 }
475 
476 void
477 kdi_watchdog_restore(void)
478 {
479 	/* nothing to do -- no watchdog to re-enable */
480 }
481 
482 void
483 mach_dump_buffer_init(void)
484 {
485 	uint64_t  ret, minsize = 0;
486 
487 	if (hvdump_buf_sz > HVDUMP_SIZE_MAX)
488 		hvdump_buf_sz = HVDUMP_SIZE_MAX;
489 
490 	hvdump_buf_va = contig_mem_alloc_align(hvdump_buf_sz, PAGESIZE);
491 	if (hvdump_buf_va == NULL)
492 		return;
493 
494 	hvdump_buf_pa = va_to_pa(hvdump_buf_va);
495 
496 	ret = hv_dump_buf_update(hvdump_buf_pa, hvdump_buf_sz,
497 	    &minsize);
498 
499 	if (ret != H_EOK) {
500 		contig_mem_free(hvdump_buf_va, hvdump_buf_sz);
501 		hvdump_buf_va = NULL;
502 		cmn_err(CE_NOTE, "!Error in setting up hvstate"
503 		    "dump buffer. Error = 0x%lx, size = 0x%lx,"
504 		    "buf_pa = 0x%lx", ret, hvdump_buf_sz,
505 		    hvdump_buf_pa);
506 
507 		if (ret == H_EINVAL) {
508 			cmn_err(CE_NOTE, "!Buffer size too small."
509 			    "Available buffer size = 0x%lx,"
510 			    "Minimum buffer size required = 0x%lx",
511 			    hvdump_buf_sz, minsize);
512 		}
513 	}
514 }
515 
516 
517 static void
518 update_hvdump_buffer(void)
519 {
520 	uint64_t ret, dummy_val;
521 
522 	if (hvdump_buf_va == NULL)
523 		return;
524 
525 	ret = hv_dump_buf_update(hvdump_buf_pa, hvdump_buf_sz,
526 	    &dummy_val);
527 	if (ret != H_EOK) {
528 		cmn_err(CE_NOTE, "!Cannot update hvstate dump"
529 		    "buffer. Error = 0x%lx", ret);
530 	}
531 }
532 
533 
534 static int
535 getintprop(pnode_t node, char *name, int deflt)
536 {
537 	int	value;
538 
539 	switch (prom_getproplen(node, name)) {
540 	case 0:
541 		value = 1;	/* boolean properties */
542 		break;
543 
544 	case sizeof (int):
545 		(void) prom_getprop(node, name, (caddr_t)&value);
546 		break;
547 
548 	default:
549 		value = deflt;
550 		break;
551 	}
552 
553 	return (value);
554 }
555 
556 /*
557  * Called by setcpudelay
558  */
559 void
560 cpu_init_tick_freq(void)
561 {
562 	sys_tick_freq = cpunodes[CPU->cpu_id].clock_freq;
563 }
564 
565 int shipit(int n, uint64_t cpu_list_ra);
566 extern uint64_t xc_tick_limit;
567 extern uint64_t xc_tick_jump_limit;
568 
569 #ifdef DEBUG
570 #define	SEND_MONDO_STATS	1
571 #endif
572 
573 #ifdef SEND_MONDO_STATS
574 uint32_t x_one_stimes[64];
575 uint32_t x_one_ltimes[16];
576 uint32_t x_set_stimes[64];
577 uint32_t x_set_ltimes[16];
578 uint32_t x_set_cpus[NCPU];
579 #endif
580 
581 void
582 send_one_mondo(int cpuid)
583 {
584 	int retries, stat;
585 	uint64_t starttick, endtick, tick, lasttick;
586 	struct machcpu	*mcpup = &(CPU->cpu_m);
587 
588 	CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
589 	starttick = lasttick = gettick();
590 	mcpup->cpu_list[0] = (uint16_t)cpuid;
591 	stat = shipit(1, mcpup->cpu_list_ra);
592 	endtick = starttick + xc_tick_limit;
593 	retries = 0;
594 	while (stat != H_EOK) {
595 		if (stat != H_EWOULDBLOCK) {
596 			if (panic_quiesce)
597 				return;
598 			if (stat == H_ECPUERROR)
599 				cmn_err(CE_PANIC, "send_one_mondo: "
600 				    "cpuid: 0x%x has been marked in "
601 				    "error", cpuid);
602 			else
603 				cmn_err(CE_PANIC, "send_one_mondo: "
604 				    "unexpected hypervisor error 0x%x "
605 				    "while sending a mondo to cpuid: "
606 				    "0x%x", stat, cpuid);
607 		}
608 		tick = gettick();
609 		/*
610 		 * If there is a big jump between the current tick
611 		 * count and lasttick, we have probably hit a break
612 		 * point.  Adjust endtick accordingly to avoid panic.
613 		 */
614 		if (tick > (lasttick + xc_tick_jump_limit))
615 			endtick += (tick - lasttick);
616 		lasttick = tick;
617 		if (tick > endtick) {
618 			if (panic_quiesce)
619 				return;
620 			cmn_err(CE_PANIC, "send mondo timeout "
621 			    "(target 0x%x) [retries: 0x%x hvstat: 0x%x]",
622 			    cpuid, retries, stat);
623 		}
624 		drv_usecwait(1);
625 		stat = shipit(1, mcpup->cpu_list_ra);
626 		retries++;
627 	}
628 #ifdef SEND_MONDO_STATS
629 	{
630 		uint64_t n = gettick() - starttick;
631 		if (n < 8192)
632 			x_one_stimes[n >> 7]++;
633 		else if (n < 15*8192)
634 			x_one_ltimes[n >> 13]++;
635 		else
636 			x_one_ltimes[0xf]++;
637 	}
638 #endif
639 }
640 
641 void
642 send_mondo_set(cpuset_t set)
643 {
644 	uint64_t starttick, endtick, tick, lasttick;
645 	int shipped = 0;
646 	int retries = 0;
647 	struct machcpu	*mcpup = &(CPU->cpu_m);
648 
649 	ASSERT(!CPUSET_ISNULL(set));
650 	starttick = lasttick = gettick();
651 	endtick = starttick + xc_tick_limit;
652 
653 	do {
654 		int ncpuids = 0;
655 		int i, stat;
656 
657 		/* assemble CPU list for HV argument */
658 		for (i = 0; i < NCPU; i++) {
659 			if (CPU_IN_SET(set, i)) {
660 				mcpup->cpu_list[ncpuids] = (uint16_t)i;
661 				ncpuids++;
662 			}
663 		}
664 
665 		stat = shipit(ncpuids, mcpup->cpu_list_ra);
666 		if (stat == H_EOK) {
667 			shipped += ncpuids;
668 			break;
669 		}
670 
671 		/*
672 		 * Either not all CPU mondos were sent, or an
673 		 * error occurred. CPUs that were sent mondos
674 		 * have their CPU IDs overwritten in cpu_list.
675 		 * Reset the cpuset so that its only members
676 		 * are those CPU IDs that still need to be sent.
677 		 */
678 		CPUSET_ZERO(set);
679 		for (i = 0; i < ncpuids; i++) {
680 			if (mcpup->cpu_list[i] == HV_SEND_MONDO_ENTRYDONE) {
681 				shipped++;
682 			} else {
683 				CPUSET_ADD(set, mcpup->cpu_list[i]);
684 			}
685 		}
686 
687 		/*
688 		 * Now handle possible errors returned
689 		 * from hypervisor.
690 		 */
691 		if (stat == H_ECPUERROR) {
692 			cpuset_t error_set;
693 
694 			/*
695 			 * One or more of the CPUs passed to HV is
696 			 * in the error state. Remove those CPUs from
697 			 * set and record them in error_set.
698 			 */
699 			CPUSET_ZERO(error_set);
700 			for (i = 0; i < NCPU; i++) {
701 				if (CPU_IN_SET(set, i)) {
702 					uint64_t state = CPU_STATE_INVALID;
703 					(void) hv_cpu_state(i, &state);
704 					if (state == CPU_STATE_ERROR) {
705 						CPUSET_ADD(error_set, i);
706 						CPUSET_DEL(set, i);
707 					}
708 				}
709 			}
710 
711 			if (!panic_quiesce) {
712 				if (CPUSET_ISNULL(error_set)) {
713 					cmn_err(CE_PANIC, "send_mondo_set: "
714 					    "hypervisor returned "
715 					    "H_ECPUERROR but no CPU in "
716 					    "cpu_list in error state");
717 				}
718 
719 				cmn_err(CE_CONT, "send_mondo_set: cpuid(s) ");
720 				for (i = 0; i < NCPU; i++) {
721 					if (CPU_IN_SET(error_set, i)) {
722 						cmn_err(CE_CONT, "0x%x ", i);
723 					}
724 				}
725 				cmn_err(CE_CONT, "have been marked in "
726 				    "error\n");
727 				cmn_err(CE_PANIC, "send_mondo_set: CPU(s) "
728 				    "in error state");
729 			}
730 		} else if (stat != H_EWOULDBLOCK) {
731 			if (panic_quiesce)
732 				return;
733 			/*
734 			 * For all other errors, panic.
735 			 */
736 			cmn_err(CE_CONT, "send_mondo_set: unexpected "
737 			    "hypervisor error 0x%x while sending a "
738 			    "mondo to cpuid(s):", stat);
739 			for (i = 0; i < NCPU; i++) {
740 				if (CPU_IN_SET(set, i)) {
741 					cmn_err(CE_CONT, " 0x%x", i);
742 				}
743 			}
744 			cmn_err(CE_CONT, "\n");
745 			cmn_err(CE_PANIC, "send_mondo_set: unexpected "
746 			    "hypervisor error");
747 		}
748 
749 		tick = gettick();
750 		/*
751 		 * If there is a big jump between the current tick
752 		 * count and lasttick, we have probably hit a break
753 		 * point.  Adjust endtick accordingly to avoid panic.
754 		 */
755 		if (tick > (lasttick + xc_tick_jump_limit))
756 			endtick += (tick - lasttick);
757 		lasttick = tick;
758 		if (tick > endtick) {
759 			if (panic_quiesce)
760 				return;
761 			cmn_err(CE_CONT, "send mondo timeout "
762 			    "[retries: 0x%x]  cpuids: ", retries);
763 			for (i = 0; i < NCPU; i++)
764 				if (CPU_IN_SET(set, i))
765 					cmn_err(CE_CONT, " 0x%x", i);
766 			cmn_err(CE_CONT, "\n");
767 			cmn_err(CE_PANIC, "send_mondo_set: timeout");
768 		}
769 
770 		while (gettick() < (tick + sys_clock_mhz))
771 			;
772 		retries++;
773 	} while (!CPUSET_ISNULL(set));
774 
775 	CPU_STATS_ADDQ(CPU, sys, xcalls, shipped);
776 
777 #ifdef SEND_MONDO_STATS
778 	{
779 		uint64_t n = gettick() - starttick;
780 		if (n < 8192)
781 			x_set_stimes[n >> 7]++;
782 		else if (n < 15*8192)
783 			x_set_ltimes[n >> 13]++;
784 		else
785 			x_set_ltimes[0xf]++;
786 	}
787 	x_set_cpus[shipped]++;
788 #endif
789 }
790 
791 void
792 syncfpu(void)
793 {
794 }
795 
796 void
797 cpu_flush_ecache(void)
798 {
799 }
800 
801 void
802 sticksync_slave(void)
803 {}
804 
805 void
806 sticksync_master(void)
807 {}
808 
809 void
810 cpu_init_cache_scrub(void)
811 {}
812 
813 int
814 dtrace_blksuword32_err(uintptr_t addr, uint32_t *data)
815 {
816 	int ret, watched;
817 
818 	watched = watch_disable_addr((void *)addr, 4, S_WRITE);
819 	ret = dtrace_blksuword32(addr, data, 0);
820 	if (watched)
821 		watch_enable_addr((void *)addr, 4, S_WRITE);
822 
823 	return (ret);
824 }
825 
826 int
827 dtrace_blksuword32(uintptr_t addr, uint32_t *data, int tryagain)
828 {
829 	if (suword32((void *)addr, *data) == -1)
830 		return (tryagain ? dtrace_blksuword32_err(addr, data) : -1);
831 	dtrace_flush_sec(addr);
832 
833 	return (0);
834 }
835 
836 /*ARGSUSED*/
837 void
838 cpu_faulted_enter(struct cpu *cp)
839 {
840 }
841 
842 /*ARGSUSED*/
843 void
844 cpu_faulted_exit(struct cpu *cp)
845 {
846 }
847 
848 static int
849 kdi_cpu_ready_iter(int (*cb)(int, void *), void *arg)
850 {
851 	int rc, i;
852 
853 	for (rc = 0, i = 0; i < NCPU; i++) {
854 		if (CPU_IN_SET(cpu_ready_set, i))
855 			rc += cb(i, arg);
856 	}
857 
858 	return (rc);
859 }
860 
861 /*
862  * Sends a cross-call to a specified processor.  The caller assumes
863  * responsibility for repetition of cross-calls, as appropriate (MARSA for
864  * debugging).
865  */
866 static int
867 kdi_xc_one(int cpuid, void (*func)(uintptr_t, uintptr_t), uintptr_t arg1,
868     uintptr_t arg2)
869 {
870 	int stat;
871 	struct machcpu	*mcpup;
872 	uint64_t cpuaddr_reg = 0, cpuaddr_scr = 0;
873 
874 	mcpup = &(((cpu_t *)get_cpuaddr(cpuaddr_reg, cpuaddr_scr))->cpu_m);
875 
876 	/*
877 	 * if (idsr_busy())
878 	 *	return (KDI_XC_RES_ERR);
879 	 */
880 
881 	init_mondo_nocheck((xcfunc_t *)func, arg1, arg2);
882 
883 	mcpup->cpu_list[0] = (uint16_t)cpuid;
884 	stat = shipit(1, mcpup->cpu_list_ra);
885 
886 	if (stat == 0)
887 		return (KDI_XC_RES_OK);
888 	else
889 		return (KDI_XC_RES_NACK);
890 }
891 
892 static void
893 kdi_tickwait(clock_t nticks)
894 {
895 	clock_t endtick = gettick() + nticks;
896 
897 	while (gettick() < endtick);
898 }
899 
900 static void
901 kdi_cpu_init(int dcache_size, int dcache_linesize, int icache_size,
902     int icache_linesize)
903 {
904 	kdi_dcache_size = dcache_size;
905 	kdi_dcache_linesize = dcache_linesize;
906 	kdi_icache_size = icache_size;
907 	kdi_icache_linesize = icache_linesize;
908 }
909 
910 /* used directly by kdi_read/write_phys */
911 void
912 kdi_flush_caches(void)
913 {
914 	/* Not required on sun4v architecture. */
915 }
916 
917 /*ARGSUSED*/
918 int
919 kdi_get_stick(uint64_t *stickp)
920 {
921 	return (-1);
922 }
923 
924 void
925 cpu_kdi_init(kdi_t *kdi)
926 {
927 	kdi->kdi_flush_caches = kdi_flush_caches;
928 	kdi->mkdi_cpu_init = kdi_cpu_init;
929 	kdi->mkdi_cpu_ready_iter = kdi_cpu_ready_iter;
930 	kdi->mkdi_xc_one = kdi_xc_one;
931 	kdi->mkdi_tickwait = kdi_tickwait;
932 	kdi->mkdi_get_stick = kdi_get_stick;
933 }
934 
935 /*
936  * Routine to return memory information associated
937  * with a physical address and syndrome.
938  */
939 /* ARGSUSED */
940 int
941 cpu_get_mem_info(uint64_t synd, uint64_t afar,
942     uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
943     int *segsp, int *banksp, int *mcidp)
944 {
945 	return (ENOTSUP);
946 }
947 
948 /*
949  * This routine returns the size of the kernel's FRU name buffer.
950  */
951 size_t
952 cpu_get_name_bufsize()
953 {
954 	return (UNUM_NAMLEN);
955 }
956 
957 /*
958  * This routine is a more generic interface to cpu_get_mem_unum(),
959  * that may be used by other modules (e.g. mm).
960  */
961 /* ARGSUSED */
962 int
963 cpu_get_mem_name(uint64_t synd, uint64_t *afsr, uint64_t afar,
964     char *buf, int buflen, int *lenp)
965 {
966 	return (ENOTSUP);
967 }
968 
969 /* ARGSUSED */
970 int
971 cpu_get_mem_sid(char *unum, char *buf, int buflen, int *lenp)
972 {
973 	return (ENOTSUP);
974 }
975 
976 /* ARGSUSED */
977 int
978 cpu_get_mem_addr(char *unum, char *sid, uint64_t offset, uint64_t *addrp)
979 {
980 	return (ENOTSUP);
981 }
982 
983 /*
984  * xt_sync - wait for previous x-traps to finish
985  */
986 void
987 xt_sync(cpuset_t cpuset)
988 {
989 	union {
990 		uint8_t volatile byte[NCPU];
991 		uint64_t volatile xword[NCPU / 8];
992 	} cpu_sync;
993 	uint64_t starttick, endtick, tick, lasttick;
994 	int i;
995 
996 	kpreempt_disable();
997 	CPUSET_DEL(cpuset, CPU->cpu_id);
998 	CPUSET_AND(cpuset, cpu_ready_set);
999 
1000 	/*
1001 	 * Sun4v uses a queue for receiving mondos. Successful
1002 	 * transmission of a mondo only indicates that the mondo
1003 	 * has been written into the queue.
1004 	 *
1005 	 * We use an array of bytes to let each cpu to signal back
1006 	 * to the cross trap sender that the cross trap has been
1007 	 * executed. Set the byte to 1 before sending the cross trap
1008 	 * and wait until other cpus reset it to 0.
1009 	 */
1010 	bzero((void *)&cpu_sync, NCPU);
1011 	for (i = 0; i < NCPU; i++)
1012 		if (CPU_IN_SET(cpuset, i))
1013 			cpu_sync.byte[i] = 1;
1014 
1015 	xt_some(cpuset, (xcfunc_t *)xt_sync_tl1,
1016 	    (uint64_t)cpu_sync.byte, 0);
1017 
1018 	starttick = lasttick = gettick();
1019 	endtick = starttick + xc_tick_limit;
1020 
1021 	for (i = 0; i < (NCPU / 8); i ++) {
1022 		while (cpu_sync.xword[i] != 0) {
1023 			tick = gettick();
1024 			/*
1025 			 * If there is a big jump between the current tick
1026 			 * count and lasttick, we have probably hit a break
1027 			 * point. Adjust endtick accordingly to avoid panic.
1028 			 */
1029 			if (tick > (lasttick + xc_tick_jump_limit)) {
1030 				endtick += (tick - lasttick);
1031 			}
1032 			lasttick = tick;
1033 			if (tick > endtick) {
1034 				if (panic_quiesce)
1035 					goto out;
1036 				cmn_err(CE_CONT, "Cross trap sync timeout "
1037 				    "at cpu_sync.xword[%d]: 0x%lx\n",
1038 				    i, cpu_sync.xword[i]);
1039 				cmn_err(CE_PANIC, "xt_sync: timeout");
1040 			}
1041 		}
1042 	}
1043 
1044 out:
1045 	kpreempt_enable();
1046 }
1047