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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#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>
#include <sys/hypervisor_api.h>
#include <sys/wdt.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;
void
clkstart(void)
{
/*
* Now is a good time to activate hardware watchdog.
*/
watchdog_init();
}
/*
* 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");
}
}
/*
* Hypervisor can return one of two error conditions
* for the TOD_GET API call. 1) H_ENOTSUPPORTED 2) H_EWOULDBLOCK
*
* To handle the H_ENOTSUPPORTED we return 0 seconds and let clkset
* set tod_broken.
* To handle the H_EWOULDBLOCK we retry for about 500usec and
* return hrestime if we can't successfully get a value.
*/
timestruc_t
tod_get(void)
{
timestruc_t ts;
uint64_t seconds;
int i;
unsigned int spl_old;
uint64_t ret;
/*
* Make sure we don't get preempted
* while getting the tod value.
* getting preempted could mean we always
* hit the hypervisor during an update
* and always get EWOULDBLOCK.
*/
spl_old = ddi_enter_critical();
for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
ret = hv_tod_get(&seconds);
if (ret != H_EWOULDBLOCK)
break;
drv_usecwait(HV_TOD_WAIT_USEC);
}
ddi_exit_critical(spl_old);
ts.tv_nsec = 0;
if (ret != H_EOK) {
switch (ret) {
default:
cmn_err(CE_WARN,
"tod_get: unknown error from hv_tod_get, %lx\n",
ret);
/*FALLTHRU*/
case H_EWOULDBLOCK:
/*
* We timed out
*/
tod_status_set(TOD_GET_FAILED);
ts.tv_sec = tod_validate(hrestime.tv_sec);
break;
case H_ENOTSUPPORTED:
ts.tv_sec = 0;
break;
};
} else {
ts.tv_sec = tod_validate(seconds);
}
return (ts);
}
extern void tod_set_prev(timestruc_t);
/*ARGSUSED*/
void
tod_set(timestruc_t ts)
{
int i;
uint64_t ret;
/* for tod_validate() */
tod_set_prev(ts);
for (i = 0; i <= HV_TOD_RETRY_THRESH; i++) {
ret = hv_tod_set(ts.tv_sec);
if (ret != H_EWOULDBLOCK)
break;
drv_usecwait(HV_TOD_WAIT_USEC);
}
if (ret != H_EOK && ret != H_ENOTSUPPORTED && ret != H_EWOULDBLOCK)
cmn_err(CE_WARN,
"tod_set: Unknown error from hv_tod_set, err %lx", ret);
else
/* TOD was modified */
tod_status_set(TOD_SET_DONE);
}
/*
* 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)
{}
|