xref: /illumos-gate/usr/src/uts/sun4u/io/hardclk.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
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 
52 uint_t sys_clock_mhz = 0;
53 uint64_t sys_tick_freq = 0;
54 uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
55 uint_t scaled_clock_mhz = 0;
56 uint_t nsec_per_sys_tick;
57 uint_t sticks_per_usec;
58 char clock_started = 0;
59 
60 /*
61  * Hardware watchdog parameters and knobs
62  */
63 int watchdog_enable = 0;		/* user knob */
64 int watchdog_available = 0;		/* system has a watchdog */
65 int watchdog_activated = 0;		/* the watchdog is armed */
66 uint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;
67 
68 /*
69  * tod module name and operations
70  */
71 struct tod_ops	tod_ops;
72 char		*tod_module_name;
73 
74 
75 void
76 clkstart(void)
77 {
78 	int ret = 0;
79 
80 	/*
81 	 * Now is a good time to activate hardware watchdog (if one exists).
82 	 */
83 	mutex_enter(&tod_lock);
84 	if (watchdog_enable)
85 		ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
86 	mutex_exit(&tod_lock);
87 	if (ret != 0)
88 		printf("Hardware watchdog enabled\n");
89 }
90 
91 /*
92  * preset the delay constant for drv_usecwait(). This is done for early
93  * use of the le or scsi drivers in the kernel. The default contant
94  * might be too high early on. We can get a pretty good approximation
95  * of this by setting it as:
96  *
97  * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
98  *
99  * setcpudelay is called twice during the boot process. The first time
100  * is before the TOD driver is loaded so cpu_init_tick_freq cannot
101  * calibrate sys_tick_freq but can only set it to the prom value. The
102  * first call is also before /etc/system is read.
103  *
104  * Only call cpu_init_tick_freq the second time around if sys_tick_freq
105  * has not been tuned via /etc/system.
106  */
107 void
108 setcpudelay(void)
109 {
110 	static uint64_t sys_tick_freq_save = 0;
111 	/*
112 	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
113 	 * if it hasn't been explicitly tuned.
114 	 */
115 	if (cpu_tick_freq != 0) {
116 		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
117 		    "tunable, use sys_tick_freq instead");
118 		sys_tick_freq = cpu_tick_freq;
119 	}
120 	if (sys_tick_freq == sys_tick_freq_save) {
121 		cpu_init_tick_freq();
122 		sys_tick_freq_save = sys_tick_freq;
123 	}
124 	ASSERT(sys_tick_freq != 0);
125 
126 	/*
127 	 * See the comments in clock.h for a full description of
128 	 * nsec_scale.  The "& ~1" operation below ensures that
129 	 * nsec_scale is always even, so that for *any* value of
130 	 * %tick, multiplying by nsec_scale clears NPT for free.
131 	 */
132 	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
133 	    sys_tick_freq) & ~1;
134 
135 	/*
136 	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
137 	 * version of sys_clock_mhz that we used to program the tick
138 	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
139 	 * instead of some whole number like 143
140 	 */
141 
142 	scaled_clock_mhz = (sys_tick_freq) / 1000;
143 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;
144 
145 	nsec_per_sys_tick = NANOSEC / sys_tick_freq;
146 
147 	/*
148 	 * Pre-calculate number of sticks per usec for drv_usecwait.
149 	 */
150 	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);
151 
152 	if (sys_clock_mhz <= 0) {
153 		cmn_err(CE_WARN, "invalid system frequency");
154 	}
155 }
156 
157 timestruc_t
158 tod_get(void)
159 {
160 	timestruc_t ts = tod_ops.tod_get();
161 	ts.tv_sec = tod_validate(ts.tv_sec);
162 	return (ts);
163 }
164 
165 void
166 tod_set(timestruc_t ts)
167 {
168 	/*
169 	 * Prevent false alarm in tod_validate() due to tod value change.
170 	 */
171 	tod_fault_reset();
172 	tod_ops.tod_set(ts);
173 }
174 
175 
176 /*
177  * The following wrappers have been added so that locking
178  * can be exported to platform-independent clock routines
179  * (ie adjtime(), clock_setttime()), via a functional interface.
180  */
181 int
182 hr_clock_lock(void)
183 {
184 	ushort_t s;
185 
186 	CLOCK_LOCK(&s);
187 	return (s);
188 }
189 
190 void
191 hr_clock_unlock(int s)
192 {
193 	CLOCK_UNLOCK(s);
194 }
195 
196 /*
197  * We don't share the trap table with the prom, so we don't need
198  * to enable/disable its clock.
199  */
200 void
201 mon_clock_init(void)
202 {}
203 
204 void
205 mon_clock_start(void)
206 {}
207 
208 void
209 mon_clock_stop(void)
210 {}
211 
212 void
213 mon_clock_share(void)
214 {}
215 
216 void
217 mon_clock_unshare(void)
218 {}
219