kern_clock.c revision 1.105 1 1.105 ad /* $NetBSD: kern_clock.c,v 1.105 2007/02/09 21:55:30 ad Exp $ */
2 1.52 thorpej
3 1.52 thorpej /*-
4 1.94 mycroft * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc.
5 1.52 thorpej * All rights reserved.
6 1.52 thorpej *
7 1.52 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.52 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.52 thorpej * NASA Ames Research Center.
10 1.94 mycroft * This code is derived from software contributed to The NetBSD Foundation
11 1.94 mycroft * by Charles M. Hannum.
12 1.52 thorpej *
13 1.52 thorpej * Redistribution and use in source and binary forms, with or without
14 1.52 thorpej * modification, are permitted provided that the following conditions
15 1.52 thorpej * are met:
16 1.52 thorpej * 1. Redistributions of source code must retain the above copyright
17 1.52 thorpej * notice, this list of conditions and the following disclaimer.
18 1.52 thorpej * 2. Redistributions in binary form must reproduce the above copyright
19 1.52 thorpej * notice, this list of conditions and the following disclaimer in the
20 1.52 thorpej * documentation and/or other materials provided with the distribution.
21 1.52 thorpej * 3. All advertising materials mentioning features or use of this software
22 1.52 thorpej * must display the following acknowledgement:
23 1.52 thorpej * This product includes software developed by the NetBSD
24 1.52 thorpej * Foundation, Inc. and its contributors.
25 1.52 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
26 1.52 thorpej * contributors may be used to endorse or promote products derived
27 1.52 thorpej * from this software without specific prior written permission.
28 1.52 thorpej *
29 1.52 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 1.52 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 1.52 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 1.52 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 1.52 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 1.52 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 1.52 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 1.52 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 1.52 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 1.52 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 1.52 thorpej * POSSIBILITY OF SUCH DAMAGE.
40 1.52 thorpej */
41 1.19 cgd
42 1.19 cgd /*-
43 1.19 cgd * Copyright (c) 1982, 1986, 1991, 1993
44 1.19 cgd * The Regents of the University of California. All rights reserved.
45 1.19 cgd * (c) UNIX System Laboratories, Inc.
46 1.19 cgd * All or some portions of this file are derived from material licensed
47 1.19 cgd * to the University of California by American Telephone and Telegraph
48 1.19 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
49 1.19 cgd * the permission of UNIX System Laboratories, Inc.
50 1.19 cgd *
51 1.19 cgd * Redistribution and use in source and binary forms, with or without
52 1.19 cgd * modification, are permitted provided that the following conditions
53 1.19 cgd * are met:
54 1.19 cgd * 1. Redistributions of source code must retain the above copyright
55 1.19 cgd * notice, this list of conditions and the following disclaimer.
56 1.19 cgd * 2. Redistributions in binary form must reproduce the above copyright
57 1.19 cgd * notice, this list of conditions and the following disclaimer in the
58 1.19 cgd * documentation and/or other materials provided with the distribution.
59 1.87 agc * 3. Neither the name of the University nor the names of its contributors
60 1.19 cgd * may be used to endorse or promote products derived from this software
61 1.19 cgd * without specific prior written permission.
62 1.19 cgd *
63 1.19 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.19 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.19 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.19 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.19 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.19 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.19 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.19 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.19 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.19 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.19 cgd * SUCH DAMAGE.
74 1.19 cgd *
75 1.19 cgd * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
76 1.19 cgd */
77 1.78 lukem
78 1.78 lukem #include <sys/cdefs.h>
79 1.105 ad __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.105 2007/02/09 21:55:30 ad Exp $");
80 1.44 jonathan
81 1.44 jonathan #include "opt_ntp.h"
82 1.86 martin #include "opt_multiprocessor.h"
83 1.80 briggs #include "opt_perfctrs.h"
84 1.19 cgd
85 1.19 cgd #include <sys/param.h>
86 1.19 cgd #include <sys/systm.h>
87 1.19 cgd #include <sys/callout.h>
88 1.19 cgd #include <sys/kernel.h>
89 1.19 cgd #include <sys/proc.h>
90 1.19 cgd #include <sys/resourcevar.h>
91 1.25 christos #include <sys/signalvar.h>
92 1.26 christos #include <sys/sysctl.h>
93 1.27 jonathan #include <sys/timex.h>
94 1.45 ross #include <sys/sched.h>
95 1.82 thorpej #include <sys/time.h>
96 1.99 kardel #ifdef __HAVE_TIMECOUNTER
97 1.99 kardel #include <sys/timetc.h>
98 1.99 kardel #endif
99 1.19 cgd
100 1.19 cgd #include <machine/cpu.h>
101 1.74 thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
102 1.74 thorpej #include <machine/intr.h>
103 1.74 thorpej #endif
104 1.25 christos
105 1.19 cgd #ifdef GPROF
106 1.19 cgd #include <sys/gmon.h>
107 1.19 cgd #endif
108 1.19 cgd
109 1.19 cgd /*
110 1.19 cgd * Clock handling routines.
111 1.19 cgd *
112 1.19 cgd * This code is written to operate with two timers that run independently of
113 1.19 cgd * each other. The main clock, running hz times per second, is used to keep
114 1.19 cgd * track of real time. The second timer handles kernel and user profiling,
115 1.19 cgd * and does resource use estimation. If the second timer is programmable,
116 1.19 cgd * it is randomized to avoid aliasing between the two clocks. For example,
117 1.90 wiz * the randomization prevents an adversary from always giving up the CPU
118 1.19 cgd * just before its quantum expires. Otherwise, it would never accumulate
119 1.90 wiz * CPU ticks. The mean frequency of the second timer is stathz.
120 1.19 cgd *
121 1.19 cgd * If no second timer exists, stathz will be zero; in this case we drive
122 1.19 cgd * profiling and statistics off the main clock. This WILL NOT be accurate;
123 1.19 cgd * do not do it unless absolutely necessary.
124 1.19 cgd *
125 1.19 cgd * The statistics clock may (or may not) be run at a higher rate while
126 1.19 cgd * profiling. This profile clock runs at profhz. We require that profhz
127 1.19 cgd * be an integral multiple of stathz.
128 1.19 cgd *
129 1.19 cgd * If the statistics clock is running fast, it must be divided by the ratio
130 1.19 cgd * profhz/stathz for statistics. (For profiling, every tick counts.)
131 1.19 cgd */
132 1.19 cgd
133 1.99 kardel #ifndef __HAVE_TIMECOUNTER
134 1.27 jonathan #ifdef NTP /* NTP phase-locked loop in kernel */
135 1.27 jonathan /*
136 1.27 jonathan * Phase/frequency-lock loop (PLL/FLL) definitions
137 1.27 jonathan *
138 1.27 jonathan * The following variables are read and set by the ntp_adjtime() system
139 1.27 jonathan * call.
140 1.27 jonathan *
141 1.27 jonathan * time_state shows the state of the system clock, with values defined
142 1.27 jonathan * in the timex.h header file.
143 1.27 jonathan *
144 1.27 jonathan * time_status shows the status of the system clock, with bits defined
145 1.27 jonathan * in the timex.h header file.
146 1.27 jonathan *
147 1.27 jonathan * time_offset is used by the PLL/FLL to adjust the system time in small
148 1.27 jonathan * increments.
149 1.27 jonathan *
150 1.27 jonathan * time_constant determines the bandwidth or "stiffness" of the PLL.
151 1.27 jonathan *
152 1.27 jonathan * time_tolerance determines maximum frequency error or tolerance of the
153 1.27 jonathan * CPU clock oscillator and is a property of the architecture; however,
154 1.27 jonathan * in principle it could change as result of the presence of external
155 1.27 jonathan * discipline signals, for instance.
156 1.27 jonathan *
157 1.27 jonathan * time_precision is usually equal to the kernel tick variable; however,
158 1.27 jonathan * in cases where a precision clock counter or external clock is
159 1.27 jonathan * available, the resolution can be much less than this and depend on
160 1.27 jonathan * whether the external clock is working or not.
161 1.27 jonathan *
162 1.27 jonathan * time_maxerror is initialized by a ntp_adjtime() call and increased by
163 1.27 jonathan * the kernel once each second to reflect the maximum error bound
164 1.27 jonathan * growth.
165 1.27 jonathan *
166 1.27 jonathan * time_esterror is set and read by the ntp_adjtime() call, but
167 1.27 jonathan * otherwise not used by the kernel.
168 1.27 jonathan */
169 1.27 jonathan int time_state = TIME_OK; /* clock state */
170 1.27 jonathan int time_status = STA_UNSYNC; /* clock status bits */
171 1.27 jonathan long time_offset = 0; /* time offset (us) */
172 1.27 jonathan long time_constant = 0; /* pll time constant */
173 1.27 jonathan long time_tolerance = MAXFREQ; /* frequency tolerance (scaled ppm) */
174 1.27 jonathan long time_precision = 1; /* clock precision (us) */
175 1.27 jonathan long time_maxerror = MAXPHASE; /* maximum error (us) */
176 1.27 jonathan long time_esterror = MAXPHASE; /* estimated error (us) */
177 1.27 jonathan
178 1.27 jonathan /*
179 1.27 jonathan * The following variables establish the state of the PLL/FLL and the
180 1.27 jonathan * residual time and frequency offset of the local clock. The scale
181 1.27 jonathan * factors are defined in the timex.h header file.
182 1.27 jonathan *
183 1.27 jonathan * time_phase and time_freq are the phase increment and the frequency
184 1.27 jonathan * increment, respectively, of the kernel time variable.
185 1.27 jonathan *
186 1.27 jonathan * time_freq is set via ntp_adjtime() from a value stored in a file when
187 1.27 jonathan * the synchronization daemon is first started. Its value is retrieved
188 1.27 jonathan * via ntp_adjtime() and written to the file about once per hour by the
189 1.27 jonathan * daemon.
190 1.27 jonathan *
191 1.27 jonathan * time_adj is the adjustment added to the value of tick at each timer
192 1.27 jonathan * interrupt and is recomputed from time_phase and time_freq at each
193 1.27 jonathan * seconds rollover.
194 1.27 jonathan *
195 1.27 jonathan * time_reftime is the second's portion of the system time at the last
196 1.27 jonathan * call to ntp_adjtime(). It is used to adjust the time_freq variable
197 1.27 jonathan * and to increase the time_maxerror as the time since last update
198 1.27 jonathan * increases.
199 1.27 jonathan */
200 1.27 jonathan long time_phase = 0; /* phase offset (scaled us) */
201 1.27 jonathan long time_freq = 0; /* frequency offset (scaled ppm) */
202 1.27 jonathan long time_adj = 0; /* tick adjust (scaled 1 / hz) */
203 1.27 jonathan long time_reftime = 0; /* time at last adjustment (s) */
204 1.27 jonathan
205 1.27 jonathan #ifdef PPS_SYNC
206 1.27 jonathan /*
207 1.27 jonathan * The following variables are used only if the kernel PPS discipline
208 1.27 jonathan * code is configured (PPS_SYNC). The scale factors are defined in the
209 1.27 jonathan * timex.h header file.
210 1.27 jonathan *
211 1.27 jonathan * pps_time contains the time at each calibration interval, as read by
212 1.27 jonathan * microtime(). pps_count counts the seconds of the calibration
213 1.27 jonathan * interval, the duration of which is nominally pps_shift in powers of
214 1.27 jonathan * two.
215 1.27 jonathan *
216 1.27 jonathan * pps_offset is the time offset produced by the time median filter
217 1.27 jonathan * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
218 1.27 jonathan * this filter.
219 1.27 jonathan *
220 1.27 jonathan * pps_freq is the frequency offset produced by the frequency median
221 1.27 jonathan * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
222 1.27 jonathan * by this filter.
223 1.27 jonathan *
224 1.27 jonathan * pps_usec is latched from a high resolution counter or external clock
225 1.27 jonathan * at pps_time. Here we want the hardware counter contents only, not the
226 1.27 jonathan * contents plus the time_tv.usec as usual.
227 1.27 jonathan *
228 1.27 jonathan * pps_valid counts the number of seconds since the last PPS update. It
229 1.27 jonathan * is used as a watchdog timer to disable the PPS discipline should the
230 1.27 jonathan * PPS signal be lost.
231 1.27 jonathan *
232 1.27 jonathan * pps_glitch counts the number of seconds since the beginning of an
233 1.27 jonathan * offset burst more than tick/2 from current nominal offset. It is used
234 1.27 jonathan * mainly to suppress error bursts due to priority conflicts between the
235 1.27 jonathan * PPS interrupt and timer interrupt.
236 1.27 jonathan *
237 1.27 jonathan * pps_intcnt counts the calibration intervals for use in the interval-
238 1.27 jonathan * adaptation algorithm. It's just too complicated for words.
239 1.89 simonb *
240 1.89 simonb * pps_kc_hardpps_source contains an arbitrary value that uniquely
241 1.89 simonb * identifies the currently bound source of the PPS signal, or NULL
242 1.89 simonb * if no source is bound.
243 1.89 simonb *
244 1.89 simonb * pps_kc_hardpps_mode indicates which transitions, if any, of the PPS
245 1.89 simonb * signal should be reported.
246 1.27 jonathan */
247 1.27 jonathan struct timeval pps_time; /* kernel time at last interval */
248 1.27 jonathan long pps_tf[] = {0, 0, 0}; /* pps time offset median filter (us) */
249 1.27 jonathan long pps_offset = 0; /* pps time offset (us) */
250 1.27 jonathan long pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
251 1.27 jonathan long pps_ff[] = {0, 0, 0}; /* pps frequency offset median filter */
252 1.27 jonathan long pps_freq = 0; /* frequency offset (scaled ppm) */
253 1.27 jonathan long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
254 1.27 jonathan long pps_usec = 0; /* microsec counter at last interval */
255 1.27 jonathan long pps_valid = PPS_VALID; /* pps signal watchdog counter */
256 1.27 jonathan int pps_glitch = 0; /* pps signal glitch counter */
257 1.27 jonathan int pps_count = 0; /* calibration interval counter (s) */
258 1.27 jonathan int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
259 1.27 jonathan int pps_intcnt = 0; /* intervals at current duration */
260 1.89 simonb void *pps_kc_hardpps_source = NULL; /* current PPS supplier's identifier */
261 1.89 simonb int pps_kc_hardpps_mode = 0; /* interesting edges of PPS signal */
262 1.27 jonathan
263 1.27 jonathan /*
264 1.27 jonathan * PPS signal quality monitors
265 1.27 jonathan *
266 1.27 jonathan * pps_jitcnt counts the seconds that have been discarded because the
267 1.27 jonathan * jitter measured by the time median filter exceeds the limit MAXTIME
268 1.27 jonathan * (100 us).
269 1.27 jonathan *
270 1.27 jonathan * pps_calcnt counts the frequency calibration intervals, which are
271 1.27 jonathan * variable from 4 s to 256 s.
272 1.27 jonathan *
273 1.27 jonathan * pps_errcnt counts the calibration intervals which have been discarded
274 1.27 jonathan * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
275 1.27 jonathan * calibration interval jitter exceeds two ticks.
276 1.27 jonathan *
277 1.27 jonathan * pps_stbcnt counts the calibration intervals that have been discarded
278 1.27 jonathan * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
279 1.27 jonathan */
280 1.27 jonathan long pps_jitcnt = 0; /* jitter limit exceeded */
281 1.27 jonathan long pps_calcnt = 0; /* calibration intervals */
282 1.27 jonathan long pps_errcnt = 0; /* calibration errors */
283 1.27 jonathan long pps_stbcnt = 0; /* stability limit exceeded */
284 1.27 jonathan #endif /* PPS_SYNC */
285 1.27 jonathan
286 1.27 jonathan #ifdef EXT_CLOCK
287 1.27 jonathan /*
288 1.27 jonathan * External clock definitions
289 1.27 jonathan *
290 1.27 jonathan * The following definitions and declarations are used only if an
291 1.27 jonathan * external clock is configured on the system.
292 1.27 jonathan */
293 1.27 jonathan #define CLOCK_INTERVAL 30 /* CPU clock update interval (s) */
294 1.27 jonathan
295 1.27 jonathan /*
296 1.27 jonathan * The clock_count variable is set to CLOCK_INTERVAL at each PPS
297 1.27 jonathan * interrupt and decremented once each second.
298 1.27 jonathan */
299 1.27 jonathan int clock_count = 0; /* CPU clock counter */
300 1.27 jonathan
301 1.27 jonathan #ifdef HIGHBALL
302 1.27 jonathan /*
303 1.27 jonathan * The clock_offset and clock_cpu variables are used by the HIGHBALL
304 1.27 jonathan * interface. The clock_offset variable defines the offset between
305 1.27 jonathan * system time and the HIGBALL counters. The clock_cpu variable contains
306 1.27 jonathan * the offset between the system clock and the HIGHBALL clock for use in
307 1.27 jonathan * disciplining the kernel time variable.
308 1.27 jonathan */
309 1.27 jonathan extern struct timeval clock_offset; /* Highball clock offset */
310 1.27 jonathan long clock_cpu = 0; /* CPU clock adjust */
311 1.27 jonathan #endif /* HIGHBALL */
312 1.27 jonathan #endif /* EXT_CLOCK */
313 1.27 jonathan #endif /* NTP */
314 1.27 jonathan
315 1.19 cgd /*
316 1.19 cgd * Bump a timeval by a small number of usec's.
317 1.19 cgd */
318 1.19 cgd #define BUMPTIME(t, usec) { \
319 1.55 augustss volatile struct timeval *tp = (t); \
320 1.55 augustss long us; \
321 1.19 cgd \
322 1.19 cgd tp->tv_usec = us = tp->tv_usec + (usec); \
323 1.19 cgd if (us >= 1000000) { \
324 1.19 cgd tp->tv_usec = us - 1000000; \
325 1.19 cgd tp->tv_sec++; \
326 1.19 cgd } \
327 1.19 cgd }
328 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
329 1.19 cgd
330 1.19 cgd int stathz;
331 1.19 cgd int profhz;
332 1.80 briggs int profsrc;
333 1.75 simonb int schedhz;
334 1.19 cgd int profprocs;
335 1.100 drochner int hardclock_ticks;
336 1.91 yamt static int statscheddiv; /* stat => sched divider (used if schedhz == 0) */
337 1.70 sommerfe static int psdiv; /* prof => stat divider */
338 1.22 cgd int psratio; /* ratio: prof / stat */
339 1.99 kardel #ifndef __HAVE_TIMECOUNTER
340 1.22 cgd int tickfix, tickfixinterval; /* used if tick not really integral */
341 1.34 briggs #ifndef NTP
342 1.39 cgd static int tickfixcnt; /* accumulated fractional error */
343 1.34 briggs #else
344 1.27 jonathan int fixtick; /* used by NTP for same */
345 1.31 mycroft int shifthz;
346 1.31 mycroft #endif
347 1.19 cgd
348 1.48 christos /*
349 1.48 christos * We might want ldd to load the both words from time at once.
350 1.48 christos * To succeed we need to be quadword aligned.
351 1.48 christos * The sparc already does that, and that it has worked so far is a fluke.
352 1.48 christos */
353 1.48 christos volatile struct timeval time __attribute__((__aligned__(__alignof__(quad_t))));
354 1.19 cgd volatile struct timeval mono_time;
355 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
356 1.19 cgd
357 1.99 kardel #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
358 1.73 thorpej void *softclock_si;
359 1.99 kardel #endif
360 1.99 kardel
361 1.99 kardel #ifdef __HAVE_TIMECOUNTER
362 1.99 kardel static u_int get_intr_timecount(struct timecounter *);
363 1.99 kardel
364 1.99 kardel static struct timecounter intr_timecounter = {
365 1.99 kardel get_intr_timecount, /* get_timecount */
366 1.99 kardel 0, /* no poll_pps */
367 1.99 kardel ~0u, /* counter_mask */
368 1.99 kardel 0, /* frequency */
369 1.99 kardel "clockinterrupt", /* name */
370 1.102 christos 0, /* quality - minimum implementation level for a clock */
371 1.102 christos NULL, /* prev */
372 1.102 christos NULL, /* next */
373 1.99 kardel };
374 1.99 kardel
375 1.99 kardel static u_int
376 1.104 yamt get_intr_timecount(struct timecounter *tc)
377 1.99 kardel {
378 1.104 yamt
379 1.100 drochner return (u_int)hardclock_ticks;
380 1.99 kardel }
381 1.99 kardel #endif
382 1.73 thorpej
383 1.66 thorpej /*
384 1.19 cgd * Initialize clock frequencies and start both clocks running.
385 1.19 cgd */
386 1.19 cgd void
387 1.63 thorpej initclocks(void)
388 1.19 cgd {
389 1.55 augustss int i;
390 1.19 cgd
391 1.73 thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
392 1.73 thorpej softclock_si = softintr_establish(IPL_SOFTCLOCK, softclock, NULL);
393 1.73 thorpej if (softclock_si == NULL)
394 1.73 thorpej panic("initclocks: unable to register softclock intr");
395 1.73 thorpej #endif
396 1.73 thorpej
397 1.19 cgd /*
398 1.19 cgd * Set divisors to 1 (normal case) and let the machine-specific
399 1.19 cgd * code do its bit.
400 1.19 cgd */
401 1.70 sommerfe psdiv = 1;
402 1.99 kardel #ifdef __HAVE_TIMECOUNTER
403 1.99 kardel /*
404 1.99 kardel * provide minimum default time counter
405 1.99 kardel * will only run at interrupt resolution
406 1.99 kardel */
407 1.99 kardel intr_timecounter.tc_frequency = hz;
408 1.99 kardel tc_init(&intr_timecounter);
409 1.99 kardel #endif
410 1.19 cgd cpu_initclocks();
411 1.19 cgd
412 1.19 cgd /*
413 1.70 sommerfe * Compute profhz/stathz/rrticks, and fix profhz if needed.
414 1.19 cgd */
415 1.19 cgd i = stathz ? stathz : hz;
416 1.19 cgd if (profhz == 0)
417 1.19 cgd profhz = i;
418 1.19 cgd psratio = profhz / i;
419 1.70 sommerfe rrticks = hz / 10;
420 1.91 yamt if (schedhz == 0) {
421 1.91 yamt /* 16Hz is best */
422 1.91 yamt statscheddiv = i / 16;
423 1.91 yamt if (statscheddiv <= 0)
424 1.91 yamt panic("statscheddiv");
425 1.91 yamt }
426 1.31 mycroft
427 1.99 kardel #ifndef __HAVE_TIMECOUNTER
428 1.31 mycroft #ifdef NTP
429 1.31 mycroft switch (hz) {
430 1.57 mycroft case 1:
431 1.57 mycroft shifthz = SHIFT_SCALE - 0;
432 1.57 mycroft break;
433 1.57 mycroft case 2:
434 1.57 mycroft shifthz = SHIFT_SCALE - 1;
435 1.57 mycroft break;
436 1.57 mycroft case 4:
437 1.57 mycroft shifthz = SHIFT_SCALE - 2;
438 1.57 mycroft break;
439 1.57 mycroft case 8:
440 1.57 mycroft shifthz = SHIFT_SCALE - 3;
441 1.57 mycroft break;
442 1.57 mycroft case 16:
443 1.57 mycroft shifthz = SHIFT_SCALE - 4;
444 1.57 mycroft break;
445 1.57 mycroft case 32:
446 1.57 mycroft shifthz = SHIFT_SCALE - 5;
447 1.57 mycroft break;
448 1.92 tls case 50:
449 1.31 mycroft case 60:
450 1.31 mycroft case 64:
451 1.31 mycroft shifthz = SHIFT_SCALE - 6;
452 1.31 mycroft break;
453 1.31 mycroft case 96:
454 1.31 mycroft case 100:
455 1.31 mycroft case 128:
456 1.31 mycroft shifthz = SHIFT_SCALE - 7;
457 1.31 mycroft break;
458 1.31 mycroft case 256:
459 1.31 mycroft shifthz = SHIFT_SCALE - 8;
460 1.41 tls break;
461 1.41 tls case 512:
462 1.41 tls shifthz = SHIFT_SCALE - 9;
463 1.31 mycroft break;
464 1.43 ross case 1000:
465 1.31 mycroft case 1024:
466 1.31 mycroft shifthz = SHIFT_SCALE - 10;
467 1.31 mycroft break;
468 1.57 mycroft case 1200:
469 1.57 mycroft case 2048:
470 1.57 mycroft shifthz = SHIFT_SCALE - 11;
471 1.57 mycroft break;
472 1.57 mycroft case 4096:
473 1.57 mycroft shifthz = SHIFT_SCALE - 12;
474 1.57 mycroft break;
475 1.57 mycroft case 8192:
476 1.57 mycroft shifthz = SHIFT_SCALE - 13;
477 1.57 mycroft break;
478 1.57 mycroft case 16384:
479 1.57 mycroft shifthz = SHIFT_SCALE - 14;
480 1.57 mycroft break;
481 1.57 mycroft case 32768:
482 1.57 mycroft shifthz = SHIFT_SCALE - 15;
483 1.57 mycroft break;
484 1.57 mycroft case 65536:
485 1.57 mycroft shifthz = SHIFT_SCALE - 16;
486 1.57 mycroft break;
487 1.31 mycroft default:
488 1.31 mycroft panic("weird hz");
489 1.50 sommerfe }
490 1.50 sommerfe if (fixtick == 0) {
491 1.52 thorpej /*
492 1.52 thorpej * Give MD code a chance to set this to a better
493 1.52 thorpej * value; but, if it doesn't, we should.
494 1.52 thorpej */
495 1.50 sommerfe fixtick = (1000000 - (hz*tick));
496 1.31 mycroft }
497 1.99 kardel #endif /* NTP */
498 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
499 1.19 cgd }
500 1.19 cgd
501 1.19 cgd /*
502 1.19 cgd * The real-time timer, interrupting hz times per second.
503 1.19 cgd */
504 1.19 cgd void
505 1.63 thorpej hardclock(struct clockframe *frame)
506 1.19 cgd {
507 1.82 thorpej struct lwp *l;
508 1.55 augustss struct proc *p;
509 1.99 kardel struct cpu_info *ci = curcpu();
510 1.99 kardel struct ptimer *pt;
511 1.99 kardel #ifndef __HAVE_TIMECOUNTER
512 1.55 augustss int delta;
513 1.19 cgd extern int tickdelta;
514 1.19 cgd extern long timedelta;
515 1.30 mycroft #ifdef NTP
516 1.55 augustss int time_update;
517 1.55 augustss int ltemp;
518 1.99 kardel #endif /* NTP */
519 1.99 kardel #endif /* __HAVE_TIMECOUNTER */
520 1.19 cgd
521 1.82 thorpej l = curlwp;
522 1.82 thorpej if (l) {
523 1.82 thorpej p = l->l_proc;
524 1.19 cgd /*
525 1.19 cgd * Run current process's virtual and profile time, as needed.
526 1.19 cgd */
527 1.82 thorpej if (CLKF_USERMODE(frame) && p->p_timers &&
528 1.82 thorpej (pt = LIST_FIRST(&p->p_timers->pts_virtual)) != NULL)
529 1.82 thorpej if (itimerdecr(pt, tick) == 0)
530 1.82 thorpej itimerfire(pt);
531 1.82 thorpej if (p->p_timers &&
532 1.82 thorpej (pt = LIST_FIRST(&p->p_timers->pts_prof)) != NULL)
533 1.82 thorpej if (itimerdecr(pt, tick) == 0)
534 1.82 thorpej itimerfire(pt);
535 1.19 cgd }
536 1.19 cgd
537 1.19 cgd /*
538 1.19 cgd * If no separate statistics clock is available, run it from here.
539 1.19 cgd */
540 1.19 cgd if (stathz == 0)
541 1.19 cgd statclock(frame);
542 1.70 sommerfe if ((--ci->ci_schedstate.spc_rrticks) <= 0)
543 1.71 sommerfe roundrobin(ci);
544 1.93 perry
545 1.60 thorpej #if defined(MULTIPROCESSOR)
546 1.60 thorpej /*
547 1.60 thorpej * If we are not the primary CPU, we're not allowed to do
548 1.60 thorpej * any more work.
549 1.60 thorpej */
550 1.70 sommerfe if (CPU_IS_PRIMARY(ci) == 0)
551 1.60 thorpej return;
552 1.60 thorpej #endif
553 1.60 thorpej
554 1.99 kardel hardclock_ticks++;
555 1.99 kardel
556 1.99 kardel #ifdef __HAVE_TIMECOUNTER
557 1.99 kardel tc_ticktock();
558 1.99 kardel #else /* __HAVE_TIMECOUNTER */
559 1.19 cgd /*
560 1.22 cgd * Increment the time-of-day. The increment is normally just
561 1.22 cgd * ``tick''. If the machine is one which has a clock frequency
562 1.22 cgd * such that ``hz'' would not divide the second evenly into
563 1.22 cgd * milliseconds, a periodic adjustment must be applied. Finally,
564 1.22 cgd * if we are still adjusting the time (see adjtime()),
565 1.22 cgd * ``tickdelta'' may also be added in.
566 1.19 cgd */
567 1.22 cgd delta = tick;
568 1.27 jonathan
569 1.27 jonathan #ifndef NTP
570 1.22 cgd if (tickfix) {
571 1.39 cgd tickfixcnt += tickfix;
572 1.24 cgd if (tickfixcnt >= tickfixinterval) {
573 1.39 cgd delta++;
574 1.39 cgd tickfixcnt -= tickfixinterval;
575 1.22 cgd }
576 1.22 cgd }
577 1.27 jonathan #endif /* !NTP */
578 1.27 jonathan /* Imprecise 4bsd adjtime() handling */
579 1.22 cgd if (timedelta != 0) {
580 1.38 cgd delta += tickdelta;
581 1.19 cgd timedelta -= tickdelta;
582 1.19 cgd }
583 1.27 jonathan
584 1.27 jonathan #ifdef notyet
585 1.27 jonathan microset();
586 1.27 jonathan #endif
587 1.27 jonathan
588 1.27 jonathan #ifndef NTP
589 1.27 jonathan BUMPTIME(&time, delta); /* XXX Now done using NTP code below */
590 1.27 jonathan #endif
591 1.19 cgd BUMPTIME(&mono_time, delta);
592 1.27 jonathan
593 1.31 mycroft #ifdef NTP
594 1.30 mycroft time_update = delta;
595 1.27 jonathan
596 1.27 jonathan /*
597 1.27 jonathan * Compute the phase adjustment. If the low-order bits
598 1.27 jonathan * (time_phase) of the update overflow, bump the high-order bits
599 1.27 jonathan * (time_update).
600 1.27 jonathan */
601 1.27 jonathan time_phase += time_adj;
602 1.27 jonathan if (time_phase <= -FINEUSEC) {
603 1.27 jonathan ltemp = -time_phase >> SHIFT_SCALE;
604 1.27 jonathan time_phase += ltemp << SHIFT_SCALE;
605 1.27 jonathan time_update -= ltemp;
606 1.31 mycroft } else if (time_phase >= FINEUSEC) {
607 1.27 jonathan ltemp = time_phase >> SHIFT_SCALE;
608 1.27 jonathan time_phase -= ltemp << SHIFT_SCALE;
609 1.27 jonathan time_update += ltemp;
610 1.27 jonathan }
611 1.27 jonathan
612 1.27 jonathan #ifdef HIGHBALL
613 1.27 jonathan /*
614 1.27 jonathan * If the HIGHBALL board is installed, we need to adjust the
615 1.27 jonathan * external clock offset in order to close the hardware feedback
616 1.27 jonathan * loop. This will adjust the external clock phase and frequency
617 1.27 jonathan * in small amounts. The additional phase noise and frequency
618 1.27 jonathan * wander this causes should be minimal. We also need to
619 1.27 jonathan * discipline the kernel time variable, since the PLL is used to
620 1.27 jonathan * discipline the external clock. If the Highball board is not
621 1.27 jonathan * present, we discipline kernel time with the PLL as usual. We
622 1.27 jonathan * assume that the external clock phase adjustment (time_update)
623 1.27 jonathan * and kernel phase adjustment (clock_cpu) are less than the
624 1.27 jonathan * value of tick.
625 1.27 jonathan */
626 1.27 jonathan clock_offset.tv_usec += time_update;
627 1.27 jonathan if (clock_offset.tv_usec >= 1000000) {
628 1.27 jonathan clock_offset.tv_sec++;
629 1.27 jonathan clock_offset.tv_usec -= 1000000;
630 1.27 jonathan }
631 1.27 jonathan if (clock_offset.tv_usec < 0) {
632 1.27 jonathan clock_offset.tv_sec--;
633 1.27 jonathan clock_offset.tv_usec += 1000000;
634 1.27 jonathan }
635 1.27 jonathan time.tv_usec += clock_cpu;
636 1.27 jonathan clock_cpu = 0;
637 1.27 jonathan #else
638 1.27 jonathan time.tv_usec += time_update;
639 1.27 jonathan #endif /* HIGHBALL */
640 1.27 jonathan
641 1.27 jonathan /*
642 1.27 jonathan * On rollover of the second the phase adjustment to be used for
643 1.27 jonathan * the next second is calculated. Also, the maximum error is
644 1.27 jonathan * increased by the tolerance. If the PPS frequency discipline
645 1.27 jonathan * code is present, the phase is increased to compensate for the
646 1.27 jonathan * CPU clock oscillator frequency error.
647 1.27 jonathan *
648 1.27 jonathan * On a 32-bit machine and given parameters in the timex.h
649 1.27 jonathan * header file, the maximum phase adjustment is +-512 ms and
650 1.27 jonathan * maximum frequency offset is a tad less than) +-512 ppm. On a
651 1.27 jonathan * 64-bit machine, you shouldn't need to ask.
652 1.27 jonathan */
653 1.27 jonathan if (time.tv_usec >= 1000000) {
654 1.27 jonathan time.tv_usec -= 1000000;
655 1.27 jonathan time.tv_sec++;
656 1.27 jonathan time_maxerror += time_tolerance >> SHIFT_USEC;
657 1.27 jonathan
658 1.27 jonathan /*
659 1.27 jonathan * Leap second processing. If in leap-insert state at
660 1.27 jonathan * the end of the day, the system clock is set back one
661 1.27 jonathan * second; if in leap-delete state, the system clock is
662 1.27 jonathan * set ahead one second. The microtime() routine or
663 1.27 jonathan * external clock driver will insure that reported time
664 1.27 jonathan * is always monotonic. The ugly divides should be
665 1.27 jonathan * replaced.
666 1.27 jonathan */
667 1.27 jonathan switch (time_state) {
668 1.31 mycroft case TIME_OK:
669 1.27 jonathan if (time_status & STA_INS)
670 1.27 jonathan time_state = TIME_INS;
671 1.27 jonathan else if (time_status & STA_DEL)
672 1.27 jonathan time_state = TIME_DEL;
673 1.27 jonathan break;
674 1.27 jonathan
675 1.31 mycroft case TIME_INS:
676 1.27 jonathan if (time.tv_sec % 86400 == 0) {
677 1.27 jonathan time.tv_sec--;
678 1.27 jonathan time_state = TIME_OOP;
679 1.27 jonathan }
680 1.27 jonathan break;
681 1.27 jonathan
682 1.31 mycroft case TIME_DEL:
683 1.27 jonathan if ((time.tv_sec + 1) % 86400 == 0) {
684 1.27 jonathan time.tv_sec++;
685 1.27 jonathan time_state = TIME_WAIT;
686 1.27 jonathan }
687 1.27 jonathan break;
688 1.27 jonathan
689 1.31 mycroft case TIME_OOP:
690 1.27 jonathan time_state = TIME_WAIT;
691 1.27 jonathan break;
692 1.27 jonathan
693 1.31 mycroft case TIME_WAIT:
694 1.27 jonathan if (!(time_status & (STA_INS | STA_DEL)))
695 1.27 jonathan time_state = TIME_OK;
696 1.31 mycroft break;
697 1.27 jonathan }
698 1.27 jonathan
699 1.27 jonathan /*
700 1.27 jonathan * Compute the phase adjustment for the next second. In
701 1.27 jonathan * PLL mode, the offset is reduced by a fixed factor
702 1.27 jonathan * times the time constant. In FLL mode the offset is
703 1.27 jonathan * used directly. In either mode, the maximum phase
704 1.27 jonathan * adjustment for each second is clamped so as to spread
705 1.27 jonathan * the adjustment over not more than the number of
706 1.27 jonathan * seconds between updates.
707 1.27 jonathan */
708 1.27 jonathan if (time_offset < 0) {
709 1.27 jonathan ltemp = -time_offset;
710 1.27 jonathan if (!(time_status & STA_FLL))
711 1.27 jonathan ltemp >>= SHIFT_KG + time_constant;
712 1.27 jonathan if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
713 1.27 jonathan ltemp = (MAXPHASE / MINSEC) <<
714 1.27 jonathan SHIFT_UPDATE;
715 1.27 jonathan time_offset += ltemp;
716 1.31 mycroft time_adj = -ltemp << (shifthz - SHIFT_UPDATE);
717 1.31 mycroft } else if (time_offset > 0) {
718 1.27 jonathan ltemp = time_offset;
719 1.27 jonathan if (!(time_status & STA_FLL))
720 1.27 jonathan ltemp >>= SHIFT_KG + time_constant;
721 1.27 jonathan if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
722 1.27 jonathan ltemp = (MAXPHASE / MINSEC) <<
723 1.27 jonathan SHIFT_UPDATE;
724 1.27 jonathan time_offset -= ltemp;
725 1.31 mycroft time_adj = ltemp << (shifthz - SHIFT_UPDATE);
726 1.31 mycroft } else
727 1.31 mycroft time_adj = 0;
728 1.27 jonathan
729 1.27 jonathan /*
730 1.27 jonathan * Compute the frequency estimate and additional phase
731 1.27 jonathan * adjustment due to frequency error for the next
732 1.27 jonathan * second. When the PPS signal is engaged, gnaw on the
733 1.27 jonathan * watchdog counter and update the frequency computed by
734 1.27 jonathan * the pll and the PPS signal.
735 1.27 jonathan */
736 1.27 jonathan #ifdef PPS_SYNC
737 1.27 jonathan pps_valid++;
738 1.27 jonathan if (pps_valid == PPS_VALID) {
739 1.27 jonathan pps_jitter = MAXTIME;
740 1.27 jonathan pps_stabil = MAXFREQ;
741 1.27 jonathan time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
742 1.27 jonathan STA_PPSWANDER | STA_PPSERROR);
743 1.27 jonathan }
744 1.27 jonathan ltemp = time_freq + pps_freq;
745 1.27 jonathan #else
746 1.27 jonathan ltemp = time_freq;
747 1.27 jonathan #endif /* PPS_SYNC */
748 1.27 jonathan
749 1.27 jonathan if (ltemp < 0)
750 1.31 mycroft time_adj -= -ltemp >> (SHIFT_USEC - shifthz);
751 1.27 jonathan else
752 1.31 mycroft time_adj += ltemp >> (SHIFT_USEC - shifthz);
753 1.31 mycroft time_adj += (long)fixtick << shifthz;
754 1.27 jonathan
755 1.27 jonathan /*
756 1.27 jonathan * When the CPU clock oscillator frequency is not a
757 1.31 mycroft * power of 2 in Hz, shifthz is only an approximate
758 1.31 mycroft * scale factor.
759 1.46 mycroft *
760 1.46 mycroft * To determine the adjustment, you can do the following:
761 1.46 mycroft * bc -q
762 1.46 mycroft * scale=24
763 1.46 mycroft * obase=2
764 1.46 mycroft * idealhz/realhz
765 1.46 mycroft * where `idealhz' is the next higher power of 2, and `realhz'
766 1.57 mycroft * is the actual value. You may need to factor this result
767 1.57 mycroft * into a sequence of 2 multipliers to get better precision.
768 1.46 mycroft *
769 1.46 mycroft * Likewise, the error can be calculated with (e.g. for 100Hz):
770 1.46 mycroft * bc -q
771 1.46 mycroft * scale=24
772 1.57 mycroft * ((1+2^-2+2^-5)*(1-2^-10)*realhz-idealhz)/idealhz
773 1.57 mycroft * (and then multiply by 1000000 to get ppm).
774 1.27 jonathan */
775 1.31 mycroft switch (hz) {
776 1.58 mycroft case 60:
777 1.58 mycroft /* A factor of 1.000100010001 gives about 15ppm
778 1.58 mycroft error. */
779 1.58 mycroft if (time_adj < 0) {
780 1.58 mycroft time_adj -= (-time_adj >> 4);
781 1.58 mycroft time_adj -= (-time_adj >> 8);
782 1.58 mycroft } else {
783 1.58 mycroft time_adj += (time_adj >> 4);
784 1.58 mycroft time_adj += (time_adj >> 8);
785 1.58 mycroft }
786 1.58 mycroft break;
787 1.58 mycroft
788 1.31 mycroft case 96:
789 1.56 mycroft /* A factor of 1.0101010101 gives about 244ppm error. */
790 1.46 mycroft if (time_adj < 0) {
791 1.46 mycroft time_adj -= (-time_adj >> 2);
792 1.46 mycroft time_adj -= (-time_adj >> 4) + (-time_adj >> 8);
793 1.46 mycroft } else {
794 1.46 mycroft time_adj += (time_adj >> 2);
795 1.46 mycroft time_adj += (time_adj >> 4) + (time_adj >> 8);
796 1.46 mycroft }
797 1.46 mycroft break;
798 1.46 mycroft
799 1.92 tls case 50:
800 1.31 mycroft case 100:
801 1.56 mycroft /* A factor of 1.010001111010111 gives about 1ppm
802 1.56 mycroft error. */
803 1.56 mycroft if (time_adj < 0) {
804 1.46 mycroft time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
805 1.56 mycroft time_adj += (-time_adj >> 10);
806 1.56 mycroft } else {
807 1.46 mycroft time_adj += (time_adj >> 2) + (time_adj >> 5);
808 1.56 mycroft time_adj -= (time_adj >> 10);
809 1.56 mycroft }
810 1.43 ross break;
811 1.46 mycroft
812 1.43 ross case 1000:
813 1.56 mycroft /* A factor of 1.000001100010100001 gives about 50ppm
814 1.56 mycroft error. */
815 1.56 mycroft if (time_adj < 0) {
816 1.56 mycroft time_adj -= (-time_adj >> 6) + (-time_adj >> 11);
817 1.56 mycroft time_adj -= (-time_adj >> 7);
818 1.56 mycroft } else {
819 1.56 mycroft time_adj += (time_adj >> 6) + (time_adj >> 11);
820 1.56 mycroft time_adj += (time_adj >> 7);
821 1.56 mycroft }
822 1.56 mycroft break;
823 1.56 mycroft
824 1.56 mycroft case 1200:
825 1.56 mycroft /* A factor of 1.1011010011100001 gives about 64ppm
826 1.56 mycroft error. */
827 1.56 mycroft if (time_adj < 0) {
828 1.56 mycroft time_adj -= (-time_adj >> 1) + (-time_adj >> 6);
829 1.56 mycroft time_adj -= (-time_adj >> 3) + (-time_adj >> 10);
830 1.56 mycroft } else {
831 1.56 mycroft time_adj += (time_adj >> 1) + (time_adj >> 6);
832 1.56 mycroft time_adj += (time_adj >> 3) + (time_adj >> 10);
833 1.56 mycroft }
834 1.31 mycroft break;
835 1.27 jonathan }
836 1.27 jonathan
837 1.27 jonathan #ifdef EXT_CLOCK
838 1.27 jonathan /*
839 1.27 jonathan * If an external clock is present, it is necessary to
840 1.27 jonathan * discipline the kernel time variable anyway, since not
841 1.27 jonathan * all system components use the microtime() interface.
842 1.27 jonathan * Here, the time offset between the external clock and
843 1.27 jonathan * kernel time variable is computed every so often.
844 1.27 jonathan */
845 1.27 jonathan clock_count++;
846 1.27 jonathan if (clock_count > CLOCK_INTERVAL) {
847 1.27 jonathan clock_count = 0;
848 1.27 jonathan microtime(&clock_ext);
849 1.27 jonathan delta.tv_sec = clock_ext.tv_sec - time.tv_sec;
850 1.27 jonathan delta.tv_usec = clock_ext.tv_usec -
851 1.27 jonathan time.tv_usec;
852 1.27 jonathan if (delta.tv_usec < 0)
853 1.27 jonathan delta.tv_sec--;
854 1.27 jonathan if (delta.tv_usec >= 500000) {
855 1.27 jonathan delta.tv_usec -= 1000000;
856 1.27 jonathan delta.tv_sec++;
857 1.27 jonathan }
858 1.27 jonathan if (delta.tv_usec < -500000) {
859 1.27 jonathan delta.tv_usec += 1000000;
860 1.27 jonathan delta.tv_sec--;
861 1.27 jonathan }
862 1.27 jonathan if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
863 1.27 jonathan delta.tv_usec > MAXPHASE) ||
864 1.27 jonathan delta.tv_sec < -1 || (delta.tv_sec == -1 &&
865 1.27 jonathan delta.tv_usec < -MAXPHASE)) {
866 1.27 jonathan time = clock_ext;
867 1.27 jonathan delta.tv_sec = 0;
868 1.27 jonathan delta.tv_usec = 0;
869 1.27 jonathan }
870 1.27 jonathan #ifdef HIGHBALL
871 1.27 jonathan clock_cpu = delta.tv_usec;
872 1.27 jonathan #else /* HIGHBALL */
873 1.27 jonathan hardupdate(delta.tv_usec);
874 1.27 jonathan #endif /* HIGHBALL */
875 1.27 jonathan }
876 1.27 jonathan #endif /* EXT_CLOCK */
877 1.27 jonathan }
878 1.27 jonathan
879 1.31 mycroft #endif /* NTP */
880 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
881 1.19 cgd
882 1.19 cgd /*
883 1.84 thorpej * Update real-time timeout queue.
884 1.90 wiz * Process callouts at a very low CPU priority, so we don't keep the
885 1.19 cgd * relatively high clock interrupt priority any longer than necessary.
886 1.19 cgd */
887 1.84 thorpej if (callout_hardclock()) {
888 1.19 cgd if (CLKF_BASEPRI(frame)) {
889 1.19 cgd /*
890 1.19 cgd * Save the overhead of a software interrupt;
891 1.52 thorpej * it will happen as soon as we return, so do
892 1.52 thorpej * it now.
893 1.19 cgd */
894 1.65 thorpej spllowersoftclock();
895 1.105 ad KERNEL_LOCK(1, NULL);
896 1.73 thorpej softclock(NULL);
897 1.105 ad KERNEL_UNLOCK_ONE(NULL);
898 1.73 thorpej } else {
899 1.73 thorpej #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
900 1.73 thorpej softintr_schedule(softclock_si);
901 1.73 thorpej #else
902 1.19 cgd setsoftclock();
903 1.73 thorpej #endif
904 1.73 thorpej }
905 1.52 thorpej }
906 1.19 cgd }
907 1.19 cgd
908 1.99 kardel #ifdef __HAVE_TIMECOUNTER
909 1.99 kardel /*
910 1.99 kardel * Compute number of hz until specified time. Used to compute second
911 1.99 kardel * argument to callout_reset() from an absolute time.
912 1.99 kardel */
913 1.99 kardel int
914 1.99 kardel hzto(struct timeval *tvp)
915 1.99 kardel {
916 1.99 kardel struct timeval now, tv;
917 1.99 kardel
918 1.99 kardel tv = *tvp; /* Don't modify original tvp. */
919 1.99 kardel getmicrotime(&now);
920 1.99 kardel timersub(&tv, &now, &tv);
921 1.99 kardel return tvtohz(&tv);
922 1.99 kardel }
923 1.99 kardel #endif /* __HAVE_TIMECOUNTER */
924 1.99 kardel
925 1.99 kardel /*
926 1.99 kardel * Compute number of ticks in the specified amount of time.
927 1.99 kardel */
928 1.99 kardel int
929 1.99 kardel tvtohz(struct timeval *tv)
930 1.99 kardel {
931 1.99 kardel unsigned long ticks;
932 1.99 kardel long sec, usec;
933 1.99 kardel
934 1.99 kardel /*
935 1.99 kardel * If the number of usecs in the whole seconds part of the time
936 1.99 kardel * difference fits in a long, then the total number of usecs will
937 1.99 kardel * fit in an unsigned long. Compute the total and convert it to
938 1.99 kardel * ticks, rounding up and adding 1 to allow for the current tick
939 1.99 kardel * to expire. Rounding also depends on unsigned long arithmetic
940 1.99 kardel * to avoid overflow.
941 1.99 kardel *
942 1.99 kardel * Otherwise, if the number of ticks in the whole seconds part of
943 1.99 kardel * the time difference fits in a long, then convert the parts to
944 1.99 kardel * ticks separately and add, using similar rounding methods and
945 1.99 kardel * overflow avoidance. This method would work in the previous
946 1.99 kardel * case, but it is slightly slower and assumes that hz is integral.
947 1.99 kardel *
948 1.99 kardel * Otherwise, round the time difference down to the maximum
949 1.99 kardel * representable value.
950 1.99 kardel *
951 1.99 kardel * If ints are 32-bit, then the maximum value for any timeout in
952 1.99 kardel * 10ms ticks is 248 days.
953 1.99 kardel */
954 1.99 kardel sec = tv->tv_sec;
955 1.99 kardel usec = tv->tv_usec;
956 1.99 kardel
957 1.99 kardel if (usec < 0) {
958 1.99 kardel sec--;
959 1.99 kardel usec += 1000000;
960 1.99 kardel }
961 1.99 kardel
962 1.99 kardel if (sec < 0 || (sec == 0 && usec <= 0)) {
963 1.99 kardel /*
964 1.99 kardel * Would expire now or in the past. Return 0 ticks.
965 1.99 kardel * This is different from the legacy hzto() interface,
966 1.99 kardel * and callers need to check for it.
967 1.99 kardel */
968 1.99 kardel ticks = 0;
969 1.99 kardel } else if (sec <= (LONG_MAX / 1000000))
970 1.99 kardel ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
971 1.99 kardel / tick) + 1;
972 1.99 kardel else if (sec <= (LONG_MAX / hz))
973 1.99 kardel ticks = (sec * hz) +
974 1.99 kardel (((unsigned long)usec + (tick - 1)) / tick) + 1;
975 1.99 kardel else
976 1.99 kardel ticks = LONG_MAX;
977 1.99 kardel
978 1.99 kardel if (ticks > INT_MAX)
979 1.99 kardel ticks = INT_MAX;
980 1.99 kardel
981 1.99 kardel return ((int)ticks);
982 1.99 kardel }
983 1.99 kardel
984 1.99 kardel #ifndef __HAVE_TIMECOUNTER
985 1.19 cgd /*
986 1.52 thorpej * Compute number of hz until specified time. Used to compute second
987 1.52 thorpej * argument to callout_reset() from an absolute time.
988 1.19 cgd */
989 1.19 cgd int
990 1.63 thorpej hzto(struct timeval *tv)
991 1.19 cgd {
992 1.62 thorpej unsigned long ticks;
993 1.62 thorpej long sec, usec;
994 1.19 cgd int s;
995 1.19 cgd
996 1.19 cgd /*
997 1.62 thorpej * If the number of usecs in the whole seconds part of the time
998 1.62 thorpej * difference fits in a long, then the total number of usecs will
999 1.62 thorpej * fit in an unsigned long. Compute the total and convert it to
1000 1.62 thorpej * ticks, rounding up and adding 1 to allow for the current tick
1001 1.62 thorpej * to expire. Rounding also depends on unsigned long arithmetic
1002 1.62 thorpej * to avoid overflow.
1003 1.19 cgd *
1004 1.62 thorpej * Otherwise, if the number of ticks in the whole seconds part of
1005 1.62 thorpej * the time difference fits in a long, then convert the parts to
1006 1.62 thorpej * ticks separately and add, using similar rounding methods and
1007 1.62 thorpej * overflow avoidance. This method would work in the previous
1008 1.62 thorpej * case, but it is slightly slower and assume that hz is integral.
1009 1.62 thorpej *
1010 1.62 thorpej * Otherwise, round the time difference down to the maximum
1011 1.62 thorpej * representable value.
1012 1.62 thorpej *
1013 1.62 thorpej * If ints are 32-bit, then the maximum value for any timeout in
1014 1.62 thorpej * 10ms ticks is 248 days.
1015 1.19 cgd */
1016 1.40 mycroft s = splclock();
1017 1.19 cgd sec = tv->tv_sec - time.tv_sec;
1018 1.62 thorpej usec = tv->tv_usec - time.tv_usec;
1019 1.62 thorpej splx(s);
1020 1.62 thorpej
1021 1.62 thorpej if (usec < 0) {
1022 1.62 thorpej sec--;
1023 1.62 thorpej usec += 1000000;
1024 1.62 thorpej }
1025 1.62 thorpej
1026 1.62 thorpej if (sec < 0 || (sec == 0 && usec <= 0)) {
1027 1.62 thorpej /*
1028 1.62 thorpej * Would expire now or in the past. Return 0 ticks.
1029 1.62 thorpej * This is different from the legacy hzto() interface,
1030 1.62 thorpej * and callers need to check for it.
1031 1.62 thorpej */
1032 1.62 thorpej ticks = 0;
1033 1.62 thorpej } else if (sec <= (LONG_MAX / 1000000))
1034 1.62 thorpej ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
1035 1.62 thorpej / tick) + 1;
1036 1.62 thorpej else if (sec <= (LONG_MAX / hz))
1037 1.62 thorpej ticks = (sec * hz) +
1038 1.62 thorpej (((unsigned long)usec + (tick - 1)) / tick) + 1;
1039 1.19 cgd else
1040 1.62 thorpej ticks = LONG_MAX;
1041 1.62 thorpej
1042 1.62 thorpej if (ticks > INT_MAX)
1043 1.62 thorpej ticks = INT_MAX;
1044 1.62 thorpej
1045 1.62 thorpej return ((int)ticks);
1046 1.19 cgd }
1047 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
1048 1.99 kardel
1049 1.99 kardel /*
1050 1.99 kardel * Compute number of ticks in the specified amount of time.
1051 1.99 kardel */
1052 1.99 kardel int
1053 1.99 kardel tstohz(struct timespec *ts)
1054 1.99 kardel {
1055 1.99 kardel struct timeval tv;
1056 1.99 kardel
1057 1.99 kardel /*
1058 1.99 kardel * usec has great enough resolution for hz, so convert to a
1059 1.99 kardel * timeval and use tvtohz() above.
1060 1.99 kardel */
1061 1.99 kardel TIMESPEC_TO_TIMEVAL(&tv, ts);
1062 1.99 kardel return tvtohz(&tv);
1063 1.99 kardel }
1064 1.19 cgd
1065 1.19 cgd /*
1066 1.19 cgd * Start profiling on a process.
1067 1.19 cgd *
1068 1.19 cgd * Kernel profiling passes proc0 which never exits and hence
1069 1.19 cgd * keeps the profile clock running constantly.
1070 1.19 cgd */
1071 1.19 cgd void
1072 1.63 thorpej startprofclock(struct proc *p)
1073 1.19 cgd {
1074 1.19 cgd
1075 1.105 ad LOCK_ASSERT(mutex_owned(&p->p_stmutex));
1076 1.105 ad
1077 1.105 ad if ((p->p_stflag & PST_PROFIL) == 0) {
1078 1.105 ad p->p_stflag |= PST_PROFIL;
1079 1.80 briggs /*
1080 1.80 briggs * This is only necessary if using the clock as the
1081 1.80 briggs * profiling source.
1082 1.80 briggs */
1083 1.70 sommerfe if (++profprocs == 1 && stathz != 0)
1084 1.70 sommerfe psdiv = psratio;
1085 1.19 cgd }
1086 1.19 cgd }
1087 1.19 cgd
1088 1.19 cgd /*
1089 1.19 cgd * Stop profiling on a process.
1090 1.19 cgd */
1091 1.19 cgd void
1092 1.63 thorpej stopprofclock(struct proc *p)
1093 1.19 cgd {
1094 1.19 cgd
1095 1.105 ad LOCK_ASSERT(mutex_owned(&p->p_stmutex));
1096 1.105 ad
1097 1.105 ad if (p->p_stflag & PST_PROFIL) {
1098 1.105 ad p->p_stflag &= ~PST_PROFIL;
1099 1.80 briggs /*
1100 1.80 briggs * This is only necessary if using the clock as the
1101 1.80 briggs * profiling source.
1102 1.80 briggs */
1103 1.70 sommerfe if (--profprocs == 0 && stathz != 0)
1104 1.70 sommerfe psdiv = 1;
1105 1.19 cgd }
1106 1.19 cgd }
1107 1.19 cgd
1108 1.80 briggs #if defined(PERFCTRS)
1109 1.80 briggs /*
1110 1.80 briggs * Independent profiling "tick" in case we're using a separate
1111 1.80 briggs * clock or profiling event source. Currently, that's just
1112 1.80 briggs * performance counters--hence the wrapper.
1113 1.80 briggs */
1114 1.80 briggs void
1115 1.80 briggs proftick(struct clockframe *frame)
1116 1.80 briggs {
1117 1.80 briggs #ifdef GPROF
1118 1.93 perry struct gmonparam *g;
1119 1.93 perry intptr_t i;
1120 1.80 briggs #endif
1121 1.105 ad struct lwp *l;
1122 1.80 briggs struct proc *p;
1123 1.80 briggs
1124 1.105 ad l = curlwp;
1125 1.105 ad p = (l ? l->l_proc : NULL);
1126 1.80 briggs if (CLKF_USERMODE(frame)) {
1127 1.105 ad mutex_spin_enter(&p->p_stmutex);
1128 1.105 ad if (p->p_stflag & PST_PROFIL)
1129 1.105 ad addupc_intr(l, CLKF_PC(frame));
1130 1.105 ad mutex_spin_exit(&p->p_stmutex);
1131 1.80 briggs } else {
1132 1.80 briggs #ifdef GPROF
1133 1.80 briggs g = &_gmonparam;
1134 1.80 briggs if (g->state == GMON_PROF_ON) {
1135 1.80 briggs i = CLKF_PC(frame) - g->lowpc;
1136 1.80 briggs if (i < g->textsize) {
1137 1.80 briggs i /= HISTFRACTION * sizeof(*g->kcount);
1138 1.80 briggs g->kcount[i]++;
1139 1.80 briggs }
1140 1.80 briggs }
1141 1.80 briggs #endif
1142 1.93 perry #ifdef PROC_PC
1143 1.105 ad if (p != NULL) {
1144 1.105 ad mutex_spin_enter(&p->p_stmutex);
1145 1.105 ad if (p->p_stflag & PST_PROFIL))
1146 1.105 ad addupc_intr(l, PROC_PC(p));
1147 1.105 ad mutex_spin_exit(&p->p_stmutex);
1148 1.105 ad }
1149 1.93 perry #endif
1150 1.80 briggs }
1151 1.80 briggs }
1152 1.80 briggs #endif
1153 1.80 briggs
1154 1.19 cgd /*
1155 1.19 cgd * Statistics clock. Grab profile sample, and if divider reaches 0,
1156 1.19 cgd * do process and kernel statistics.
1157 1.19 cgd */
1158 1.19 cgd void
1159 1.63 thorpej statclock(struct clockframe *frame)
1160 1.19 cgd {
1161 1.19 cgd #ifdef GPROF
1162 1.55 augustss struct gmonparam *g;
1163 1.68 eeh intptr_t i;
1164 1.19 cgd #endif
1165 1.60 thorpej struct cpu_info *ci = curcpu();
1166 1.60 thorpej struct schedstate_percpu *spc = &ci->ci_schedstate;
1167 1.55 augustss struct proc *p;
1168 1.98 christos struct lwp *l;
1169 1.19 cgd
1170 1.70 sommerfe /*
1171 1.70 sommerfe * Notice changes in divisor frequency, and adjust clock
1172 1.70 sommerfe * frequency accordingly.
1173 1.70 sommerfe */
1174 1.70 sommerfe if (spc->spc_psdiv != psdiv) {
1175 1.70 sommerfe spc->spc_psdiv = psdiv;
1176 1.70 sommerfe spc->spc_pscnt = psdiv;
1177 1.70 sommerfe if (psdiv == 1) {
1178 1.70 sommerfe setstatclockrate(stathz);
1179 1.70 sommerfe } else {
1180 1.93 perry setstatclockrate(profhz);
1181 1.70 sommerfe }
1182 1.70 sommerfe }
1183 1.98 christos l = curlwp;
1184 1.105 ad if ((p = (l ? l->l_proc : NULL)) != NULL)
1185 1.105 ad mutex_spin_enter(&p->p_stmutex);
1186 1.19 cgd if (CLKF_USERMODE(frame)) {
1187 1.97 elad KASSERT(p != NULL);
1188 1.97 elad
1189 1.105 ad if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
1190 1.105 ad addupc_intr(l, CLKF_PC(frame));
1191 1.105 ad if (--spc->spc_pscnt > 0) {
1192 1.105 ad mutex_spin_exit(&p->p_stmutex);
1193 1.19 cgd return;
1194 1.105 ad }
1195 1.105 ad
1196 1.19 cgd /*
1197 1.19 cgd * Came from user mode; CPU was in user state.
1198 1.19 cgd * If this process is being profiled record the tick.
1199 1.19 cgd */
1200 1.19 cgd p->p_uticks++;
1201 1.19 cgd if (p->p_nice > NZERO)
1202 1.60 thorpej spc->spc_cp_time[CP_NICE]++;
1203 1.19 cgd else
1204 1.60 thorpej spc->spc_cp_time[CP_USER]++;
1205 1.19 cgd } else {
1206 1.19 cgd #ifdef GPROF
1207 1.19 cgd /*
1208 1.19 cgd * Kernel statistics are just like addupc_intr, only easier.
1209 1.19 cgd */
1210 1.19 cgd g = &_gmonparam;
1211 1.80 briggs if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
1212 1.19 cgd i = CLKF_PC(frame) - g->lowpc;
1213 1.19 cgd if (i < g->textsize) {
1214 1.19 cgd i /= HISTFRACTION * sizeof(*g->kcount);
1215 1.19 cgd g->kcount[i]++;
1216 1.19 cgd }
1217 1.19 cgd }
1218 1.19 cgd #endif
1219 1.82 thorpej #ifdef LWP_PC
1220 1.105 ad if (p && profsrc == PROFSRC_CLOCK && (p->p_stflag & PST_PROFIL))
1221 1.105 ad addupc_intr(l, LWP_PC(l));
1222 1.72 mycroft #endif
1223 1.105 ad if (--spc->spc_pscnt > 0) {
1224 1.105 ad if (p != NULL)
1225 1.105 ad mutex_spin_exit(&p->p_stmutex);
1226 1.19 cgd return;
1227 1.105 ad }
1228 1.19 cgd /*
1229 1.19 cgd * Came from kernel mode, so we were:
1230 1.19 cgd * - handling an interrupt,
1231 1.19 cgd * - doing syscall or trap work on behalf of the current
1232 1.19 cgd * user process, or
1233 1.19 cgd * - spinning in the idle loop.
1234 1.19 cgd * Whichever it is, charge the time as appropriate.
1235 1.19 cgd * Note that we charge interrupts to the current process,
1236 1.19 cgd * regardless of whether they are ``for'' that process,
1237 1.19 cgd * so that we know how much of its real time was spent
1238 1.19 cgd * in ``non-process'' (i.e., interrupt) work.
1239 1.19 cgd */
1240 1.19 cgd if (CLKF_INTR(frame)) {
1241 1.19 cgd if (p != NULL)
1242 1.19 cgd p->p_iticks++;
1243 1.60 thorpej spc->spc_cp_time[CP_INTR]++;
1244 1.19 cgd } else if (p != NULL) {
1245 1.19 cgd p->p_sticks++;
1246 1.60 thorpej spc->spc_cp_time[CP_SYS]++;
1247 1.19 cgd } else
1248 1.60 thorpej spc->spc_cp_time[CP_IDLE]++;
1249 1.19 cgd }
1250 1.70 sommerfe spc->spc_pscnt = psdiv;
1251 1.19 cgd
1252 1.97 elad if (p != NULL) {
1253 1.45 ross ++p->p_cpticks;
1254 1.105 ad mutex_spin_exit(&p->p_stmutex);
1255 1.105 ad
1256 1.45 ross /*
1257 1.93 perry * If no separate schedclock is provided, call it here
1258 1.91 yamt * at about 16 Hz.
1259 1.45 ross */
1260 1.60 thorpej if (schedhz == 0)
1261 1.91 yamt if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
1262 1.82 thorpej schedclock(l);
1263 1.91 yamt ci->ci_schedstate.spc_schedticks = statscheddiv;
1264 1.91 yamt }
1265 1.19 cgd }
1266 1.19 cgd }
1267 1.27 jonathan
1268 1.99 kardel #ifndef __HAVE_TIMECOUNTER
1269 1.27 jonathan #ifdef NTP /* NTP phase-locked loop in kernel */
1270 1.27 jonathan /*
1271 1.27 jonathan * hardupdate() - local clock update
1272 1.27 jonathan *
1273 1.27 jonathan * This routine is called by ntp_adjtime() to update the local clock
1274 1.27 jonathan * phase and frequency. The implementation is of an adaptive-parameter,
1275 1.27 jonathan * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
1276 1.27 jonathan * time and frequency offset estimates for each call. If the kernel PPS
1277 1.27 jonathan * discipline code is configured (PPS_SYNC), the PPS signal itself
1278 1.27 jonathan * determines the new time offset, instead of the calling argument.
1279 1.27 jonathan * Presumably, calls to ntp_adjtime() occur only when the caller
1280 1.27 jonathan * believes the local clock is valid within some bound (+-128 ms with
1281 1.27 jonathan * NTP). If the caller's time is far different than the PPS time, an
1282 1.27 jonathan * argument will ensue, and it's not clear who will lose.
1283 1.27 jonathan *
1284 1.27 jonathan * For uncompensated quartz crystal oscillatores and nominal update
1285 1.27 jonathan * intervals less than 1024 s, operation should be in phase-lock mode
1286 1.27 jonathan * (STA_FLL = 0), where the loop is disciplined to phase. For update
1287 1.27 jonathan * intervals greater than thiss, operation should be in frequency-lock
1288 1.27 jonathan * mode (STA_FLL = 1), where the loop is disciplined to frequency.
1289 1.27 jonathan *
1290 1.27 jonathan * Note: splclock() is in effect.
1291 1.27 jonathan */
1292 1.27 jonathan void
1293 1.63 thorpej hardupdate(long offset)
1294 1.27 jonathan {
1295 1.27 jonathan long ltemp, mtemp;
1296 1.27 jonathan
1297 1.27 jonathan if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
1298 1.27 jonathan return;
1299 1.27 jonathan ltemp = offset;
1300 1.27 jonathan #ifdef PPS_SYNC
1301 1.27 jonathan if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
1302 1.27 jonathan ltemp = pps_offset;
1303 1.27 jonathan #endif /* PPS_SYNC */
1304 1.27 jonathan
1305 1.27 jonathan /*
1306 1.27 jonathan * Scale the phase adjustment and clamp to the operating range.
1307 1.27 jonathan */
1308 1.27 jonathan if (ltemp > MAXPHASE)
1309 1.27 jonathan time_offset = MAXPHASE << SHIFT_UPDATE;
1310 1.27 jonathan else if (ltemp < -MAXPHASE)
1311 1.27 jonathan time_offset = -(MAXPHASE << SHIFT_UPDATE);
1312 1.27 jonathan else
1313 1.27 jonathan time_offset = ltemp << SHIFT_UPDATE;
1314 1.27 jonathan
1315 1.27 jonathan /*
1316 1.27 jonathan * Select whether the frequency is to be controlled and in which
1317 1.27 jonathan * mode (PLL or FLL). Clamp to the operating range. Ugly
1318 1.27 jonathan * multiply/divide should be replaced someday.
1319 1.27 jonathan */
1320 1.27 jonathan if (time_status & STA_FREQHOLD || time_reftime == 0)
1321 1.27 jonathan time_reftime = time.tv_sec;
1322 1.27 jonathan mtemp = time.tv_sec - time_reftime;
1323 1.27 jonathan time_reftime = time.tv_sec;
1324 1.27 jonathan if (time_status & STA_FLL) {
1325 1.27 jonathan if (mtemp >= MINSEC) {
1326 1.27 jonathan ltemp = ((time_offset / mtemp) << (SHIFT_USEC -
1327 1.27 jonathan SHIFT_UPDATE));
1328 1.27 jonathan if (ltemp < 0)
1329 1.27 jonathan time_freq -= -ltemp >> SHIFT_KH;
1330 1.27 jonathan else
1331 1.27 jonathan time_freq += ltemp >> SHIFT_KH;
1332 1.27 jonathan }
1333 1.27 jonathan } else {
1334 1.27 jonathan if (mtemp < MAXSEC) {
1335 1.27 jonathan ltemp *= mtemp;
1336 1.27 jonathan if (ltemp < 0)
1337 1.27 jonathan time_freq -= -ltemp >> (time_constant +
1338 1.27 jonathan time_constant + SHIFT_KF -
1339 1.27 jonathan SHIFT_USEC);
1340 1.27 jonathan else
1341 1.27 jonathan time_freq += ltemp >> (time_constant +
1342 1.27 jonathan time_constant + SHIFT_KF -
1343 1.27 jonathan SHIFT_USEC);
1344 1.27 jonathan }
1345 1.27 jonathan }
1346 1.27 jonathan if (time_freq > time_tolerance)
1347 1.27 jonathan time_freq = time_tolerance;
1348 1.27 jonathan else if (time_freq < -time_tolerance)
1349 1.27 jonathan time_freq = -time_tolerance;
1350 1.27 jonathan }
1351 1.27 jonathan
1352 1.27 jonathan #ifdef PPS_SYNC
1353 1.27 jonathan /*
1354 1.27 jonathan * hardpps() - discipline CPU clock oscillator to external PPS signal
1355 1.27 jonathan *
1356 1.27 jonathan * This routine is called at each PPS interrupt in order to discipline
1357 1.27 jonathan * the CPU clock oscillator to the PPS signal. It measures the PPS phase
1358 1.27 jonathan * and leaves it in a handy spot for the hardclock() routine. It
1359 1.27 jonathan * integrates successive PPS phase differences and calculates the
1360 1.27 jonathan * frequency offset. This is used in hardclock() to discipline the CPU
1361 1.27 jonathan * clock oscillator so that intrinsic frequency error is cancelled out.
1362 1.27 jonathan * The code requires the caller to capture the time and hardware counter
1363 1.27 jonathan * value at the on-time PPS signal transition.
1364 1.27 jonathan *
1365 1.27 jonathan * Note that, on some Unix systems, this routine runs at an interrupt
1366 1.27 jonathan * priority level higher than the timer interrupt routine hardclock().
1367 1.27 jonathan * Therefore, the variables used are distinct from the hardclock()
1368 1.27 jonathan * variables, except for certain exceptions: The PPS frequency pps_freq
1369 1.27 jonathan * and phase pps_offset variables are determined by this routine and
1370 1.27 jonathan * updated atomically. The time_tolerance variable can be considered a
1371 1.27 jonathan * constant, since it is infrequently changed, and then only when the
1372 1.27 jonathan * PPS signal is disabled. The watchdog counter pps_valid is updated
1373 1.27 jonathan * once per second by hardclock() and is atomically cleared in this
1374 1.27 jonathan * routine.
1375 1.27 jonathan */
1376 1.27 jonathan void
1377 1.63 thorpej hardpps(struct timeval *tvp, /* time at PPS */
1378 1.63 thorpej long usec /* hardware counter at PPS */)
1379 1.27 jonathan {
1380 1.27 jonathan long u_usec, v_usec, bigtick;
1381 1.27 jonathan long cal_sec, cal_usec;
1382 1.27 jonathan
1383 1.27 jonathan /*
1384 1.27 jonathan * An occasional glitch can be produced when the PPS interrupt
1385 1.27 jonathan * occurs in the hardclock() routine before the time variable is
1386 1.27 jonathan * updated. Here the offset is discarded when the difference
1387 1.27 jonathan * between it and the last one is greater than tick/2, but not
1388 1.27 jonathan * if the interval since the first discard exceeds 30 s.
1389 1.27 jonathan */
1390 1.27 jonathan time_status |= STA_PPSSIGNAL;
1391 1.27 jonathan time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
1392 1.27 jonathan pps_valid = 0;
1393 1.27 jonathan u_usec = -tvp->tv_usec;
1394 1.27 jonathan if (u_usec < -500000)
1395 1.27 jonathan u_usec += 1000000;
1396 1.27 jonathan v_usec = pps_offset - u_usec;
1397 1.27 jonathan if (v_usec < 0)
1398 1.27 jonathan v_usec = -v_usec;
1399 1.27 jonathan if (v_usec > (tick >> 1)) {
1400 1.27 jonathan if (pps_glitch > MAXGLITCH) {
1401 1.27 jonathan pps_glitch = 0;
1402 1.27 jonathan pps_tf[2] = u_usec;
1403 1.27 jonathan pps_tf[1] = u_usec;
1404 1.27 jonathan } else {
1405 1.27 jonathan pps_glitch++;
1406 1.27 jonathan u_usec = pps_offset;
1407 1.27 jonathan }
1408 1.27 jonathan } else
1409 1.27 jonathan pps_glitch = 0;
1410 1.27 jonathan
1411 1.27 jonathan /*
1412 1.27 jonathan * A three-stage median filter is used to help deglitch the pps
1413 1.27 jonathan * time. The median sample becomes the time offset estimate; the
1414 1.27 jonathan * difference between the other two samples becomes the time
1415 1.27 jonathan * dispersion (jitter) estimate.
1416 1.27 jonathan */
1417 1.27 jonathan pps_tf[2] = pps_tf[1];
1418 1.27 jonathan pps_tf[1] = pps_tf[0];
1419 1.27 jonathan pps_tf[0] = u_usec;
1420 1.27 jonathan if (pps_tf[0] > pps_tf[1]) {
1421 1.27 jonathan if (pps_tf[1] > pps_tf[2]) {
1422 1.27 jonathan pps_offset = pps_tf[1]; /* 0 1 2 */
1423 1.27 jonathan v_usec = pps_tf[0] - pps_tf[2];
1424 1.27 jonathan } else if (pps_tf[2] > pps_tf[0]) {
1425 1.27 jonathan pps_offset = pps_tf[0]; /* 2 0 1 */
1426 1.27 jonathan v_usec = pps_tf[2] - pps_tf[1];
1427 1.27 jonathan } else {
1428 1.27 jonathan pps_offset = pps_tf[2]; /* 0 2 1 */
1429 1.27 jonathan v_usec = pps_tf[0] - pps_tf[1];
1430 1.27 jonathan }
1431 1.27 jonathan } else {
1432 1.27 jonathan if (pps_tf[1] < pps_tf[2]) {
1433 1.27 jonathan pps_offset = pps_tf[1]; /* 2 1 0 */
1434 1.27 jonathan v_usec = pps_tf[2] - pps_tf[0];
1435 1.27 jonathan } else if (pps_tf[2] < pps_tf[0]) {
1436 1.27 jonathan pps_offset = pps_tf[0]; /* 1 0 2 */
1437 1.27 jonathan v_usec = pps_tf[1] - pps_tf[2];
1438 1.27 jonathan } else {
1439 1.27 jonathan pps_offset = pps_tf[2]; /* 1 2 0 */
1440 1.27 jonathan v_usec = pps_tf[1] - pps_tf[0];
1441 1.27 jonathan }
1442 1.27 jonathan }
1443 1.27 jonathan if (v_usec > MAXTIME)
1444 1.27 jonathan pps_jitcnt++;
1445 1.27 jonathan v_usec = (v_usec << PPS_AVG) - pps_jitter;
1446 1.27 jonathan if (v_usec < 0)
1447 1.27 jonathan pps_jitter -= -v_usec >> PPS_AVG;
1448 1.27 jonathan else
1449 1.27 jonathan pps_jitter += v_usec >> PPS_AVG;
1450 1.27 jonathan if (pps_jitter > (MAXTIME >> 1))
1451 1.27 jonathan time_status |= STA_PPSJITTER;
1452 1.27 jonathan
1453 1.27 jonathan /*
1454 1.27 jonathan * During the calibration interval adjust the starting time when
1455 1.27 jonathan * the tick overflows. At the end of the interval compute the
1456 1.27 jonathan * duration of the interval and the difference of the hardware
1457 1.27 jonathan * counters at the beginning and end of the interval. This code
1458 1.27 jonathan * is deliciously complicated by the fact valid differences may
1459 1.27 jonathan * exceed the value of tick when using long calibration
1460 1.27 jonathan * intervals and small ticks. Note that the counter can be
1461 1.27 jonathan * greater than tick if caught at just the wrong instant, but
1462 1.27 jonathan * the values returned and used here are correct.
1463 1.27 jonathan */
1464 1.27 jonathan bigtick = (long)tick << SHIFT_USEC;
1465 1.27 jonathan pps_usec -= pps_freq;
1466 1.27 jonathan if (pps_usec >= bigtick)
1467 1.27 jonathan pps_usec -= bigtick;
1468 1.27 jonathan if (pps_usec < 0)
1469 1.27 jonathan pps_usec += bigtick;
1470 1.27 jonathan pps_time.tv_sec++;
1471 1.27 jonathan pps_count++;
1472 1.27 jonathan if (pps_count < (1 << pps_shift))
1473 1.27 jonathan return;
1474 1.27 jonathan pps_count = 0;
1475 1.27 jonathan pps_calcnt++;
1476 1.27 jonathan u_usec = usec << SHIFT_USEC;
1477 1.27 jonathan v_usec = pps_usec - u_usec;
1478 1.27 jonathan if (v_usec >= bigtick >> 1)
1479 1.27 jonathan v_usec -= bigtick;
1480 1.27 jonathan if (v_usec < -(bigtick >> 1))
1481 1.27 jonathan v_usec += bigtick;
1482 1.27 jonathan if (v_usec < 0)
1483 1.27 jonathan v_usec = -(-v_usec >> pps_shift);
1484 1.27 jonathan else
1485 1.27 jonathan v_usec = v_usec >> pps_shift;
1486 1.27 jonathan pps_usec = u_usec;
1487 1.27 jonathan cal_sec = tvp->tv_sec;
1488 1.27 jonathan cal_usec = tvp->tv_usec;
1489 1.27 jonathan cal_sec -= pps_time.tv_sec;
1490 1.27 jonathan cal_usec -= pps_time.tv_usec;
1491 1.27 jonathan if (cal_usec < 0) {
1492 1.27 jonathan cal_usec += 1000000;
1493 1.27 jonathan cal_sec--;
1494 1.27 jonathan }
1495 1.27 jonathan pps_time = *tvp;
1496 1.27 jonathan
1497 1.27 jonathan /*
1498 1.27 jonathan * Check for lost interrupts, noise, excessive jitter and
1499 1.27 jonathan * excessive frequency error. The number of timer ticks during
1500 1.27 jonathan * the interval may vary +-1 tick. Add to this a margin of one
1501 1.27 jonathan * tick for the PPS signal jitter and maximum frequency
1502 1.27 jonathan * deviation. If the limits are exceeded, the calibration
1503 1.27 jonathan * interval is reset to the minimum and we start over.
1504 1.27 jonathan */
1505 1.27 jonathan u_usec = (long)tick << 1;
1506 1.27 jonathan if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec))
1507 1.27 jonathan || (cal_sec == 0 && cal_usec < u_usec))
1508 1.27 jonathan || v_usec > time_tolerance || v_usec < -time_tolerance) {
1509 1.27 jonathan pps_errcnt++;
1510 1.27 jonathan pps_shift = PPS_SHIFT;
1511 1.27 jonathan pps_intcnt = 0;
1512 1.27 jonathan time_status |= STA_PPSERROR;
1513 1.27 jonathan return;
1514 1.27 jonathan }
1515 1.27 jonathan
1516 1.27 jonathan /*
1517 1.27 jonathan * A three-stage median filter is used to help deglitch the pps
1518 1.27 jonathan * frequency. The median sample becomes the frequency offset
1519 1.27 jonathan * estimate; the difference between the other two samples
1520 1.27 jonathan * becomes the frequency dispersion (stability) estimate.
1521 1.27 jonathan */
1522 1.27 jonathan pps_ff[2] = pps_ff[1];
1523 1.27 jonathan pps_ff[1] = pps_ff[0];
1524 1.27 jonathan pps_ff[0] = v_usec;
1525 1.27 jonathan if (pps_ff[0] > pps_ff[1]) {
1526 1.27 jonathan if (pps_ff[1] > pps_ff[2]) {
1527 1.27 jonathan u_usec = pps_ff[1]; /* 0 1 2 */
1528 1.27 jonathan v_usec = pps_ff[0] - pps_ff[2];
1529 1.27 jonathan } else if (pps_ff[2] > pps_ff[0]) {
1530 1.27 jonathan u_usec = pps_ff[0]; /* 2 0 1 */
1531 1.27 jonathan v_usec = pps_ff[2] - pps_ff[1];
1532 1.27 jonathan } else {
1533 1.27 jonathan u_usec = pps_ff[2]; /* 0 2 1 */
1534 1.27 jonathan v_usec = pps_ff[0] - pps_ff[1];
1535 1.27 jonathan }
1536 1.27 jonathan } else {
1537 1.27 jonathan if (pps_ff[1] < pps_ff[2]) {
1538 1.27 jonathan u_usec = pps_ff[1]; /* 2 1 0 */
1539 1.27 jonathan v_usec = pps_ff[2] - pps_ff[0];
1540 1.27 jonathan } else if (pps_ff[2] < pps_ff[0]) {
1541 1.27 jonathan u_usec = pps_ff[0]; /* 1 0 2 */
1542 1.27 jonathan v_usec = pps_ff[1] - pps_ff[2];
1543 1.27 jonathan } else {
1544 1.27 jonathan u_usec = pps_ff[2]; /* 1 2 0 */
1545 1.27 jonathan v_usec = pps_ff[1] - pps_ff[0];
1546 1.27 jonathan }
1547 1.27 jonathan }
1548 1.27 jonathan
1549 1.27 jonathan /*
1550 1.27 jonathan * Here the frequency dispersion (stability) is updated. If it
1551 1.27 jonathan * is less than one-fourth the maximum (MAXFREQ), the frequency
1552 1.27 jonathan * offset is updated as well, but clamped to the tolerance. It
1553 1.27 jonathan * will be processed later by the hardclock() routine.
1554 1.27 jonathan */
1555 1.27 jonathan v_usec = (v_usec >> 1) - pps_stabil;
1556 1.27 jonathan if (v_usec < 0)
1557 1.27 jonathan pps_stabil -= -v_usec >> PPS_AVG;
1558 1.27 jonathan else
1559 1.27 jonathan pps_stabil += v_usec >> PPS_AVG;
1560 1.27 jonathan if (pps_stabil > MAXFREQ >> 2) {
1561 1.27 jonathan pps_stbcnt++;
1562 1.27 jonathan time_status |= STA_PPSWANDER;
1563 1.27 jonathan return;
1564 1.27 jonathan }
1565 1.27 jonathan if (time_status & STA_PPSFREQ) {
1566 1.27 jonathan if (u_usec < 0) {
1567 1.27 jonathan pps_freq -= -u_usec >> PPS_AVG;
1568 1.27 jonathan if (pps_freq < -time_tolerance)
1569 1.27 jonathan pps_freq = -time_tolerance;
1570 1.27 jonathan u_usec = -u_usec;
1571 1.27 jonathan } else {
1572 1.27 jonathan pps_freq += u_usec >> PPS_AVG;
1573 1.27 jonathan if (pps_freq > time_tolerance)
1574 1.27 jonathan pps_freq = time_tolerance;
1575 1.27 jonathan }
1576 1.27 jonathan }
1577 1.27 jonathan
1578 1.27 jonathan /*
1579 1.27 jonathan * Here the calibration interval is adjusted. If the maximum
1580 1.27 jonathan * time difference is greater than tick / 4, reduce the interval
1581 1.27 jonathan * by half. If this is not the case for four consecutive
1582 1.27 jonathan * intervals, double the interval.
1583 1.27 jonathan */
1584 1.27 jonathan if (u_usec << pps_shift > bigtick >> 2) {
1585 1.27 jonathan pps_intcnt = 0;
1586 1.27 jonathan if (pps_shift > PPS_SHIFT)
1587 1.27 jonathan pps_shift--;
1588 1.27 jonathan } else if (pps_intcnt >= 4) {
1589 1.27 jonathan pps_intcnt = 0;
1590 1.27 jonathan if (pps_shift < PPS_SHIFTMAX)
1591 1.27 jonathan pps_shift++;
1592 1.27 jonathan } else
1593 1.27 jonathan pps_intcnt++;
1594 1.27 jonathan }
1595 1.27 jonathan #endif /* PPS_SYNC */
1596 1.27 jonathan #endif /* NTP */
1597 1.95 christos
1598 1.99 kardel /* timecounter compat functions */
1599 1.99 kardel void
1600 1.95 christos nanotime(struct timespec *ts)
1601 1.95 christos {
1602 1.95 christos struct timeval tv;
1603 1.95 christos
1604 1.95 christos microtime(&tv);
1605 1.95 christos TIMEVAL_TO_TIMESPEC(&tv, ts);
1606 1.95 christos }
1607 1.99 kardel
1608 1.99 kardel void
1609 1.99 kardel getbinuptime(struct bintime *bt)
1610 1.99 kardel {
1611 1.99 kardel struct timeval tv;
1612 1.99 kardel
1613 1.99 kardel microtime(&tv);
1614 1.99 kardel timeval2bintime(&tv, bt);
1615 1.99 kardel }
1616 1.99 kardel
1617 1.99 kardel void
1618 1.99 kardel nanouptime(struct timespec *tsp)
1619 1.99 kardel {
1620 1.99 kardel int s;
1621 1.99 kardel
1622 1.99 kardel s = splclock();
1623 1.99 kardel TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
1624 1.99 kardel splx(s);
1625 1.99 kardel }
1626 1.99 kardel
1627 1.99 kardel void
1628 1.99 kardel getnanouptime(struct timespec *tsp)
1629 1.99 kardel {
1630 1.99 kardel int s;
1631 1.99 kardel
1632 1.99 kardel s = splclock();
1633 1.99 kardel TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
1634 1.99 kardel splx(s);
1635 1.99 kardel }
1636 1.99 kardel
1637 1.99 kardel void
1638 1.99 kardel getmicrouptime(struct timeval *tvp)
1639 1.99 kardel {
1640 1.99 kardel int s;
1641 1.99 kardel
1642 1.99 kardel s = splclock();
1643 1.99 kardel *tvp = mono_time;
1644 1.99 kardel splx(s);
1645 1.99 kardel }
1646 1.99 kardel
1647 1.99 kardel void
1648 1.99 kardel getnanotime(struct timespec *tsp)
1649 1.99 kardel {
1650 1.99 kardel int s;
1651 1.99 kardel
1652 1.99 kardel s = splclock();
1653 1.99 kardel TIMEVAL_TO_TIMESPEC(&time, tsp);
1654 1.99 kardel splx(s);
1655 1.99 kardel }
1656 1.99 kardel
1657 1.99 kardel void
1658 1.99 kardel getmicrotime(struct timeval *tvp)
1659 1.99 kardel {
1660 1.99 kardel int s;
1661 1.99 kardel
1662 1.99 kardel s = splclock();
1663 1.99 kardel *tvp = time;
1664 1.99 kardel splx(s);
1665 1.99 kardel }
1666 1.99 kardel #endif /* !__HAVE_TIMECOUNTER */
1667