summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/hardclk.c
blob: 1e36967fc94f497162e8c65ecb552157d3e5141e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/clock.h>
#include <sys/intreg.h>
#include <sys/x_call.h>
#include <sys/cpuvar.h>
#include <sys/promif.h>
#include <sys/mman.h>
#include <sys/sysmacros.h>
#include <sys/lockstat.h>
#include <vm/as.h>
#include <vm/hat.h>
#include <sys/intr.h>
#include <sys/ivintr.h>
#include <sys/machsystm.h>
#include <sys/reboot.h>
#include <sys/membar.h>
#include <sys/atomic.h>
#include <sys/cpu_module.h>

uint_t sys_clock_mhz = 0;
uint64_t sys_tick_freq = 0;
uint_t cpu_tick_freq = 0;	/* deprecated, tune sys_tick_freq instead */
uint_t scaled_clock_mhz = 0;
uint_t nsec_per_sys_tick;
uint_t sticks_per_usec;
char clock_started = 0;

/*
 * Hardware watchdog parameters and knobs
 */
int watchdog_enable = 0;		/* user knob */
int watchdog_available = 0;		/* system has a watchdog */
int watchdog_activated = 0;		/* the watchdog is armed */
uint_t watchdog_timeout_seconds = CLK_WATCHDOG_DEFAULT;

/*
 * tod module name and operations
 */
struct tod_ops	tod_ops;
char		*tod_module_name;


void
clkstart(void)
{
	int ret = 0;

	/*
	 * Now is a good time to activate hardware watchdog (if one exists).
	 */
	mutex_enter(&tod_lock);
	if (watchdog_enable)
		ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
	mutex_exit(&tod_lock);
	if (ret != 0)
		printf("Hardware watchdog enabled\n");
}

/*
 * preset the delay constant for drv_usecwait(). This is done for early
 * use of the le or scsi drivers in the kernel. The default contant
 * might be too high early on. We can get a pretty good approximation
 * of this by setting it as:
 *
 * 	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000
 *
 * setcpudelay is called twice during the boot process. The first time
 * is before the TOD driver is loaded so cpu_init_tick_freq cannot
 * calibrate sys_tick_freq but can only set it to the prom value. The
 * first call is also before /etc/system is read.
 *
 * Only call cpu_init_tick_freq the second time around if sys_tick_freq
 * has not been tuned via /etc/system.
 */
void
setcpudelay(void)
{
	static uint64_t sys_tick_freq_save = 0;
	/*
	 * We want to allow cpu_tick_freq to be tunable; we'll only set it
	 * if it hasn't been explicitly tuned.
	 */
	if (cpu_tick_freq != 0) {
		cmn_err(CE_WARN, "cpu_tick_freq is no longer a kernel "
		    "tunable, use sys_tick_freq instead");
		sys_tick_freq = cpu_tick_freq;
	}
	if (sys_tick_freq == sys_tick_freq_save) {
		cpu_init_tick_freq();
		sys_tick_freq_save = sys_tick_freq;
	}
	ASSERT(sys_tick_freq != 0);

	/*
	 * See the comments in clock.h for a full description of
	 * nsec_scale.  The "& ~1" operation below ensures that
	 * nsec_scale is always even, so that for *any* value of
	 * %tick, multiplying by nsec_scale clears NPT for free.
	 */
	nsec_scale = (uint_t)(((u_longlong_t)NANOSEC << (32 - nsec_shift)) /
	    sys_tick_freq) & ~1;

	/*
	 * scaled_clock_mhz is a more accurated (ie not rounded-off)
	 * version of sys_clock_mhz that we used to program the tick
	 * compare register. Just in case sys_tick_freq is like 142.5 Mhz
	 * instead of some whole number like 143
	 */

	scaled_clock_mhz = (sys_tick_freq) / 1000;
	sys_clock_mhz = (sys_tick_freq + 500000) / 1000000;

	nsec_per_sys_tick = NANOSEC / sys_tick_freq;

	/*
	 * Pre-calculate number of sticks per usec for drv_usecwait.
	 */
	sticks_per_usec = MAX((sys_tick_freq + (MICROSEC - 1)) / MICROSEC, 1);

	if (sys_clock_mhz <= 0) {
		cmn_err(CE_WARN, "invalid system frequency");
	}
}

timestruc_t
tod_get(void)
{
	timestruc_t ts = tod_ops.tod_get();
	ts.tv_sec = tod_validate(ts.tv_sec);
	return (ts);
}

void
tod_set(timestruc_t ts)
{
	/*
	 * Prevent false alarm in tod_validate() due to tod value change.
	 */
	tod_fault_reset();
	tod_ops.tod_set(ts);
}


/*
 * The following wrappers have been added so that locking
 * can be exported to platform-independent clock routines
 * (ie adjtime(), clock_setttime()), via a functional interface.
 */
int
hr_clock_lock(void)
{
	ushort_t s;

	CLOCK_LOCK(&s);
	return (s);
}

void
hr_clock_unlock(int s)
{
	CLOCK_UNLOCK(s);
}

/*
 * We don't share the trap table with the prom, so we don't need
 * to enable/disable its clock.
 */
void
mon_clock_init(void)
{}

void
mon_clock_start(void)
{}

void
mon_clock_stop(void)
{}

void
mon_clock_share(void)
{}

void
mon_clock_unshare(void)
{}