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