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/param.h> 30 #include <sys/time.h> 31 #include <sys/systm.h> 32 #include <sys/cmn_err.h> 33 #include <sys/debug.h> 34 #include <sys/clock.h> 35 #include <sys/intreg.h> 36 #include <sys/x_call.h> 37 #include <sys/cpuvar.h> 38 #include <sys/promif.h> 39 #include <sys/mman.h> 40 #include <sys/sysmacros.h> 41 #include <sys/lockstat.h> 42 #include <vm/as.h> 43 #include <vm/hat.h> 44 #include <sys/intr.h> 45 #include <sys/ivintr.h> 46 #include <sys/machsystm.h> 47 #include <sys/reboot.h> 48 #include <sys/membar.h> 49 #include <sys/atomic.h> 50 #include <sys/cpu_module.h> 51 #include <sys/hypervisor_api.h> 52 53 uint_t sys_clock_mhz = 0; 54 uint64_t sys_tick_freq = 0; 55 uint_t cpu_tick_freq = 0; /* deprecated, tune sys_tick_freq instead */ 56 uint_t scaled_clock_mhz = 0; 57 uint_t nsec_per_sys_tick; 58 uint_t sticks_per_usec; 59 char clock_started = 0; 60 61 void 62 clkstart(void) 63 { 64 } 65 66 /* 67 * preset the delay constant for drv_usecwait(). This is done for early 68 * use of the le or scsi drivers in the kernel. The default contant 69 * might be too high early on. We can get a pretty good approximation 70 * of this by setting it as: 71 * 72 * sys_clock_mhz = (sys_tick_freq + 500000) / 1000000 73 * 74 * setcpudelay is called twice during the boot process. The first time 75 * is before the TOD driver is loaded so cpu_init_tick_freq cannot 76 * calibrate sys_tick_freq but can only set it to the prom value. The 77 * first call is also before /etc/system is read. 78 * 79 * Only call cpu_init_tick_freq the second time around if sys_tick_freq 80 * has not been tuned via /etc/system. 81 */ 82 void 83 setcpudelay(void) 84 { 85 static uint64_t sys_tick_freq_save = 0; 86 /* 87 * We want to allow cpu_tick_freq to be tunable; we'll only set it 88 * if it hasn't been explicitly tuned. 89 */ 90 if (cpu_tick_freq != 0) { 91 cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel " 92 "tunable, use sys_tick_freq instead"); 93 sys_tick_freq = cpu_tick_freq; 94 } 95 if (sys_tick_freq == sys_tick_freq_save) { 96 cpu_init_tick_freq(); 97 sys_tick_freq_save = sys_tick_freq; 98 } 99 ASSERT(sys_tick_freq != 0); 100 101 /* 102 * See the comments in clock.h for a full description of 103 * nsec_scale. The "& ~1" operation below ensures that 104 * nsec_scale is always even, so that for *any* value of 105 * %tick, multiplying by nsec_scale clears NPT for free. 106 */ 107 nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) / 108 sys_tick_freq) & ~1; 109 110 /* 111 * scaled_clock_mhz is a more accurated (ie not rounded-off) 112 * version of sys_clock_mhz that we used to program the tick 113 * compare register. Just in case sys_tick_freq is like 142.5 Mhz 114 * instead of some whole number like 143 115 */ 116 117 scaled_clock_mhz = (sys_tick_freq) / 1000; 118 sys_clock_mhz = (sys_tick_freq + 500000) / 1000000; 119 120 nsec_per_sys_tick = NANOSEC / sys_tick_freq; 121 122 /* 123 * Pre-calculate number of sticks per usec for drv_usecwait. 124 */ 125 sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1); 126 127 if (sys_clock_mhz <= 0) { 128 cmn_err(CE_WARN, "invalid system frequency"); 129 } 130 } 131 132 timestruc_t 133 tod_get(void) 134 { 135 timestruc_t ts; 136 uint64_t seconds; 137 138 (void) hv_tod_get(&seconds); 139 ts.tv_sec = seconds; 140 ts.tv_nsec = 0; 141 142 return (ts); 143 } 144 145 /*ARGSUSED*/ 146 void 147 tod_set(timestruc_t ts) 148 { 149 (void) hv_tod_set(ts.tv_sec); 150 } 151 152 153 /* 154 * The following wrappers have been added so that locking 155 * can be exported to platform-independent clock routines 156 * (ie adjtime(), clock_setttime()), via a functional interface. 157 */ 158 int 159 hr_clock_lock(void) 160 { 161 ushort_t s; 162 163 CLOCK_LOCK(&s); 164 return (s); 165 } 166 167 void 168 hr_clock_unlock(int s) 169 { 170 CLOCK_UNLOCK(s); 171 } 172 173 /* 174 * We don't share the trap table with the prom, so we don't need 175 * to enable/disable its clock. 176 */ 177 void 178 mon_clock_init(void) 179 {} 180 181 void 182 mon_clock_start(void) 183 {} 184 185 void 186 mon_clock_stop(void) 187 {} 188 189 void 190 mon_clock_share(void) 191 {} 192 193 void 194 mon_clock_unshare(void) 195 {} 196