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