10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52036Swentaoy * Common Development and Distribution License (the "License").
62036Swentaoy * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11752STrevor.Thompson@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/time.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/clock.h>
320Sstevel@tonic-gate #include <sys/intreg.h>
330Sstevel@tonic-gate #include <sys/x_call.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/promif.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/lockstat.h>
390Sstevel@tonic-gate #include <vm/as.h>
400Sstevel@tonic-gate #include <vm/hat.h>
410Sstevel@tonic-gate #include <sys/intr.h>
420Sstevel@tonic-gate #include <sys/ivintr.h>
430Sstevel@tonic-gate #include <sys/machsystm.h>
440Sstevel@tonic-gate #include <sys/reboot.h>
450Sstevel@tonic-gate #include <sys/membar.h>
460Sstevel@tonic-gate #include <sys/atomic.h>
470Sstevel@tonic-gate #include <sys/cpu_module.h>
480Sstevel@tonic-gate #include <sys/hypervisor_api.h>
492036Swentaoy #include <sys/wdt.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate uint_t sys_clock_mhz = 0;
520Sstevel@tonic-gate uint64_t sys_tick_freq = 0;
530Sstevel@tonic-gate uint_t cpu_tick_freq = 0; /* deprecated, tune sys_tick_freq instead */
540Sstevel@tonic-gate uint_t scaled_clock_mhz = 0;
550Sstevel@tonic-gate uint_t nsec_per_sys_tick;
560Sstevel@tonic-gate uint_t sticks_per_usec;
570Sstevel@tonic-gate char clock_started = 0;
580Sstevel@tonic-gate
590Sstevel@tonic-gate void
clkstart(void)600Sstevel@tonic-gate clkstart(void)
610Sstevel@tonic-gate {
622036Swentaoy /*
632036Swentaoy * Now is a good time to activate hardware watchdog.
642036Swentaoy */
652036Swentaoy watchdog_init();
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * preset the delay constant for drv_usecwait(). This is done for early
700Sstevel@tonic-gate * use of the le or scsi drivers in the kernel. The default contant
710Sstevel@tonic-gate * might be too high early on. We can get a pretty good approximation
720Sstevel@tonic-gate * of this by setting it as:
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * setcpudelay is called twice during the boot process. The first time
770Sstevel@tonic-gate * is before the TOD driver is loaded so cpu_init_tick_freq cannot
780Sstevel@tonic-gate * calibrate sys_tick_freq but can only set it to the prom value. The
790Sstevel@tonic-gate * first call is also before /etc/system is read.
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * Only call cpu_init_tick_freq the second time around if sys_tick_freq
820Sstevel@tonic-gate * has not been tuned via /etc/system.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate void
setcpudelay(void)850Sstevel@tonic-gate setcpudelay(void)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate static uint64_t sys_tick_freq_save = 0;
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * We want to allow cpu_tick_freq to be tunable; we'll only set it
900Sstevel@tonic-gate * if it hasn't been explicitly tuned.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate if (cpu_tick_freq != 0) {
930Sstevel@tonic-gate cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
940Sstevel@tonic-gate "tunable, use sys_tick_freq instead");
950Sstevel@tonic-gate sys_tick_freq = cpu_tick_freq;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate if (sys_tick_freq == sys_tick_freq_save) {
980Sstevel@tonic-gate cpu_init_tick_freq();
990Sstevel@tonic-gate sys_tick_freq_save = sys_tick_freq;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate ASSERT(sys_tick_freq != 0);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * See the comments in clock.h for a full description of
1050Sstevel@tonic-gate * nsec_scale. The "& ~1" operation below ensures that
1060Sstevel@tonic-gate * nsec_scale is always even, so that for *any* value of
1070Sstevel@tonic-gate * %tick, multiplying by nsec_scale clears NPT for free.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
1100Sstevel@tonic-gate sys_tick_freq) & ~1;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * scaled_clock_mhz is a more accurated (ie not rounded-off)
1140Sstevel@tonic-gate * version of sys_clock_mhz that we used to program the tick
1150Sstevel@tonic-gate * compare register. Just in case sys_tick_freq is like 142.5 Mhz
1160Sstevel@tonic-gate * instead of some whole number like 143
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate scaled_clock_mhz = (sys_tick_freq) / 1000;
1200Sstevel@tonic-gate sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate nsec_per_sys_tick = NANOSEC / sys_tick_freq;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * Pre-calculate number of sticks per usec for drv_usecwait.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (sys_clock_mhz <= 0) {
1300Sstevel@tonic-gate cmn_err(CE_WARN, "invalid system frequency");
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
13420Sarao /*
13520Sarao * Hypervisor can return one of two error conditions
13620Sarao * for the TOD_GET API call. 1) H_ENOTSUPPORTED 2) H_EWOULDBLOCK
13720Sarao *
13820Sarao * To handle the H_ENOTSUPPORTED we return 0 seconds and let clkset
13920Sarao * set tod_broken.
14020Sarao * To handle the H_EWOULDBLOCK we retry for about 500usec and
14120Sarao * return hrestime if we can't successfully get a value.
14220Sarao */
1430Sstevel@tonic-gate timestruc_t
tod_get(void)1440Sstevel@tonic-gate tod_get(void)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate timestruc_t ts;
1470Sstevel@tonic-gate uint64_t seconds;
14820Sarao int i;
14920Sarao unsigned int spl_old;
15020Sarao uint64_t ret;
1512036Swentaoy
1522036Swentaoy /*
15320Sarao * Make sure we don't get preempted
15420Sarao * while getting the tod value.
15520Sarao * getting preempted could mean we always
15620Sarao * hit the hypervisor during an update
15720Sarao * and always get EWOULDBLOCK.
15820Sarao */
1590Sstevel@tonic-gate
16020Sarao spl_old = ddi_enter_critical();
16120Sarao for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
16220Sarao ret = hv_tod_get(&seconds);
16320Sarao
16420Sarao if (ret != H_EWOULDBLOCK)
16520Sarao break;
16620Sarao drv_usecwait(HV_TOD_WAIT_USEC);
16720Sarao }
16820Sarao ddi_exit_critical(spl_old);
16920Sarao
1700Sstevel@tonic-gate ts.tv_nsec = 0;
17120Sarao if (ret != H_EOK) {
17220Sarao
17320Sarao switch (ret) {
17420Sarao default:
17520Sarao cmn_err(CE_WARN,
17620Sarao "tod_get: unknown error from hv_tod_get, %lx\n",
17720Sarao ret);
17820Sarao /*FALLTHRU*/
17920Sarao case H_EWOULDBLOCK:
18020Sarao /*
18120Sarao * We timed out
18220Sarao */
183*11752STrevor.Thompson@Sun.COM tod_status_set(TOD_GET_FAILED);
18420Sarao ts.tv_sec = tod_validate(hrestime.tv_sec);
18520Sarao break;
18620Sarao
18720Sarao case H_ENOTSUPPORTED:
18820Sarao ts.tv_sec = 0;
18920Sarao break;
19020Sarao };
19120Sarao } else {
19220Sarao ts.tv_sec = tod_validate(seconds);
19320Sarao }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate return (ts);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
198*11752STrevor.Thompson@Sun.COM extern void tod_set_prev(timestruc_t);
199*11752STrevor.Thompson@Sun.COM
2000Sstevel@tonic-gate /*ARGSUSED*/
2010Sstevel@tonic-gate void
tod_set(timestruc_t ts)2020Sstevel@tonic-gate tod_set(timestruc_t ts)
2030Sstevel@tonic-gate {
20420Sarao int i;
20520Sarao uint64_t ret;
20620Sarao
207*11752STrevor.Thompson@Sun.COM /* for tod_validate() */
208*11752STrevor.Thompson@Sun.COM tod_set_prev(ts);
209*11752STrevor.Thompson@Sun.COM
21020Sarao for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
21120Sarao ret = hv_tod_set(ts.tv_sec);
21220Sarao if (ret != H_EWOULDBLOCK)
21320Sarao break;
21420Sarao drv_usecwait(HV_TOD_WAIT_USEC);
21520Sarao }
216*11752STrevor.Thompson@Sun.COM
21720Sarao if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK)
21820Sarao cmn_err(CE_WARN,
21920Sarao "tod_set: Unknown error from hv_tod_set, err %lx", ret);
220*11752STrevor.Thompson@Sun.COM else
221*11752STrevor.Thompson@Sun.COM /* TOD was modified */
222*11752STrevor.Thompson@Sun.COM tod_status_set(TOD_SET_DONE);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * The following wrappers have been added so that locking
2270Sstevel@tonic-gate * can be exported to platform-independent clock routines
2280Sstevel@tonic-gate * (ie adjtime(), clock_setttime()), via a functional interface.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate int
hr_clock_lock(void)2310Sstevel@tonic-gate hr_clock_lock(void)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate ushort_t s;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate CLOCK_LOCK(&s);
2360Sstevel@tonic-gate return (s);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate void
hr_clock_unlock(int s)2400Sstevel@tonic-gate hr_clock_unlock(int s)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate CLOCK_UNLOCK(s);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * We don't share the trap table with the prom, so we don't need
2470Sstevel@tonic-gate * to enable/disable its clock.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate void
mon_clock_init(void)2500Sstevel@tonic-gate mon_clock_init(void)
2510Sstevel@tonic-gate {}
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate void
mon_clock_start(void)2540Sstevel@tonic-gate mon_clock_start(void)
2550Sstevel@tonic-gate {}
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate void
mon_clock_stop(void)2580Sstevel@tonic-gate mon_clock_stop(void)
2590Sstevel@tonic-gate {}
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate void
mon_clock_share(void)2620Sstevel@tonic-gate mon_clock_share(void)
2630Sstevel@tonic-gate {}
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate void
mon_clock_unshare(void)2660Sstevel@tonic-gate mon_clock_unshare(void)
2670Sstevel@tonic-gate {}
268