1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/clock.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/intreg.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/x_call.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/promif.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/lockstat.h> 42*7c478bd9Sstevel@tonic-gate #include <vm/as.h> 43*7c478bd9Sstevel@tonic-gate #include <vm/hat.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/intr.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/ivintr.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/membar.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h> 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate uint_t sys_clock_mhz = 0; 53*7c478bd9Sstevel@tonic-gate uint64_t sys_tick_freq = 0; 54*7c478bd9Sstevel@tonic-gate uint_t cpu_tick_freq = 0; /* deprecated, tune sys_tick_freq instead */ 55*7c478bd9Sstevel@tonic-gate uint_t scaled_clock_mhz = 0; 56*7c478bd9Sstevel@tonic-gate uint_t nsec_per_sys_tick; 57*7c478bd9Sstevel@tonic-gate uint_t sticks_per_usec; 58*7c478bd9Sstevel@tonic-gate char clock_started = 0; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate /* 61*7c478bd9Sstevel@tonic-gate * Hardware watchdog parameters and knobs 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate int watchdog_enable = 0; /* user knob */ 64*7c478bd9Sstevel@tonic-gate int watchdog_available = 0; /* system has a watchdog */ 65*7c478bd9Sstevel@tonic-gate int watchdog_activated = 0; /* the watchdog is armed */ 66*7c478bd9Sstevel@tonic-gate uint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * tod module name and operations 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate struct tod_ops tod_ops; 72*7c478bd9Sstevel@tonic-gate char *tod_module_name; 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate void 76*7c478bd9Sstevel@tonic-gate clkstart(void) 77*7c478bd9Sstevel@tonic-gate { 78*7c478bd9Sstevel@tonic-gate int ret = 0; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate /* 81*7c478bd9Sstevel@tonic-gate * Now is a good time to activate hardware watchdog (if one exists). 82*7c478bd9Sstevel@tonic-gate */ 83*7c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 84*7c478bd9Sstevel@tonic-gate if (watchdog_enable) 85*7c478bd9Sstevel@tonic-gate ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds); 86*7c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock); 87*7c478bd9Sstevel@tonic-gate if (ret != 0) 88*7c478bd9Sstevel@tonic-gate printf("Hardware watchdog enabled\n"); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate /* 92*7c478bd9Sstevel@tonic-gate * preset the delay constant for drv_usecwait(). This is done for early 93*7c478bd9Sstevel@tonic-gate * use of the le or scsi drivers in the kernel. The default contant 94*7c478bd9Sstevel@tonic-gate * might be too high early on. We can get a pretty good approximation 95*7c478bd9Sstevel@tonic-gate * of this by setting it as: 96*7c478bd9Sstevel@tonic-gate * 97*7c478bd9Sstevel@tonic-gate * sys_clock_mhz = (sys_tick_freq + 500000) / 1000000 98*7c478bd9Sstevel@tonic-gate * 99*7c478bd9Sstevel@tonic-gate * setcpudelay is called twice during the boot process. The first time 100*7c478bd9Sstevel@tonic-gate * is before the TOD driver is loaded so cpu_init_tick_freq cannot 101*7c478bd9Sstevel@tonic-gate * calibrate sys_tick_freq but can only set it to the prom value. The 102*7c478bd9Sstevel@tonic-gate * first call is also before /etc/system is read. 103*7c478bd9Sstevel@tonic-gate * 104*7c478bd9Sstevel@tonic-gate * Only call cpu_init_tick_freq the second time around if sys_tick_freq 105*7c478bd9Sstevel@tonic-gate * has not been tuned via /etc/system. 106*7c478bd9Sstevel@tonic-gate */ 107*7c478bd9Sstevel@tonic-gate void 108*7c478bd9Sstevel@tonic-gate setcpudelay(void) 109*7c478bd9Sstevel@tonic-gate { 110*7c478bd9Sstevel@tonic-gate static uint64_t sys_tick_freq_save = 0; 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate * We want to allow cpu_tick_freq to be tunable; we'll only set it 113*7c478bd9Sstevel@tonic-gate * if it hasn't been explicitly tuned. 114*7c478bd9Sstevel@tonic-gate */ 115*7c478bd9Sstevel@tonic-gate if (cpu_tick_freq != 0) { 116*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel " 117*7c478bd9Sstevel@tonic-gate "tunable, use sys_tick_freq instead"); 118*7c478bd9Sstevel@tonic-gate sys_tick_freq = cpu_tick_freq; 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate if (sys_tick_freq == sys_tick_freq_save) { 121*7c478bd9Sstevel@tonic-gate cpu_init_tick_freq(); 122*7c478bd9Sstevel@tonic-gate sys_tick_freq_save = sys_tick_freq; 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate ASSERT(sys_tick_freq != 0); 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate /* 127*7c478bd9Sstevel@tonic-gate * See the comments in clock.h for a full description of 128*7c478bd9Sstevel@tonic-gate * nsec_scale. The "& ~1" operation below ensures that 129*7c478bd9Sstevel@tonic-gate * nsec_scale is always even, so that for *any* value of 130*7c478bd9Sstevel@tonic-gate * %tick, multiplying by nsec_scale clears NPT for free. 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) / 133*7c478bd9Sstevel@tonic-gate sys_tick_freq) & ~1; 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate * scaled_clock_mhz is a more accurated (ie not rounded-off) 137*7c478bd9Sstevel@tonic-gate * version of sys_clock_mhz that we used to program the tick 138*7c478bd9Sstevel@tonic-gate * compare register. Just in case sys_tick_freq is like 142.5 Mhz 139*7c478bd9Sstevel@tonic-gate * instead of some whole number like 143 140*7c478bd9Sstevel@tonic-gate */ 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate scaled_clock_mhz = (sys_tick_freq) / 1000; 143*7c478bd9Sstevel@tonic-gate sys_clock_mhz = (sys_tick_freq + 500000) / 1000000; 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate nsec_per_sys_tick = NANOSEC / sys_tick_freq; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * Pre-calculate number of sticks per usec for drv_usecwait. 149*7c478bd9Sstevel@tonic-gate */ 150*7c478bd9Sstevel@tonic-gate sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1); 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate if (sys_clock_mhz <= 0) { 153*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "invalid system frequency"); 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate timestruc_t 158*7c478bd9Sstevel@tonic-gate tod_get(void) 159*7c478bd9Sstevel@tonic-gate { 160*7c478bd9Sstevel@tonic-gate timestruc_t ts = tod_ops.tod_get(); 161*7c478bd9Sstevel@tonic-gate ts.tv_sec = tod_validate(ts.tv_sec); 162*7c478bd9Sstevel@tonic-gate return (ts); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate void 166*7c478bd9Sstevel@tonic-gate tod_set(timestruc_t ts) 167*7c478bd9Sstevel@tonic-gate { 168*7c478bd9Sstevel@tonic-gate /* 169*7c478bd9Sstevel@tonic-gate * Prevent false alarm in tod_validate() due to tod value change. 170*7c478bd9Sstevel@tonic-gate */ 171*7c478bd9Sstevel@tonic-gate tod_fault_reset(); 172*7c478bd9Sstevel@tonic-gate tod_ops.tod_set(ts); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate /* 177*7c478bd9Sstevel@tonic-gate * The following wrappers have been added so that locking 178*7c478bd9Sstevel@tonic-gate * can be exported to platform-independent clock routines 179*7c478bd9Sstevel@tonic-gate * (ie adjtime(), clock_setttime()), via a functional interface. 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate int 182*7c478bd9Sstevel@tonic-gate hr_clock_lock(void) 183*7c478bd9Sstevel@tonic-gate { 184*7c478bd9Sstevel@tonic-gate ushort_t s; 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate CLOCK_LOCK(&s); 187*7c478bd9Sstevel@tonic-gate return (s); 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate void 191*7c478bd9Sstevel@tonic-gate hr_clock_unlock(int s) 192*7c478bd9Sstevel@tonic-gate { 193*7c478bd9Sstevel@tonic-gate CLOCK_UNLOCK(s); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate /* 197*7c478bd9Sstevel@tonic-gate * We don't share the trap table with the prom, so we don't need 198*7c478bd9Sstevel@tonic-gate * to enable/disable its clock. 199*7c478bd9Sstevel@tonic-gate */ 200*7c478bd9Sstevel@tonic-gate void 201*7c478bd9Sstevel@tonic-gate mon_clock_init(void) 202*7c478bd9Sstevel@tonic-gate {} 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate void 205*7c478bd9Sstevel@tonic-gate mon_clock_start(void) 206*7c478bd9Sstevel@tonic-gate {} 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate void 209*7c478bd9Sstevel@tonic-gate mon_clock_stop(void) 210*7c478bd9Sstevel@tonic-gate {} 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate void 213*7c478bd9Sstevel@tonic-gate mon_clock_share(void) 214*7c478bd9Sstevel@tonic-gate {} 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate void 217*7c478bd9Sstevel@tonic-gate mon_clock_unshare(void) 218*7c478bd9Sstevel@tonic-gate {} 219