xref: /titanic_44/usr/src/uts/sun4v/io/hardclk.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
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 /*
133  * Hypervisor can return one of two error conditions
134  * for the TOD_GET API call. 1) H_ENOTSUPPORTED 2) H_EWOULDBLOCK
135  *
136  * To handle the H_ENOTSUPPORTED we return 0 seconds and let clkset
137  * set tod_broken.
138  * To handle the H_EWOULDBLOCK we retry for about 500usec and
139  * return hrestime if we can't successfully get a value.
140  */
141 timestruc_t
142 tod_get(void)
143 {
144 	timestruc_t ts;
145 	uint64_t seconds;
146 	int i;
147 	unsigned int spl_old;
148 	uint64_t ret;
149 	/*
150 	 * Make sure we don't get preempted
151 	 * while getting the tod value.
152 	 * getting preempted could mean we always
153 	 * hit the hypervisor during an update
154 	 * and always get EWOULDBLOCK.
155 	 */
156 
157 	spl_old = ddi_enter_critical();
158 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
159 		ret = hv_tod_get(&seconds);
160 
161 		if (ret != H_EWOULDBLOCK)
162 			break;
163 		drv_usecwait(HV_TOD_WAIT_USEC);
164 	}
165 	ddi_exit_critical(spl_old);
166 
167 	ts.tv_nsec = 0;
168 	if (ret != H_EOK) {
169 
170 		switch (ret) {
171 		default:
172 			cmn_err(CE_WARN,
173 			    "tod_get: unknown error from hv_tod_get, %lx\n",
174 			    ret);
175 			/*FALLTHRU*/
176 		case H_EWOULDBLOCK:
177 			/*
178 			 * We timed out
179 			 */
180 			tod_fault_reset();
181 			ts.tv_sec = tod_validate(hrestime.tv_sec);
182 			break;
183 
184 		case H_ENOTSUPPORTED:
185 			ts.tv_sec = 0;
186 			break;
187 		};
188 	} else {
189 		ts.tv_sec = tod_validate(seconds);
190 	}
191 
192 	return (ts);
193 }
194 
195 /*ARGSUSED*/
196 void
197 tod_set(timestruc_t ts)
198 {
199 	int i;
200 	uint64_t ret;
201 
202 	tod_fault_reset();
203 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
204 		ret = hv_tod_set(ts.tv_sec);
205 		if (ret != H_EWOULDBLOCK)
206 			break;
207 		drv_usecwait(HV_TOD_WAIT_USEC);
208 	}
209 	if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK)
210 		cmn_err(CE_WARN,
211 		    "tod_set: Unknown error from hv_tod_set, err %lx", ret);
212 }
213 
214 /*
215  * The following wrappers have been added so that locking
216  * can be exported to platform-independent clock routines
217  * (ie adjtime(), clock_setttime()), via a functional interface.
218  */
219 int
220 hr_clock_lock(void)
221 {
222 	ushort_t s;
223 
224 	CLOCK_LOCK(&s);
225 	return (s);
226 }
227 
228 void
229 hr_clock_unlock(int s)
230 {
231 	CLOCK_UNLOCK(s);
232 }
233 
234 /*
235  * We don't share the trap table with the prom, so we don't need
236  * to enable/disable its clock.
237  */
238 void
239 mon_clock_init(void)
240 {}
241 
242 void
243 mon_clock_start(void)
244 {}
245 
246 void
247 mon_clock_stop(void)
248 {}
249 
250 void
251 mon_clock_share(void)
252 {}
253 
254 void
255 mon_clock_unshare(void)
256 {}
257