xref: /titanic_51/usr/src/uts/sun4v/io/hardclk.c (revision 8fc99e42676a23421c75e76660640f9765d693b1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53c431bb5Swentaoy  * Common Development and Distribution License (the "License").
63c431bb5Swentaoy  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8fc99e42STrevor Thompson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/time.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/debug.h>
317c478bd9Sstevel@tonic-gate #include <sys/clock.h>
327c478bd9Sstevel@tonic-gate #include <sys/intreg.h>
337c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
357c478bd9Sstevel@tonic-gate #include <sys/promif.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
397c478bd9Sstevel@tonic-gate #include <vm/as.h>
407c478bd9Sstevel@tonic-gate #include <vm/hat.h>
417c478bd9Sstevel@tonic-gate #include <sys/intr.h>
427c478bd9Sstevel@tonic-gate #include <sys/ivintr.h>
437c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
457c478bd9Sstevel@tonic-gate #include <sys/membar.h>
467c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
477c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
487c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
493c431bb5Swentaoy #include <sys/wdt.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate uint_t sys_clock_mhz = 0;
527c478bd9Sstevel@tonic-gate uint64_t sys_tick_freq = 0;
537c478bd9Sstevel@tonic-gate uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
547c478bd9Sstevel@tonic-gate uint_t scaled_clock_mhz = 0;
557c478bd9Sstevel@tonic-gate uint_t nsec_per_sys_tick;
567c478bd9Sstevel@tonic-gate uint_t sticks_per_usec;
577c478bd9Sstevel@tonic-gate char clock_started = 0;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate void
607c478bd9Sstevel@tonic-gate clkstart(void)
617c478bd9Sstevel@tonic-gate {
623c431bb5Swentaoy 	/*
633c431bb5Swentaoy 	 * Now is a good time to activate hardware watchdog.
643c431bb5Swentaoy 	 */
653c431bb5Swentaoy 	watchdog_init();
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * preset the delay constant for drv_usecwait(). This is done for early
707c478bd9Sstevel@tonic-gate  * use of the le or scsi drivers in the kernel. The default contant
717c478bd9Sstevel@tonic-gate  * might be too high early on. We can get a pretty good approximation
727c478bd9Sstevel@tonic-gate  * of this by setting it as:
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * setcpudelay is called twice during the boot process. The first time
777c478bd9Sstevel@tonic-gate  * is before the TOD driver is loaded so cpu_init_tick_freq cannot
787c478bd9Sstevel@tonic-gate  * calibrate sys_tick_freq but can only set it to the prom value. The
797c478bd9Sstevel@tonic-gate  * first call is also before /etc/system is read.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * Only call cpu_init_tick_freq the second time around if sys_tick_freq
827c478bd9Sstevel@tonic-gate  * has not been tuned via /etc/system.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate void
857c478bd9Sstevel@tonic-gate setcpudelay(void)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	static uint64_t sys_tick_freq_save = 0;
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
907c478bd9Sstevel@tonic-gate 	 * if it hasn't been explicitly tuned.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (cpu_tick_freq != 0) {
937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
947c478bd9Sstevel@tonic-gate 		    "tunable, use sys_tick_freq instead");
957c478bd9Sstevel@tonic-gate 		sys_tick_freq = cpu_tick_freq;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	if (sys_tick_freq == sys_tick_freq_save) {
987c478bd9Sstevel@tonic-gate 		cpu_init_tick_freq();
997c478bd9Sstevel@tonic-gate 		sys_tick_freq_save = sys_tick_freq;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	ASSERT(sys_tick_freq != 0);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * See the comments in clock.h for a full description of
1057c478bd9Sstevel@tonic-gate 	 * nsec_scale.  The "& ~1" operation below ensures that
1067c478bd9Sstevel@tonic-gate 	 * nsec_scale is always even, so that for *any* value of
1077c478bd9Sstevel@tonic-gate 	 * %tick, multiplying by nsec_scale clears NPT for free.
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
1107c478bd9Sstevel@tonic-gate 	    sys_tick_freq) & ~1;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
1147c478bd9Sstevel@tonic-gate 	 * version of sys_clock_mhz that we used to program the tick
1157c478bd9Sstevel@tonic-gate 	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
1167c478bd9Sstevel@tonic-gate 	 * instead of some whole number like 143
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	scaled_clock_mhz = (sys_tick_freq) / 1000;
1207c478bd9Sstevel@tonic-gate 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	nsec_per_sys_tick = NANOSEC / sys_tick_freq;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * Pre-calculate number of sticks per usec for drv_usecwait.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (sys_clock_mhz <= 0) {
1307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid system frequency");
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
13489a8ba83Sarao /*
13589a8ba83Sarao  * Hypervisor can return one of two error conditions
13689a8ba83Sarao  * for the TOD_GET API call. 1) H_ENOTSUPPORTED 2) H_EWOULDBLOCK
13789a8ba83Sarao  *
13889a8ba83Sarao  * To handle the H_ENOTSUPPORTED we return 0 seconds and let clkset
13989a8ba83Sarao  * set tod_broken.
14089a8ba83Sarao  * To handle the H_EWOULDBLOCK we retry for about 500usec and
14189a8ba83Sarao  * return hrestime if we can't successfully get a value.
14289a8ba83Sarao  */
1437c478bd9Sstevel@tonic-gate timestruc_t
1447c478bd9Sstevel@tonic-gate tod_get(void)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	timestruc_t ts;
1477c478bd9Sstevel@tonic-gate 	uint64_t seconds;
14889a8ba83Sarao 	int i;
14989a8ba83Sarao 	unsigned int spl_old;
15089a8ba83Sarao 	uint64_t ret;
1513c431bb5Swentaoy 
1523c431bb5Swentaoy 	/*
15389a8ba83Sarao 	 * Make sure we don't get preempted
15489a8ba83Sarao 	 * while getting the tod value.
15589a8ba83Sarao 	 * getting preempted could mean we always
15689a8ba83Sarao 	 * hit the hypervisor during an update
15789a8ba83Sarao 	 * and always get EWOULDBLOCK.
15889a8ba83Sarao 	 */
1597c478bd9Sstevel@tonic-gate 
16089a8ba83Sarao 	spl_old = ddi_enter_critical();
16189a8ba83Sarao 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
16289a8ba83Sarao 		ret = hv_tod_get(&seconds);
16389a8ba83Sarao 
16489a8ba83Sarao 		if (ret != H_EWOULDBLOCK)
16589a8ba83Sarao 			break;
16689a8ba83Sarao 		drv_usecwait(HV_TOD_WAIT_USEC);
16789a8ba83Sarao 	}
16889a8ba83Sarao 	ddi_exit_critical(spl_old);
16989a8ba83Sarao 
1707c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
17189a8ba83Sarao 	if (ret != H_EOK) {
17289a8ba83Sarao 
17389a8ba83Sarao 		switch (ret) {
17489a8ba83Sarao 		default:
17589a8ba83Sarao 			cmn_err(CE_WARN,
17689a8ba83Sarao 			    "tod_get: unknown error from hv_tod_get, %lx\n",
17789a8ba83Sarao 			    ret);
17889a8ba83Sarao 			/*FALLTHRU*/
17989a8ba83Sarao 		case H_EWOULDBLOCK:
18089a8ba83Sarao 			/*
18189a8ba83Sarao 			 * We timed out
18289a8ba83Sarao 			 */
183*8fc99e42STrevor Thompson 			tod_status_set(TOD_GET_FAILED);
18489a8ba83Sarao 			ts.tv_sec = tod_validate(hrestime.tv_sec);
18589a8ba83Sarao 			break;
18689a8ba83Sarao 
18789a8ba83Sarao 		case H_ENOTSUPPORTED:
18889a8ba83Sarao 			ts.tv_sec = 0;
18989a8ba83Sarao 			break;
19089a8ba83Sarao 		};
19189a8ba83Sarao 	} else {
19289a8ba83Sarao 		ts.tv_sec = tod_validate(seconds);
19389a8ba83Sarao 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (ts);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
198*8fc99e42STrevor Thompson extern void tod_set_prev(timestruc_t);
199*8fc99e42STrevor Thompson 
2007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2017c478bd9Sstevel@tonic-gate void
2027c478bd9Sstevel@tonic-gate tod_set(timestruc_t ts)
2037c478bd9Sstevel@tonic-gate {
20489a8ba83Sarao 	int i;
20589a8ba83Sarao 	uint64_t ret;
2067c478bd9Sstevel@tonic-gate 
207*8fc99e42STrevor Thompson 	/* for tod_validate() */
208*8fc99e42STrevor Thompson 	tod_set_prev(ts);
209*8fc99e42STrevor Thompson 
21089a8ba83Sarao 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
21189a8ba83Sarao 		ret = hv_tod_set(ts.tv_sec);
21289a8ba83Sarao 		if (ret != H_EWOULDBLOCK)
21389a8ba83Sarao 			break;
21489a8ba83Sarao 		drv_usecwait(HV_TOD_WAIT_USEC);
21589a8ba83Sarao 	}
216*8fc99e42STrevor Thompson 
21789a8ba83Sarao 	if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK)
21889a8ba83Sarao 		cmn_err(CE_WARN,
21989a8ba83Sarao 		    "tod_set: Unknown error from hv_tod_set, err %lx", ret);
220*8fc99e42STrevor Thompson 	else
221*8fc99e42STrevor Thompson 		/* TOD was modified */
222*8fc99e42STrevor Thompson 		tod_status_set(TOD_SET_DONE);
22389a8ba83Sarao }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * The following wrappers have been added so that locking
2277c478bd9Sstevel@tonic-gate  * can be exported to platform-independent clock routines
2287c478bd9Sstevel@tonic-gate  * (ie adjtime(), clock_setttime()), via a functional interface.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate int
2317c478bd9Sstevel@tonic-gate hr_clock_lock(void)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	ushort_t s;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	CLOCK_LOCK(&s);
2367c478bd9Sstevel@tonic-gate 	return (s);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate void
2407c478bd9Sstevel@tonic-gate hr_clock_unlock(int s)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	CLOCK_UNLOCK(s);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * We don't share the trap table with the prom, so we don't need
2477c478bd9Sstevel@tonic-gate  * to enable/disable its clock.
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate void
2507c478bd9Sstevel@tonic-gate mon_clock_init(void)
2517c478bd9Sstevel@tonic-gate {}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate void
2547c478bd9Sstevel@tonic-gate mon_clock_start(void)
2557c478bd9Sstevel@tonic-gate {}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate void
2587c478bd9Sstevel@tonic-gate mon_clock_stop(void)
2597c478bd9Sstevel@tonic-gate {}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate void
2627c478bd9Sstevel@tonic-gate mon_clock_share(void)
2637c478bd9Sstevel@tonic-gate {}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate void
2667c478bd9Sstevel@tonic-gate mon_clock_unshare(void)
2677c478bd9Sstevel@tonic-gate {}
268