xref: /titanic_41/usr/src/uts/sun4v/io/hardclk.c (revision fef1e07ef354c2dcda4dc397c33f5a5532432c7a)
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 2006 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 	 * Pat the watchdog timer regularly.
156 	 */
157 	watchdog_pat();
158 
159 	/*
160 	 * Make sure we don't get preempted
161 	 * while getting the tod value.
162 	 * getting preempted could mean we always
163 	 * hit the hypervisor during an update
164 	 * and always get EWOULDBLOCK.
165 	 */
166 
167 	spl_old = ddi_enter_critical();
168 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
169 		ret = hv_tod_get(&seconds);
170 
171 		if (ret != H_EWOULDBLOCK)
172 			break;
173 		drv_usecwait(HV_TOD_WAIT_USEC);
174 	}
175 	ddi_exit_critical(spl_old);
176 
177 	ts.tv_nsec = 0;
178 	if (ret != H_EOK) {
179 
180 		switch (ret) {
181 		default:
182 			cmn_err(CE_WARN,
183 			    "tod_get: unknown error from hv_tod_get, %lx\n",
184 			    ret);
185 			/*FALLTHRU*/
186 		case H_EWOULDBLOCK:
187 			/*
188 			 * We timed out
189 			 */
190 			tod_fault_reset();
191 			ts.tv_sec = tod_validate(hrestime.tv_sec);
192 			break;
193 
194 		case H_ENOTSUPPORTED:
195 			ts.tv_sec = 0;
196 			break;
197 		};
198 	} else {
199 		ts.tv_sec = tod_validate(seconds);
200 	}
201 
202 	return (ts);
203 }
204 
205 /*ARGSUSED*/
206 void
207 tod_set(timestruc_t ts)
208 {
209 	int i;
210 	uint64_t ret;
211 
212 	tod_fault_reset();
213 	for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
214 		ret = hv_tod_set(ts.tv_sec);
215 		if (ret != H_EWOULDBLOCK)
216 			break;
217 		drv_usecwait(HV_TOD_WAIT_USEC);
218 	}
219 	if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK)
220 		cmn_err(CE_WARN,
221 		    "tod_set: Unknown error from hv_tod_set, err %lx", ret);
222 }
223 
224 /*
225  * The following wrappers have been added so that locking
226  * can be exported to platform-independent clock routines
227  * (ie adjtime(), clock_setttime()), via a functional interface.
228  */
229 int
230 hr_clock_lock(void)
231 {
232 	ushort_t s;
233 
234 	CLOCK_LOCK(&s);
235 	return (s);
236 }
237 
238 void
239 hr_clock_unlock(int s)
240 {
241 	CLOCK_UNLOCK(s);
242 }
243 
244 /*
245  * We don't share the trap table with the prom, so we don't need
246  * to enable/disable its clock.
247  */
248 void
249 mon_clock_init(void)
250 {}
251 
252 void
253 mon_clock_start(void)
254 {}
255 
256 void
257 mon_clock_stop(void)
258 {}
259 
260 void
261 mon_clock_share(void)
262 {}
263 
264 void
265 mon_clock_unshare(void)
266 {}
267