xref: /freebsd/sys/kern/kern_tc.c (revision 4232b8b1341f8d46987e09760eb36e2b052b29a9)
1 /*-
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  */
9 
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
12 
13 #include "opt_ntp.h"
14 
15 #include <sys/param.h>
16 #include <sys/kernel.h>
17 #include <sys/sysctl.h>
18 #include <sys/syslog.h>
19 #include <sys/systm.h>
20 #include <sys/timepps.h>
21 #include <sys/timetc.h>
22 #include <sys/timex.h>
23 
24 /*
25  * A large step happens on boot.  This constant detects such steps.
26  * It is relatively small so that ntp_update_second gets called enough
27  * in the typical 'missed a couple of seconds' case, but doesn't loop
28  * forever when the time step is large.
29  */
30 #define LARGE_STEP	200
31 
32 /*
33  * Implement a dummy timecounter which we can use until we get a real one
34  * in the air.  This allows the console and other early stuff to use
35  * time services.
36  */
37 
38 static u_int
39 dummy_get_timecount(struct timecounter *tc)
40 {
41 	static u_int now;
42 
43 	return (++now);
44 }
45 
46 static struct timecounter dummy_timecounter = {
47 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
48 };
49 
50 struct timehands {
51 	/* These fields must be initialized by the driver. */
52 	struct timecounter	*th_counter;
53 	int64_t			th_adjustment;
54 	u_int64_t		th_scale;
55 	u_int	 		th_offset_count;
56 	struct bintime		th_offset;
57 	struct timeval		th_microtime;
58 	struct timespec		th_nanotime;
59 	/* Fields not to be copied in tc_windup start with th_generation. */
60 	volatile u_int		th_generation;
61 	struct timehands	*th_next;
62 };
63 
64 static struct timehands th0;
65 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
66 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
67 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
68 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
69 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
70 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
71 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
72 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
73 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
74 static struct timehands th0 = {
75 	&dummy_timecounter,
76 	0,
77 	(uint64_t)-1 / 1000000,
78 	0,
79 	{1, 0},
80 	{0, 0},
81 	{0, 0},
82 	1,
83 	&th1
84 };
85 
86 static struct timehands *volatile timehands = &th0;
87 struct timecounter *timecounter = &dummy_timecounter;
88 static struct timecounter *timecounters = &dummy_timecounter;
89 
90 time_t time_second = 1;
91 time_t time_uptime = 1;
92 
93 static struct bintime boottimebin;
94 struct timeval boottime;
95 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
96 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
97     NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
98 
99 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
100 
101 static int timestepwarnings;
102 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
103     &timestepwarnings, 0, "");
104 
105 #define TC_STATS(foo) \
106 	static u_int foo; \
107 	SYSCTL_UINT(_kern_timecounter, OID_AUTO, foo, CTLFLAG_RD, &foo, 0, "");\
108 	struct __hack
109 
110 TC_STATS(nbinuptime);    TC_STATS(nnanouptime);    TC_STATS(nmicrouptime);
111 TC_STATS(nbintime);      TC_STATS(nnanotime);      TC_STATS(nmicrotime);
112 TC_STATS(ngetbinuptime); TC_STATS(ngetnanouptime); TC_STATS(ngetmicrouptime);
113 TC_STATS(ngetbintime);   TC_STATS(ngetnanotime);   TC_STATS(ngetmicrotime);
114 TC_STATS(nsetclock);
115 
116 #undef TC_STATS
117 
118 static void tc_windup(void);
119 static void cpu_tick_calibrate(int);
120 
121 static int
122 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
123 {
124 #ifdef SCTL_MASK32
125 	int tv[2];
126 
127 	if (req->flags & SCTL_MASK32) {
128 		tv[0] = boottime.tv_sec;
129 		tv[1] = boottime.tv_usec;
130 		return SYSCTL_OUT(req, tv, sizeof(tv));
131 	} else
132 #endif
133 		return SYSCTL_OUT(req, &boottime, sizeof(boottime));
134 }
135 
136 /*
137  * Return the difference between the timehands' counter value now and what
138  * was when we copied it to the timehands' offset_count.
139  */
140 static __inline u_int
141 tc_delta(struct timehands *th)
142 {
143 	struct timecounter *tc;
144 
145 	tc = th->th_counter;
146 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
147 	    tc->tc_counter_mask);
148 }
149 
150 /*
151  * Functions for reading the time.  We have to loop until we are sure that
152  * the timehands that we operated on was not updated under our feet.  See
153  * the comment in <sys/time.h> for a description of these 12 functions.
154  */
155 
156 void
157 binuptime(struct bintime *bt)
158 {
159 	struct timehands *th;
160 	u_int gen;
161 
162 	nbinuptime++;
163 	do {
164 		th = timehands;
165 		gen = th->th_generation;
166 		*bt = th->th_offset;
167 		bintime_addx(bt, th->th_scale * tc_delta(th));
168 	} while (gen == 0 || gen != th->th_generation);
169 }
170 
171 void
172 nanouptime(struct timespec *tsp)
173 {
174 	struct bintime bt;
175 
176 	nnanouptime++;
177 	binuptime(&bt);
178 	bintime2timespec(&bt, tsp);
179 }
180 
181 void
182 microuptime(struct timeval *tvp)
183 {
184 	struct bintime bt;
185 
186 	nmicrouptime++;
187 	binuptime(&bt);
188 	bintime2timeval(&bt, tvp);
189 }
190 
191 void
192 bintime(struct bintime *bt)
193 {
194 
195 	nbintime++;
196 	binuptime(bt);
197 	bintime_add(bt, &boottimebin);
198 }
199 
200 void
201 nanotime(struct timespec *tsp)
202 {
203 	struct bintime bt;
204 
205 	nnanotime++;
206 	bintime(&bt);
207 	bintime2timespec(&bt, tsp);
208 }
209 
210 void
211 microtime(struct timeval *tvp)
212 {
213 	struct bintime bt;
214 
215 	nmicrotime++;
216 	bintime(&bt);
217 	bintime2timeval(&bt, tvp);
218 }
219 
220 void
221 getbinuptime(struct bintime *bt)
222 {
223 	struct timehands *th;
224 	u_int gen;
225 
226 	ngetbinuptime++;
227 	do {
228 		th = timehands;
229 		gen = th->th_generation;
230 		*bt = th->th_offset;
231 	} while (gen == 0 || gen != th->th_generation);
232 }
233 
234 void
235 getnanouptime(struct timespec *tsp)
236 {
237 	struct timehands *th;
238 	u_int gen;
239 
240 	ngetnanouptime++;
241 	do {
242 		th = timehands;
243 		gen = th->th_generation;
244 		bintime2timespec(&th->th_offset, tsp);
245 	} while (gen == 0 || gen != th->th_generation);
246 }
247 
248 void
249 getmicrouptime(struct timeval *tvp)
250 {
251 	struct timehands *th;
252 	u_int gen;
253 
254 	ngetmicrouptime++;
255 	do {
256 		th = timehands;
257 		gen = th->th_generation;
258 		bintime2timeval(&th->th_offset, tvp);
259 	} while (gen == 0 || gen != th->th_generation);
260 }
261 
262 void
263 getbintime(struct bintime *bt)
264 {
265 	struct timehands *th;
266 	u_int gen;
267 
268 	ngetbintime++;
269 	do {
270 		th = timehands;
271 		gen = th->th_generation;
272 		*bt = th->th_offset;
273 	} while (gen == 0 || gen != th->th_generation);
274 	bintime_add(bt, &boottimebin);
275 }
276 
277 void
278 getnanotime(struct timespec *tsp)
279 {
280 	struct timehands *th;
281 	u_int gen;
282 
283 	ngetnanotime++;
284 	do {
285 		th = timehands;
286 		gen = th->th_generation;
287 		*tsp = th->th_nanotime;
288 	} while (gen == 0 || gen != th->th_generation);
289 }
290 
291 void
292 getmicrotime(struct timeval *tvp)
293 {
294 	struct timehands *th;
295 	u_int gen;
296 
297 	ngetmicrotime++;
298 	do {
299 		th = timehands;
300 		gen = th->th_generation;
301 		*tvp = th->th_microtime;
302 	} while (gen == 0 || gen != th->th_generation);
303 }
304 
305 /*
306  * Initialize a new timecounter and possibly use it.
307  */
308 void
309 tc_init(struct timecounter *tc)
310 {
311 	u_int u;
312 
313 	u = tc->tc_frequency / tc->tc_counter_mask;
314 	/* XXX: We need some margin here, 10% is a guess */
315 	u *= 11;
316 	u /= 10;
317 	if (u > hz && tc->tc_quality >= 0) {
318 		tc->tc_quality = -2000;
319 		if (bootverbose) {
320 			printf("Timecounter \"%s\" frequency %ju Hz",
321 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
322 			printf(" -- Insufficient hz, needs at least %u\n", u);
323 		}
324 	} else if (tc->tc_quality >= 0 || bootverbose) {
325 		printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
326 		    tc->tc_name, (uintmax_t)tc->tc_frequency,
327 		    tc->tc_quality);
328 	}
329 
330 	tc->tc_next = timecounters;
331 	timecounters = tc;
332 	/*
333 	 * Never automatically use a timecounter with negative quality.
334 	 * Even though we run on the dummy counter, switching here may be
335 	 * worse since this timecounter may not be monotonous.
336 	 */
337 	if (tc->tc_quality < 0)
338 		return;
339 	if (tc->tc_quality < timecounter->tc_quality)
340 		return;
341 	if (tc->tc_quality == timecounter->tc_quality &&
342 	    tc->tc_frequency < timecounter->tc_frequency)
343 		return;
344 	(void)tc->tc_get_timecount(tc);
345 	(void)tc->tc_get_timecount(tc);
346 	timecounter = tc;
347 }
348 
349 /* Report the frequency of the current timecounter. */
350 u_int64_t
351 tc_getfrequency(void)
352 {
353 
354 	return (timehands->th_counter->tc_frequency);
355 }
356 
357 /*
358  * Step our concept of UTC.  This is done by modifying our estimate of
359  * when we booted.
360  * XXX: not locked.
361  */
362 void
363 tc_setclock(struct timespec *ts)
364 {
365 	struct timespec ts2;
366 	struct bintime bt, bt2;
367 
368 	cpu_tick_calibrate(1);
369 	nsetclock++;
370 	binuptime(&bt2);
371 	timespec2bintime(ts, &bt);
372 	bintime_sub(&bt, &bt2);
373 	bintime_add(&bt2, &boottimebin);
374 	boottimebin = bt;
375 	bintime2timeval(&bt, &boottime);
376 
377 	/* XXX fiddle all the little crinkly bits around the fiords... */
378 	tc_windup();
379 	if (timestepwarnings) {
380 		bintime2timespec(&bt2, &ts2);
381 		log(LOG_INFO, "Time stepped from %jd.%09ld to %jd.%09ld\n",
382 		    (intmax_t)ts2.tv_sec, ts2.tv_nsec,
383 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
384 	}
385 	cpu_tick_calibrate(1);
386 }
387 
388 /*
389  * Initialize the next struct timehands in the ring and make
390  * it the active timehands.  Along the way we might switch to a different
391  * timecounter and/or do seconds processing in NTP.  Slightly magic.
392  */
393 static void
394 tc_windup(void)
395 {
396 	struct bintime bt;
397 	struct timehands *th, *tho;
398 	u_int64_t scale;
399 	u_int delta, ncount, ogen;
400 	int i;
401 	time_t t;
402 
403 	/*
404 	 * Make the next timehands a copy of the current one, but do not
405 	 * overwrite the generation or next pointer.  While we update
406 	 * the contents, the generation must be zero.
407 	 */
408 	tho = timehands;
409 	th = tho->th_next;
410 	ogen = th->th_generation;
411 	th->th_generation = 0;
412 	bcopy(tho, th, offsetof(struct timehands, th_generation));
413 
414 	/*
415 	 * Capture a timecounter delta on the current timecounter and if
416 	 * changing timecounters, a counter value from the new timecounter.
417 	 * Update the offset fields accordingly.
418 	 */
419 	delta = tc_delta(th);
420 	if (th->th_counter != timecounter)
421 		ncount = timecounter->tc_get_timecount(timecounter);
422 	else
423 		ncount = 0;
424 	th->th_offset_count += delta;
425 	th->th_offset_count &= th->th_counter->tc_counter_mask;
426 	bintime_addx(&th->th_offset, th->th_scale * delta);
427 
428 	/*
429 	 * Hardware latching timecounters may not generate interrupts on
430 	 * PPS events, so instead we poll them.  There is a finite risk that
431 	 * the hardware might capture a count which is later than the one we
432 	 * got above, and therefore possibly in the next NTP second which might
433 	 * have a different rate than the current NTP second.  It doesn't
434 	 * matter in practice.
435 	 */
436 	if (tho->th_counter->tc_poll_pps)
437 		tho->th_counter->tc_poll_pps(tho->th_counter);
438 
439 	/*
440 	 * Deal with NTP second processing.  The for loop normally
441 	 * iterates at most once, but in extreme situations it might
442 	 * keep NTP sane if timeouts are not run for several seconds.
443 	 * At boot, the time step can be large when the TOD hardware
444 	 * has been read, so on really large steps, we call
445 	 * ntp_update_second only twice.  We need to call it twice in
446 	 * case we missed a leap second.
447 	 */
448 	bt = th->th_offset;
449 	bintime_add(&bt, &boottimebin);
450 	i = bt.sec - tho->th_microtime.tv_sec;
451 	if (i > LARGE_STEP)
452 		i = 2;
453 	for (; i > 0; i--) {
454 		t = bt.sec;
455 		ntp_update_second(&th->th_adjustment, &bt.sec);
456 		if (bt.sec != t)
457 			boottimebin.sec += bt.sec - t;
458 	}
459 	/* Update the UTC timestamps used by the get*() functions. */
460 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
461 	bintime2timeval(&bt, &th->th_microtime);
462 	bintime2timespec(&bt, &th->th_nanotime);
463 
464 	/* Now is a good time to change timecounters. */
465 	if (th->th_counter != timecounter) {
466 		th->th_counter = timecounter;
467 		th->th_offset_count = ncount;
468 	}
469 
470 	/*-
471 	 * Recalculate the scaling factor.  We want the number of 1/2^64
472 	 * fractions of a second per period of the hardware counter, taking
473 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
474 	 * processing provides us with.
475 	 *
476 	 * The th_adjustment is nanoseconds per second with 32 bit binary
477 	 * fraction and we want 64 bit binary fraction of second:
478 	 *
479 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
480 	 *
481 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
482 	 * we can only multiply by about 850 without overflowing, that
483 	 * leaves no suitably precise fractions for multiply before divide.
484 	 *
485 	 * Divide before multiply with a fraction of 2199/512 results in a
486 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
487 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
488  	 *
489 	 * We happily sacrifice the lowest of the 64 bits of our result
490 	 * to the goddess of code clarity.
491 	 *
492 	 */
493 	scale = (u_int64_t)1 << 63;
494 	scale += (th->th_adjustment / 1024) * 2199;
495 	scale /= th->th_counter->tc_frequency;
496 	th->th_scale = scale * 2;
497 
498 	/*
499 	 * Now that the struct timehands is again consistent, set the new
500 	 * generation number, making sure to not make it zero.
501 	 */
502 	if (++ogen == 0)
503 		ogen = 1;
504 	th->th_generation = ogen;
505 
506 	/* Go live with the new struct timehands. */
507 	time_second = th->th_microtime.tv_sec;
508 	time_uptime = th->th_offset.sec;
509 	timehands = th;
510 }
511 
512 /* Report or change the active timecounter hardware. */
513 static int
514 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
515 {
516 	char newname[32];
517 	struct timecounter *newtc, *tc;
518 	int error;
519 
520 	tc = timecounter;
521 	strlcpy(newname, tc->tc_name, sizeof(newname));
522 
523 	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
524 	if (error != 0 || req->newptr == NULL ||
525 	    strcmp(newname, tc->tc_name) == 0)
526 		return (error);
527 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
528 		if (strcmp(newname, newtc->tc_name) != 0)
529 			continue;
530 
531 		/* Warm up new timecounter. */
532 		(void)newtc->tc_get_timecount(newtc);
533 		(void)newtc->tc_get_timecount(newtc);
534 
535 		timecounter = newtc;
536 		return (0);
537 	}
538 	return (EINVAL);
539 }
540 
541 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
542     0, 0, sysctl_kern_timecounter_hardware, "A", "");
543 
544 
545 /* Report or change the active timecounter hardware. */
546 static int
547 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
548 {
549 	char buf[32], *spc;
550 	struct timecounter *tc;
551 	int error;
552 
553 	spc = "";
554 	error = 0;
555 	for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
556 		sprintf(buf, "%s%s(%d)",
557 		    spc, tc->tc_name, tc->tc_quality);
558 		error = SYSCTL_OUT(req, buf, strlen(buf));
559 		spc = " ";
560 	}
561 	return (error);
562 }
563 
564 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
565     0, 0, sysctl_kern_timecounter_choice, "A", "");
566 
567 /*
568  * RFC 2783 PPS-API implementation.
569  */
570 
571 int
572 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
573 {
574 	pps_params_t *app;
575 	struct pps_fetch_args *fapi;
576 #ifdef PPS_SYNC
577 	struct pps_kcbind_args *kapi;
578 #endif
579 
580 	KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
581 	switch (cmd) {
582 	case PPS_IOC_CREATE:
583 		return (0);
584 	case PPS_IOC_DESTROY:
585 		return (0);
586 	case PPS_IOC_SETPARAMS:
587 		app = (pps_params_t *)data;
588 		if (app->mode & ~pps->ppscap)
589 			return (EINVAL);
590 		pps->ppsparam = *app;
591 		return (0);
592 	case PPS_IOC_GETPARAMS:
593 		app = (pps_params_t *)data;
594 		*app = pps->ppsparam;
595 		app->api_version = PPS_API_VERS_1;
596 		return (0);
597 	case PPS_IOC_GETCAP:
598 		*(int*)data = pps->ppscap;
599 		return (0);
600 	case PPS_IOC_FETCH:
601 		fapi = (struct pps_fetch_args *)data;
602 		if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
603 			return (EINVAL);
604 		if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
605 			return (EOPNOTSUPP);
606 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
607 		fapi->pps_info_buf = pps->ppsinfo;
608 		return (0);
609 	case PPS_IOC_KCBIND:
610 #ifdef PPS_SYNC
611 		kapi = (struct pps_kcbind_args *)data;
612 		/* XXX Only root should be able to do this */
613 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
614 			return (EINVAL);
615 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
616 			return (EINVAL);
617 		if (kapi->edge & ~pps->ppscap)
618 			return (EINVAL);
619 		pps->kcmode = kapi->edge;
620 		return (0);
621 #else
622 		return (EOPNOTSUPP);
623 #endif
624 	default:
625 		return (ENOIOCTL);
626 	}
627 }
628 
629 void
630 pps_init(struct pps_state *pps)
631 {
632 	pps->ppscap |= PPS_TSFMT_TSPEC;
633 	if (pps->ppscap & PPS_CAPTUREASSERT)
634 		pps->ppscap |= PPS_OFFSETASSERT;
635 	if (pps->ppscap & PPS_CAPTURECLEAR)
636 		pps->ppscap |= PPS_OFFSETCLEAR;
637 }
638 
639 void
640 pps_capture(struct pps_state *pps)
641 {
642 	struct timehands *th;
643 
644 	KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
645 	th = timehands;
646 	pps->capgen = th->th_generation;
647 	pps->capth = th;
648 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
649 	if (pps->capgen != th->th_generation)
650 		pps->capgen = 0;
651 }
652 
653 void
654 pps_event(struct pps_state *pps, int event)
655 {
656 	struct bintime bt;
657 	struct timespec ts, *tsp, *osp;
658 	u_int tcount, *pcount;
659 	int foff, fhard;
660 	pps_seq_t *pseq;
661 
662 	KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
663 	/* If the timecounter was wound up underneath us, bail out. */
664 	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
665 		return;
666 
667 	/* Things would be easier with arrays. */
668 	if (event == PPS_CAPTUREASSERT) {
669 		tsp = &pps->ppsinfo.assert_timestamp;
670 		osp = &pps->ppsparam.assert_offset;
671 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
672 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
673 		pcount = &pps->ppscount[0];
674 		pseq = &pps->ppsinfo.assert_sequence;
675 	} else {
676 		tsp = &pps->ppsinfo.clear_timestamp;
677 		osp = &pps->ppsparam.clear_offset;
678 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
679 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
680 		pcount = &pps->ppscount[1];
681 		pseq = &pps->ppsinfo.clear_sequence;
682 	}
683 
684 	/*
685 	 * If the timecounter changed, we cannot compare the count values, so
686 	 * we have to drop the rest of the PPS-stuff until the next event.
687 	 */
688 	if (pps->ppstc != pps->capth->th_counter) {
689 		pps->ppstc = pps->capth->th_counter;
690 		*pcount = pps->capcount;
691 		pps->ppscount[2] = pps->capcount;
692 		return;
693 	}
694 
695 	/* Convert the count to a timespec. */
696 	tcount = pps->capcount - pps->capth->th_offset_count;
697 	tcount &= pps->capth->th_counter->tc_counter_mask;
698 	bt = pps->capth->th_offset;
699 	bintime_addx(&bt, pps->capth->th_scale * tcount);
700 	bintime_add(&bt, &boottimebin);
701 	bintime2timespec(&bt, &ts);
702 
703 	/* If the timecounter was wound up underneath us, bail out. */
704 	if (pps->capgen != pps->capth->th_generation)
705 		return;
706 
707 	*pcount = pps->capcount;
708 	(*pseq)++;
709 	*tsp = ts;
710 
711 	if (foff) {
712 		timespecadd(tsp, osp);
713 		if (tsp->tv_nsec < 0) {
714 			tsp->tv_nsec += 1000000000;
715 			tsp->tv_sec -= 1;
716 		}
717 	}
718 #ifdef PPS_SYNC
719 	if (fhard) {
720 		u_int64_t scale;
721 
722 		/*
723 		 * Feed the NTP PLL/FLL.
724 		 * The FLL wants to know how many (hardware) nanoseconds
725 		 * elapsed since the previous event.
726 		 */
727 		tcount = pps->capcount - pps->ppscount[2];
728 		pps->ppscount[2] = pps->capcount;
729 		tcount &= pps->capth->th_counter->tc_counter_mask;
730 		scale = (u_int64_t)1 << 63;
731 		scale /= pps->capth->th_counter->tc_frequency;
732 		scale *= 2;
733 		bt.sec = 0;
734 		bt.frac = 0;
735 		bintime_addx(&bt, scale * tcount);
736 		bintime2timespec(&bt, &ts);
737 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
738 	}
739 #endif
740 }
741 
742 /*
743  * Timecounters need to be updated every so often to prevent the hardware
744  * counter from overflowing.  Updating also recalculates the cached values
745  * used by the get*() family of functions, so their precision depends on
746  * the update frequency.
747  */
748 
749 static int tc_tick;
750 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
751 
752 void
753 tc_ticktock(void)
754 {
755 	static int count;
756 	static time_t last_calib;
757 
758 	if (++count < tc_tick)
759 		return;
760 	count = 0;
761 	tc_windup();
762 	if (time_uptime != last_calib && !(time_uptime & 0xf)) {
763 		cpu_tick_calibrate(0);
764 		last_calib = time_uptime;
765 	}
766 }
767 
768 static void
769 inittimecounter(void *dummy)
770 {
771 	u_int p;
772 
773 	/*
774 	 * Set the initial timeout to
775 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
776 	 * People should probably not use the sysctl to set the timeout
777 	 * to smaller than its inital value, since that value is the
778 	 * smallest reasonable one.  If they want better timestamps they
779 	 * should use the non-"get"* functions.
780 	 */
781 	if (hz > 1000)
782 		tc_tick = (hz + 500) / 1000;
783 	else
784 		tc_tick = 1;
785 	p = (tc_tick * 1000000) / hz;
786 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
787 
788 	/* warm up new timecounter (again) and get rolling. */
789 	(void)timecounter->tc_get_timecount(timecounter);
790 	(void)timecounter->tc_get_timecount(timecounter);
791 }
792 
793 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL)
794 
795 /* Cpu tick handling -------------------------------------------------*/
796 
797 static int cpu_tick_variable;
798 static uint64_t	cpu_tick_frequency;
799 
800 static
801 uint64_t
802 tc_cpu_ticks(void)
803 {
804 	static uint64_t base;
805 	static unsigned last;
806 	unsigned u;
807 	struct timecounter *tc;
808 
809 	tc = timehands->th_counter;
810 	u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
811 	if (u < last)
812 		base += tc->tc_counter_mask + 1;
813 	last = u;
814 	return (u + base);
815 }
816 
817 /*
818  * This function gets called ever 16 seconds on only one designated
819  * CPU in the system from hardclock() via tc_ticktock().
820  *
821  * Whenever the real time clock is stepped we get called with reset=1
822  * to make sure we handle suspend/resume and similar events correctly.
823  */
824 
825 static void
826 cpu_tick_calibrate(int reset)
827 {
828 	static uint64_t c_last;
829 	uint64_t c_this, c_delta;
830 	static struct bintime  t_last;
831 	struct bintime t_this, t_delta;
832 
833 	if (reset) {
834 		/* The clock was stepped, abort & reset */
835 		t_last.sec = 0;
836 		return;
837 	}
838 
839 	/* we don't calibrate fixed rate cputicks */
840 	if (!cpu_tick_variable)
841 		return;
842 
843 	getbinuptime(&t_this);
844 	c_this = cpu_ticks();
845 	if (t_last.sec != 0) {
846 		c_delta = c_this - c_last;
847 		t_delta = t_this;
848 		bintime_sub(&t_delta, &t_last);
849 		if (0 && bootverbose) {
850 			struct timespec ts;
851 			bintime2timespec(&t_delta, &ts);
852 			printf("%ju  %ju.%016jx %ju.%09ju",
853 			    (uintmax_t)c_delta >> 4,
854 			    (uintmax_t)t_delta.sec, (uintmax_t)t_delta.frac,
855 			    (uintmax_t)ts.tv_sec, (uintmax_t)ts.tv_nsec);
856 		}
857 		/*
858 		 * Validate that 16 +/- 1/256 seconds passed.
859 		 * After division by 16 this gives us a precision of
860 		 * roughly 250PPM which is sufficient
861 		 */
862 		if (t_delta.sec > 16 || (
863 		    t_delta.sec == 16 && t_delta.frac >= (0x01LL << 56))) {
864 			/* too long */
865 			if (0 && bootverbose)
866 				printf("\ttoo long\n");
867 		} else if (t_delta.sec < 15 ||
868 		    (t_delta.sec == 15 && t_delta.frac <= (0xffLL << 56))) {
869 			/* too short */
870 			if (0 && bootverbose)
871 				printf("\ttoo short\n");
872 		} else {
873 			/* just right */
874 			c_delta >>= 4;
875 			if (c_delta  > cpu_tick_frequency) {
876 				if (0 && bootverbose)
877 					printf("\thigher\n");
878 				cpu_tick_frequency = c_delta;
879 			} else {
880 				if (0 && bootverbose)
881 					printf("\tlower\n");
882 			}
883 		}
884 	}
885 	c_last = c_this;
886 	t_last = t_this;
887 }
888 
889 void
890 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
891 {
892 
893 	if (func == NULL) {
894 		cpu_ticks = tc_cpu_ticks;
895 	} else {
896 		cpu_tick_frequency = freq;
897 		cpu_tick_variable = var;
898 		cpu_ticks = func;
899 	}
900 }
901 
902 uint64_t
903 cpu_tickrate(void)
904 {
905 
906 	if (cpu_ticks == tc_cpu_ticks)
907 		return (tc_getfrequency());
908 	return (cpu_tick_frequency);
909 }
910 
911 /*
912  * We need to be slightly careful converting cputicks to microseconds.
913  * There is plenty of margin in 64 bits of microseconds (half a million
914  * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
915  * before divide conversion (to retain precision) we find that the
916  * margin shrinks to 1.5 hours (one millionth of 146y).
917  * With a three prong approach we never loose significant bits, no
918  * matter what the cputick rate and length of timeinterval is.
919  */
920 
921 uint64_t
922 cputick2usec(uint64_t tick)
923 {
924 
925 	if (tick > 18446744073709551LL)		/* floor(2^64 / 1000) */
926 		return (tick / (cpu_tickrate() / 1000000LL));
927 	else if (tick > 18446744073709LL)	/* floor(2^64 / 1000000) */
928 		return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
929 	else
930 		return ((tick * 1000000LL) / cpu_tickrate());
931 }
932 
933 cpu_tick_f	*cpu_ticks = tc_cpu_ticks;
934