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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/param.h> 27 #include <sys/time.h> 28 #include <sys/systm.h> 29 #include <sys/cmn_err.h> 30 #include <sys/debug.h> 31 #include <sys/clock.h> 32 #include <sys/intreg.h> 33 #include <sys/x_call.h> 34 #include <sys/cpuvar.h> 35 #include <sys/promif.h> 36 #include <sys/mman.h> 37 #include <sys/sysmacros.h> 38 #include <sys/lockstat.h> 39 #include <vm/as.h> 40 #include <vm/hat.h> 41 #include <sys/intr.h> 42 #include <sys/ivintr.h> 43 #include <sys/machsystm.h> 44 #include <sys/reboot.h> 45 #include <sys/membar.h> 46 #include <sys/atomic.h> 47 #include <sys/cpu_module.h> 48 #include <sys/hypervisor_api.h> 49 #include <sys/wdt.h> 50 51 uint_t sys_clock_mhz = 0; 52 uint64_t sys_tick_freq = 0; 53 uint_t cpu_tick_freq = 0; /* deprecated, tune sys_tick_freq instead */ 54 uint_t scaled_clock_mhz = 0; 55 uint_t nsec_per_sys_tick; 56 uint_t sticks_per_usec; 57 char clock_started = 0; 58 59 void 60 clkstart(void) 61 { 62 /* 63 * Now is a good time to activate hardware watchdog. 64 */ 65 watchdog_init(); 66 } 67 68 /* 69 * preset the delay constant for drv_usecwait(). This is done for early 70 * use of the le or scsi drivers in the kernel. The default contant 71 * might be too high early on. We can get a pretty good approximation 72 * of this by setting it as: 73 * 74 * sys_clock_mhz = (sys_tick_freq + 500000) / 1000000 75 * 76 * setcpudelay is called twice during the boot process. The first time 77 * is before the TOD driver is loaded so cpu_init_tick_freq cannot 78 * calibrate sys_tick_freq but can only set it to the prom value. The 79 * first call is also before /etc/system is read. 80 * 81 * Only call cpu_init_tick_freq the second time around if sys_tick_freq 82 * has not been tuned via /etc/system. 83 */ 84 void 85 setcpudelay(void) 86 { 87 static uint64_t sys_tick_freq_save = 0; 88 /* 89 * We want to allow cpu_tick_freq to be tunable; we'll only set it 90 * if it hasn't been explicitly tuned. 91 */ 92 if (cpu_tick_freq != 0) { 93 cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel " 94 "tunable, use sys_tick_freq instead"); 95 sys_tick_freq = cpu_tick_freq; 96 } 97 if (sys_tick_freq == sys_tick_freq_save) { 98 cpu_init_tick_freq(); 99 sys_tick_freq_save = sys_tick_freq; 100 } 101 ASSERT(sys_tick_freq != 0); 102 103 /* 104 * See the comments in clock.h for a full description of 105 * nsec_scale. The "& ~1" operation below ensures that 106 * nsec_scale is always even, so that for *any* value of 107 * %tick, multiplying by nsec_scale clears NPT for free. 108 */ 109 nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) / 110 sys_tick_freq) & ~1; 111 112 /* 113 * scaled_clock_mhz is a more accurated (ie not rounded-off) 114 * version of sys_clock_mhz that we used to program the tick 115 * compare register. Just in case sys_tick_freq is like 142.5 Mhz 116 * instead of some whole number like 143 117 */ 118 119 scaled_clock_mhz = (sys_tick_freq) / 1000; 120 sys_clock_mhz = (sys_tick_freq + 500000) / 1000000; 121 122 nsec_per_sys_tick = NANOSEC / sys_tick_freq; 123 124 /* 125 * Pre-calculate number of sticks per usec for drv_usecwait. 126 */ 127 sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1); 128 129 if (sys_clock_mhz <= 0) { 130 cmn_err(CE_WARN, "invalid system frequency"); 131 } 132 } 133 134 /* 135 * Hypervisor can return one of two error conditions 136 * for the TOD_GET API call. 1) H_ENOTSUPPORTED 2) H_EWOULDBLOCK 137 * 138 * To handle the H_ENOTSUPPORTED we return 0 seconds and let clkset 139 * set tod_broken. 140 * To handle the H_EWOULDBLOCK we retry for about 500usec and 141 * return hrestime if we can't successfully get a value. 142 */ 143 timestruc_t 144 tod_get(void) 145 { 146 timestruc_t ts; 147 uint64_t seconds; 148 int i; 149 unsigned int spl_old; 150 uint64_t ret; 151 152 /* 153 * Make sure we don't get preempted 154 * while getting the tod value. 155 * getting preempted could mean we always 156 * hit the hypervisor during an update 157 * and always get EWOULDBLOCK. 158 */ 159 160 spl_old = ddi_enter_critical(); 161 for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) { 162 ret = hv_tod_get(&seconds); 163 164 if (ret != H_EWOULDBLOCK) 165 break; 166 drv_usecwait(HV_TOD_WAIT_USEC); 167 } 168 ddi_exit_critical(spl_old); 169 170 ts.tv_nsec = 0; 171 if (ret != H_EOK) { 172 173 switch (ret) { 174 default: 175 cmn_err(CE_WARN, 176 "tod_get: unknown error from hv_tod_get, %lx\n", 177 ret); 178 /*FALLTHRU*/ 179 case H_EWOULDBLOCK: 180 /* 181 * We timed out 182 */ 183 tod_status_set(TOD_GET_FAILED); 184 ts.tv_sec = tod_validate(hrestime.tv_sec); 185 break; 186 187 case H_ENOTSUPPORTED: 188 ts.tv_sec = 0; 189 break; 190 }; 191 } else { 192 ts.tv_sec = tod_validate(seconds); 193 } 194 195 return (ts); 196 } 197 198 extern void tod_set_prev(timestruc_t); 199 200 /*ARGSUSED*/ 201 void 202 tod_set(timestruc_t ts) 203 { 204 int i; 205 uint64_t ret; 206 207 /* for tod_validate() */ 208 tod_set_prev(ts); 209 210 for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) { 211 ret = hv_tod_set(ts.tv_sec); 212 if (ret != H_EWOULDBLOCK) 213 break; 214 drv_usecwait(HV_TOD_WAIT_USEC); 215 } 216 217 if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK) 218 cmn_err(CE_WARN, 219 "tod_set: Unknown error from hv_tod_set, err %lx", ret); 220 else 221 /* TOD was modified */ 222 tod_status_set(TOD_SET_DONE); 223 } 224 225 /* 226 * The following wrappers have been added so that locking 227 * can be exported to platform-independent clock routines 228 * (ie adjtime(), clock_setttime()), via a functional interface. 229 */ 230 int 231 hr_clock_lock(void) 232 { 233 ushort_t s; 234 235 CLOCK_LOCK(&s); 236 return (s); 237 } 238 239 void 240 hr_clock_unlock(int s) 241 { 242 CLOCK_UNLOCK(s); 243 } 244 245 /* 246 * We don't share the trap table with the prom, so we don't need 247 * to enable/disable its clock. 248 */ 249 void 250 mon_clock_init(void) 251 {} 252 253 void 254 mon_clock_start(void) 255 {} 256 257 void 258 mon_clock_stop(void) 259 {} 260 261 void 262 mon_clock_share(void) 263 {} 264 265 void 266 mon_clock_unshare(void) 267 {} 268