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