xref: /titanic_53/usr/src/uts/sun4v/io/hardclk.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/clock.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/intreg.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
42*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
43*7c478bd9Sstevel@tonic-gate #include <vm/hat.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/intr.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/ivintr.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/membar.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/hypervisor_api.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate uint_t sys_clock_mhz = 0;
54*7c478bd9Sstevel@tonic-gate uint64_t sys_tick_freq = 0;
55*7c478bd9Sstevel@tonic-gate uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
56*7c478bd9Sstevel@tonic-gate uint_t scaled_clock_mhz = 0;
57*7c478bd9Sstevel@tonic-gate uint_t nsec_per_sys_tick;
58*7c478bd9Sstevel@tonic-gate uint_t sticks_per_usec;
59*7c478bd9Sstevel@tonic-gate char clock_started = 0;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate void
62*7c478bd9Sstevel@tonic-gate clkstart(void)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * preset the delay constant for drv_usecwait(). This is done for early
68*7c478bd9Sstevel@tonic-gate  * use of the le or scsi drivers in the kernel. The default contant
69*7c478bd9Sstevel@tonic-gate  * might be too high early on. We can get a pretty good approximation
70*7c478bd9Sstevel@tonic-gate  * of this by setting it as:
71*7c478bd9Sstevel@tonic-gate  *
72*7c478bd9Sstevel@tonic-gate  * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  * setcpudelay is called twice during the boot process. The first time
75*7c478bd9Sstevel@tonic-gate  * is before the TOD driver is loaded so cpu_init_tick_freq cannot
76*7c478bd9Sstevel@tonic-gate  * calibrate sys_tick_freq but can only set it to the prom value. The
77*7c478bd9Sstevel@tonic-gate  * first call is also before /etc/system is read.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * Only call cpu_init_tick_freq the second time around if sys_tick_freq
80*7c478bd9Sstevel@tonic-gate  * has not been tuned via /etc/system.
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate void
83*7c478bd9Sstevel@tonic-gate setcpudelay(void)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	static uint64_t sys_tick_freq_save = 0;
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
88*7c478bd9Sstevel@tonic-gate 	 * if it hasn't been explicitly tuned.
89*7c478bd9Sstevel@tonic-gate 	 */
90*7c478bd9Sstevel@tonic-gate 	if (cpu_tick_freq != 0) {
91*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
92*7c478bd9Sstevel@tonic-gate 		    "tunable, use sys_tick_freq instead");
93*7c478bd9Sstevel@tonic-gate 		sys_tick_freq = cpu_tick_freq;
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	if (sys_tick_freq == sys_tick_freq_save) {
96*7c478bd9Sstevel@tonic-gate 		cpu_init_tick_freq();
97*7c478bd9Sstevel@tonic-gate 		sys_tick_freq_save = sys_tick_freq;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	ASSERT(sys_tick_freq != 0);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * See the comments in clock.h for a full description of
103*7c478bd9Sstevel@tonic-gate 	 * nsec_scale.  The "& ~1" operation below ensures that
104*7c478bd9Sstevel@tonic-gate 	 * nsec_scale is always even, so that for *any* value of
105*7c478bd9Sstevel@tonic-gate 	 * %tick, multiplying by nsec_scale clears NPT for free.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
108*7c478bd9Sstevel@tonic-gate 	    sys_tick_freq) & ~1;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/*
111*7c478bd9Sstevel@tonic-gate 	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
112*7c478bd9Sstevel@tonic-gate 	 * version of sys_clock_mhz that we used to program the tick
113*7c478bd9Sstevel@tonic-gate 	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
114*7c478bd9Sstevel@tonic-gate 	 * instead of some whole number like 143
115*7c478bd9Sstevel@tonic-gate 	 */
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	scaled_clock_mhz = (sys_tick_freq) / 1000;
118*7c478bd9Sstevel@tonic-gate 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	nsec_per_sys_tick = NANOSEC / sys_tick_freq;
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	/*
123*7c478bd9Sstevel@tonic-gate 	 * Pre-calculate number of sticks per usec for drv_usecwait.
124*7c478bd9Sstevel@tonic-gate 	 */
125*7c478bd9Sstevel@tonic-gate 	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (sys_clock_mhz <= 0) {
128*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid system frequency");
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate timestruc_t
133*7c478bd9Sstevel@tonic-gate tod_get(void)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	timestruc_t ts;
136*7c478bd9Sstevel@tonic-gate 	uint64_t seconds;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	(void) hv_tod_get(&seconds);
139*7c478bd9Sstevel@tonic-gate 	ts.tv_sec = seconds;
140*7c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	return (ts);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
146*7c478bd9Sstevel@tonic-gate void
147*7c478bd9Sstevel@tonic-gate tod_set(timestruc_t ts)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	(void) hv_tod_set(ts.tv_sec);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * The following wrappers have been added so that locking
155*7c478bd9Sstevel@tonic-gate  * can be exported to platform-independent clock routines
156*7c478bd9Sstevel@tonic-gate  * (ie adjtime(), clock_setttime()), via a functional interface.
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate hr_clock_lock(void)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	ushort_t s;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	CLOCK_LOCK(&s);
164*7c478bd9Sstevel@tonic-gate 	return (s);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate void
168*7c478bd9Sstevel@tonic-gate hr_clock_unlock(int s)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	CLOCK_UNLOCK(s);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * We don't share the trap table with the prom, so we don't need
175*7c478bd9Sstevel@tonic-gate  * to enable/disable its clock.
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate void
178*7c478bd9Sstevel@tonic-gate mon_clock_init(void)
179*7c478bd9Sstevel@tonic-gate {}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate void
182*7c478bd9Sstevel@tonic-gate mon_clock_start(void)
183*7c478bd9Sstevel@tonic-gate {}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate void
186*7c478bd9Sstevel@tonic-gate mon_clock_stop(void)
187*7c478bd9Sstevel@tonic-gate {}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate void
190*7c478bd9Sstevel@tonic-gate mon_clock_share(void)
191*7c478bd9Sstevel@tonic-gate {}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate void
194*7c478bd9Sstevel@tonic-gate mon_clock_unshare(void)
195*7c478bd9Sstevel@tonic-gate {}
196