xref: /freebsd/sys/kern/kern_tc.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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/systm.h>
19 #include <sys/timepps.h>
20 #include <sys/timetc.h>
21 #include <sys/timex.h>
22 
23 /*
24  * A large step happens on boot.  This constant detects such steps.
25  * It is relatively small so that ntp_update_second gets called enough
26  * in the typical 'missed a couple of seconds' case, but doesn't loop
27  * forever when the time step is large.
28  */
29 #define LARGE_STEP	200
30 
31 /*
32  * Implement a dummy timecounter which we can use until we get a real one
33  * in the air.  This allows the console and other early stuff to use
34  * time services.
35  */
36 
37 static u_int
38 dummy_get_timecount(struct timecounter *tc)
39 {
40 	static u_int now;
41 
42 	return (++now);
43 }
44 
45 static struct timecounter dummy_timecounter = {
46 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
47 };
48 
49 struct timehands {
50 	/* These fields must be initialized by the driver. */
51 	struct timecounter	*th_counter;
52 	int64_t			th_adjustment;
53 	u_int64_t		th_scale;
54 	u_int	 		th_offset_count;
55 	struct bintime		th_offset;
56 	struct timeval		th_microtime;
57 	struct timespec		th_nanotime;
58 	/* Fields not to be copied in tc_windup start with th_generation. */
59 	volatile u_int		th_generation;
60 	struct timehands	*th_next;
61 };
62 
63 extern struct timehands th0;
64 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
65 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
66 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
67 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
68 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
69 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
70 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
71 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
72 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
73 static struct timehands th0 = {
74 	&dummy_timecounter,
75 	0,
76 	(uint64_t)-1 / 1000000,
77 	0,
78 	{1, 0},
79 	{0, 0},
80 	{0, 0},
81 	1,
82 	&th1
83 };
84 
85 static struct timehands *volatile timehands = &th0;
86 struct timecounter *timecounter = &dummy_timecounter;
87 static struct timecounter *timecounters = &dummy_timecounter;
88 
89 time_t time_second = 1;
90 time_t time_uptime = 0;
91 
92 static struct bintime boottimebin;
93 struct timeval boottime;
94 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
95     &boottime, timeval, "System boottime");
96 
97 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
98 
99 #define TC_STATS(foo) \
100 	static u_int foo; \
101 	SYSCTL_UINT(_kern_timecounter, OID_AUTO, foo, CTLFLAG_RD, &foo, 0, "");\
102 	struct __hack
103 
104 TC_STATS(nbinuptime);    TC_STATS(nnanouptime);    TC_STATS(nmicrouptime);
105 TC_STATS(nbintime);      TC_STATS(nnanotime);      TC_STATS(nmicrotime);
106 TC_STATS(ngetbinuptime); TC_STATS(ngetnanouptime); TC_STATS(ngetmicrouptime);
107 TC_STATS(ngetbintime);   TC_STATS(ngetnanotime);   TC_STATS(ngetmicrotime);
108 TC_STATS(nsetclock);
109 
110 #undef TC_STATS
111 
112 static void tc_windup(void);
113 
114 /*
115  * Return the difference between the timehands' counter value now and what
116  * was when we copied it to the timehands' offset_count.
117  */
118 static __inline u_int
119 tc_delta(struct timehands *th)
120 {
121 	struct timecounter *tc;
122 
123 	tc = th->th_counter;
124 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
125 	    tc->tc_counter_mask);
126 }
127 
128 /*
129  * Functions for reading the time.  We have to loop until we are sure that
130  * the timehands that we operated on was not updated under our feet.  See
131  * the comment in <sys/time.h> for a description of these 12 functions.
132  */
133 
134 void
135 binuptime(struct bintime *bt)
136 {
137 	struct timehands *th;
138 	u_int gen;
139 
140 	nbinuptime++;
141 	do {
142 		th = timehands;
143 		gen = th->th_generation;
144 		*bt = th->th_offset;
145 		bintime_addx(bt, th->th_scale * tc_delta(th));
146 	} while (gen == 0 || gen != th->th_generation);
147 }
148 
149 void
150 nanouptime(struct timespec *tsp)
151 {
152 	struct bintime bt;
153 
154 	nnanouptime++;
155 	binuptime(&bt);
156 	bintime2timespec(&bt, tsp);
157 }
158 
159 void
160 microuptime(struct timeval *tvp)
161 {
162 	struct bintime bt;
163 
164 	nmicrouptime++;
165 	binuptime(&bt);
166 	bintime2timeval(&bt, tvp);
167 }
168 
169 void
170 bintime(struct bintime *bt)
171 {
172 
173 	nbintime++;
174 	binuptime(bt);
175 	bintime_add(bt, &boottimebin);
176 }
177 
178 void
179 nanotime(struct timespec *tsp)
180 {
181 	struct bintime bt;
182 
183 	nnanotime++;
184 	bintime(&bt);
185 	bintime2timespec(&bt, tsp);
186 }
187 
188 void
189 microtime(struct timeval *tvp)
190 {
191 	struct bintime bt;
192 
193 	nmicrotime++;
194 	bintime(&bt);
195 	bintime2timeval(&bt, tvp);
196 }
197 
198 void
199 getbinuptime(struct bintime *bt)
200 {
201 	struct timehands *th;
202 	u_int gen;
203 
204 	ngetbinuptime++;
205 	do {
206 		th = timehands;
207 		gen = th->th_generation;
208 		*bt = th->th_offset;
209 	} while (gen == 0 || gen != th->th_generation);
210 }
211 
212 void
213 getnanouptime(struct timespec *tsp)
214 {
215 	struct timehands *th;
216 	u_int gen;
217 
218 	ngetnanouptime++;
219 	do {
220 		th = timehands;
221 		gen = th->th_generation;
222 		bintime2timespec(&th->th_offset, tsp);
223 	} while (gen == 0 || gen != th->th_generation);
224 }
225 
226 void
227 getmicrouptime(struct timeval *tvp)
228 {
229 	struct timehands *th;
230 	u_int gen;
231 
232 	ngetmicrouptime++;
233 	do {
234 		th = timehands;
235 		gen = th->th_generation;
236 		bintime2timeval(&th->th_offset, tvp);
237 	} while (gen == 0 || gen != th->th_generation);
238 }
239 
240 void
241 getbintime(struct bintime *bt)
242 {
243 	struct timehands *th;
244 	u_int gen;
245 
246 	ngetbintime++;
247 	do {
248 		th = timehands;
249 		gen = th->th_generation;
250 		*bt = th->th_offset;
251 	} while (gen == 0 || gen != th->th_generation);
252 	bintime_add(bt, &boottimebin);
253 }
254 
255 void
256 getnanotime(struct timespec *tsp)
257 {
258 	struct timehands *th;
259 	u_int gen;
260 
261 	ngetnanotime++;
262 	do {
263 		th = timehands;
264 		gen = th->th_generation;
265 		*tsp = th->th_nanotime;
266 	} while (gen == 0 || gen != th->th_generation);
267 }
268 
269 void
270 getmicrotime(struct timeval *tvp)
271 {
272 	struct timehands *th;
273 	u_int gen;
274 
275 	ngetmicrotime++;
276 	do {
277 		th = timehands;
278 		gen = th->th_generation;
279 		*tvp = th->th_microtime;
280 	} while (gen == 0 || gen != th->th_generation);
281 }
282 
283 /*
284  * Initialize a new timecounter and possibly use it.
285  */
286 void
287 tc_init(struct timecounter *tc)
288 {
289 	unsigned u;
290 
291 	u = tc->tc_frequency / tc->tc_counter_mask;
292 	if (u > hz && tc->tc_quality >= 0) {
293 		tc->tc_quality = -2000;
294 		if (bootverbose) {
295 			printf("Timecounter \"%s\" frequency %ju Hz",
296 			    tc->tc_name, (intmax_t)tc->tc_frequency);
297 			printf(" -- Insufficient hz, needs at least %u\n", u);
298 		}
299 	} else if (tc->tc_quality >= 0 || bootverbose) {
300 		printf("Timecounter \"%s\" frequency %ju Hz quality %d",
301 		    tc->tc_name, (intmax_t)tc->tc_frequency,
302 		    tc->tc_quality);
303 	}
304 
305 	printf("\n");
306 	tc->tc_next = timecounters;
307 	timecounters = tc;
308 	(void)tc->tc_get_timecount(tc);
309 	(void)tc->tc_get_timecount(tc);
310 	/* Never automatically use a timecounter with negative quality */
311 	if (tc->tc_quality < 0)
312 		return;
313 	if (tc->tc_quality < timecounter->tc_quality)
314 		return;
315 	timecounter = tc;
316 }
317 
318 /* Report the frequency of the current timecounter. */
319 u_int64_t
320 tc_getfrequency(void)
321 {
322 
323 	return (timehands->th_counter->tc_frequency);
324 }
325 
326 /*
327  * Step our concept of UTC.  This is done by modifying our estimate of
328  * when we booted.  XXX: needs further work.
329  */
330 void
331 tc_setclock(struct timespec *ts)
332 {
333 	struct timespec ts2;
334 
335 	nsetclock++;
336 	nanouptime(&ts2);
337 	boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
338 	/* XXX boottime should probably be a timespec. */
339 	boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
340 	if (boottime.tv_usec < 0) {
341 		boottime.tv_usec += 1000000;
342 		boottime.tv_sec--;
343 	}
344 	timeval2bintime(&boottime, &boottimebin);
345 
346 	/* XXX fiddle all the little crinkly bits around the fiords... */
347 	tc_windup();
348 }
349 
350 /*
351  * Initialize the next struct timehands in the ring and make
352  * it the active timehands.  Along the way we might switch to a different
353  * timecounter and/or do seconds processing in NTP.  Slightly magic.
354  */
355 static void
356 tc_windup(void)
357 {
358 	struct bintime bt;
359 	struct timehands *th, *tho;
360 	u_int64_t scale;
361 	u_int delta, ncount, ogen;
362 	int i;
363 	time_t t;
364 
365 	/*
366 	 * Make the next timehands a copy of the current one, but do not
367 	 * overwrite the generation or next pointer.  While we update
368 	 * the contents, the generation must be zero.
369 	 */
370 	tho = timehands;
371 	th = tho->th_next;
372 	ogen = th->th_generation;
373 	th->th_generation = 0;
374 	bcopy(tho, th, offsetof(struct timehands, th_generation));
375 
376 	/*
377 	 * Capture a timecounter delta on the current timecounter and if
378 	 * changing timecounters, a counter value from the new timecounter.
379 	 * Update the offset fields accordingly.
380 	 */
381 	delta = tc_delta(th);
382 	if (th->th_counter != timecounter)
383 		ncount = timecounter->tc_get_timecount(timecounter);
384 	else
385 		ncount = 0;
386 	th->th_offset_count += delta;
387 	th->th_offset_count &= th->th_counter->tc_counter_mask;
388 	bintime_addx(&th->th_offset, th->th_scale * delta);
389 
390 	/*
391 	 * Hardware latching timecounters may not generate interrupts on
392 	 * PPS events, so instead we poll them.  There is a finite risk that
393 	 * the hardware might capture a count which is later than the one we
394 	 * got above, and therefore possibly in the next NTP second which might
395 	 * have a different rate than the current NTP second.  It doesn't
396 	 * matter in practice.
397 	 */
398 	if (tho->th_counter->tc_poll_pps)
399 		tho->th_counter->tc_poll_pps(tho->th_counter);
400 
401 	/*
402 	 * Deal with NTP second processing.  The for loop normally
403 	 * iterates at most once, but in extreme situations it might
404 	 * keep NTP sane if timeouts are not run for several seconds.
405 	 * At boot, the time step can be large when the TOD hardware
406 	 * has been read, so on really large steps, we call
407 	 * ntp_update_second only twice.  We need to call it twice in
408 	 * case we missed a leap second.
409 	 */
410 	bt = th->th_offset;
411 	bintime_add(&bt, &boottimebin);
412 	i = bt.sec - tho->th_microtime.tv_sec;
413 	if (i > LARGE_STEP)
414 		i = 2;
415 	for (; i > 0; i--) {
416 		t = bt.sec;
417 		ntp_update_second(&th->th_adjustment, &bt.sec);
418 		if (bt.sec != t)
419 			boottimebin.sec += bt.sec - t;
420 	}
421 	/* Update the UTC timestamps used by the get*() functions. */
422 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
423 	bintime2timeval(&bt, &th->th_microtime);
424 	bintime2timespec(&bt, &th->th_nanotime);
425 
426 	/* Now is a good time to change timecounters. */
427 	if (th->th_counter != timecounter) {
428 		th->th_counter = timecounter;
429 		th->th_offset_count = ncount;
430 	}
431 
432 	/*-
433 	 * Recalculate the scaling factor.  We want the number of 1/2^64
434 	 * fractions of a second per period of the hardware counter, taking
435 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
436 	 * processing provides us with.
437 	 *
438 	 * The th_adjustment is nanoseconds per second with 32 bit binary
439 	 * fraction and we want 64 bit binary fraction of second:
440 	 *
441 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
442 	 *
443 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
444 	 * we can only multiply by about 850 without overflowing, but that
445 	 * leaves suitably precise fractions for multiply before divide.
446 	 *
447 	 * Divide before multiply with a fraction of 2199/512 results in a
448 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
449 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
450  	 *
451 	 * We happily sacrifice the lowest of the 64 bits of our result
452 	 * to the goddess of code clarity.
453 	 *
454 	 */
455 	scale = (u_int64_t)1 << 63;
456 	scale += (th->th_adjustment / 1024) * 2199;
457 	scale /= th->th_counter->tc_frequency;
458 	th->th_scale = scale * 2;
459 
460 	/*
461 	 * Now that the struct timehands is again consistent, set the new
462 	 * generation number, making sure to not make it zero.
463 	 */
464 	if (++ogen == 0)
465 		ogen = 1;
466 	th->th_generation = ogen;
467 
468 	/* Go live with the new struct timehands. */
469 	time_second = th->th_microtime.tv_sec;
470 	time_uptime = th->th_offset.sec;
471 	timehands = th;
472 }
473 
474 /* Report or change the active timecounter hardware. */
475 static int
476 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
477 {
478 	char newname[32];
479 	struct timecounter *newtc, *tc;
480 	int error;
481 
482 	tc = timecounter;
483 	strlcpy(newname, tc->tc_name, sizeof(newname));
484 
485 	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
486 	if (error != 0 || req->newptr == NULL ||
487 	    strcmp(newname, tc->tc_name) == 0)
488 		return (error);
489 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
490 		if (strcmp(newname, newtc->tc_name) != 0)
491 			continue;
492 
493 		/* Warm up new timecounter. */
494 		(void)newtc->tc_get_timecount(newtc);
495 		(void)newtc->tc_get_timecount(newtc);
496 
497 		timecounter = newtc;
498 		return (0);
499 	}
500 	return (EINVAL);
501 }
502 
503 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
504     0, 0, sysctl_kern_timecounter_hardware, "A", "");
505 
506 
507 /* Report or change the active timecounter hardware. */
508 static int
509 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
510 {
511 	char buf[32], *spc;
512 	struct timecounter *tc;
513 	int error;
514 
515 	spc = "";
516 	error = 0;
517 	for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
518 		sprintf(buf, "%s%s(%d)",
519 		    spc, tc->tc_name, tc->tc_quality);
520 		error = SYSCTL_OUT(req, buf, strlen(buf));
521 		spc = " ";
522 	}
523 	return (error);
524 }
525 
526 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
527     0, 0, sysctl_kern_timecounter_choice, "A", "");
528 
529 /*
530  * RFC 2783 PPS-API implementation.
531  */
532 
533 int
534 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
535 {
536 	pps_params_t *app;
537 	struct pps_fetch_args *fapi;
538 #ifdef PPS_SYNC
539 	struct pps_kcbind_args *kapi;
540 #endif
541 
542 	switch (cmd) {
543 	case PPS_IOC_CREATE:
544 		return (0);
545 	case PPS_IOC_DESTROY:
546 		return (0);
547 	case PPS_IOC_SETPARAMS:
548 		app = (pps_params_t *)data;
549 		if (app->mode & ~pps->ppscap)
550 			return (EINVAL);
551 		pps->ppsparam = *app;
552 		return (0);
553 	case PPS_IOC_GETPARAMS:
554 		app = (pps_params_t *)data;
555 		*app = pps->ppsparam;
556 		app->api_version = PPS_API_VERS_1;
557 		return (0);
558 	case PPS_IOC_GETCAP:
559 		*(int*)data = pps->ppscap;
560 		return (0);
561 	case PPS_IOC_FETCH:
562 		fapi = (struct pps_fetch_args *)data;
563 		if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
564 			return (EINVAL);
565 		if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
566 			return (EOPNOTSUPP);
567 		pps->ppsinfo.current_mode = pps->ppsparam.mode;
568 		fapi->pps_info_buf = pps->ppsinfo;
569 		return (0);
570 	case PPS_IOC_KCBIND:
571 #ifdef PPS_SYNC
572 		kapi = (struct pps_kcbind_args *)data;
573 		/* XXX Only root should be able to do this */
574 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
575 			return (EINVAL);
576 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
577 			return (EINVAL);
578 		if (kapi->edge & ~pps->ppscap)
579 			return (EINVAL);
580 		pps->kcmode = kapi->edge;
581 		return (0);
582 #else
583 		return (EOPNOTSUPP);
584 #endif
585 	default:
586 		return (ENOTTY);
587 	}
588 }
589 
590 void
591 pps_init(struct pps_state *pps)
592 {
593 	pps->ppscap |= PPS_TSFMT_TSPEC;
594 	if (pps->ppscap & PPS_CAPTUREASSERT)
595 		pps->ppscap |= PPS_OFFSETASSERT;
596 	if (pps->ppscap & PPS_CAPTURECLEAR)
597 		pps->ppscap |= PPS_OFFSETCLEAR;
598 }
599 
600 void
601 pps_capture(struct pps_state *pps)
602 {
603 	struct timehands *th;
604 
605 	th = timehands;
606 	pps->capgen = th->th_generation;
607 	pps->capth = th;
608 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
609 	if (pps->capgen != th->th_generation)
610 		pps->capgen = 0;
611 }
612 
613 void
614 pps_event(struct pps_state *pps, int event)
615 {
616 	struct bintime bt;
617 	struct timespec ts, *tsp, *osp;
618 	u_int tcount, *pcount;
619 	int foff, fhard;
620 	pps_seq_t *pseq;
621 
622 	/* If the timecounter was wound up underneath us, bail out. */
623 	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
624 		return;
625 
626 	/* Things would be easier with arrays. */
627 	if (event == PPS_CAPTUREASSERT) {
628 		tsp = &pps->ppsinfo.assert_timestamp;
629 		osp = &pps->ppsparam.assert_offset;
630 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
631 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
632 		pcount = &pps->ppscount[0];
633 		pseq = &pps->ppsinfo.assert_sequence;
634 	} else {
635 		tsp = &pps->ppsinfo.clear_timestamp;
636 		osp = &pps->ppsparam.clear_offset;
637 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
638 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
639 		pcount = &pps->ppscount[1];
640 		pseq = &pps->ppsinfo.clear_sequence;
641 	}
642 
643 	/*
644 	 * If the timecounter changed, we cannot compare the count values, so
645 	 * we have to drop the rest of the PPS-stuff until the next event.
646 	 */
647 	if (pps->ppstc != pps->capth->th_counter) {
648 		pps->ppstc = pps->capth->th_counter;
649 		*pcount = pps->capcount;
650 		pps->ppscount[2] = pps->capcount;
651 		return;
652 	}
653 
654 	/* Return if nothing really happened. */
655 	if (*pcount == pps->capcount)
656 		return;
657 
658 	/* Convert the count to a timespec. */
659 	tcount = pps->capcount - pps->capth->th_offset_count;
660 	tcount &= pps->capth->th_counter->tc_counter_mask;
661 	bt = pps->capth->th_offset;
662 	bintime_addx(&bt, pps->capth->th_scale * tcount);
663 	bintime_add(&bt, &boottimebin);
664 	bintime2timespec(&bt, &ts);
665 
666 	/* If the timecounter was wound up underneath us, bail out. */
667 	if (pps->capgen != pps->capth->th_generation)
668 		return;
669 
670 	*pcount = pps->capcount;
671 	(*pseq)++;
672 	*tsp = ts;
673 
674 	if (foff) {
675 		timespecadd(tsp, osp);
676 		if (tsp->tv_nsec < 0) {
677 			tsp->tv_nsec += 1000000000;
678 			tsp->tv_sec -= 1;
679 		}
680 	}
681 #ifdef PPS_SYNC
682 	if (fhard) {
683 		u_int64_t scale;
684 
685 		/*
686 		 * Feed the NTP PLL/FLL.
687 		 * The FLL wants to know how many (hardware) nanoseconds
688 		 * elapsed since the previous event.
689 		 */
690 		tcount = pps->capcount - pps->ppscount[2];
691 		pps->ppscount[2] = pps->capcount;
692 		tcount &= pps->capth->th_counter->tc_counter_mask;
693 		scale = (u_int64_t)1 << 63;
694 		scale /= pps->capth->th_counter->tc_frequency;
695 		scale *= 2;
696 		bt.sec = 0;
697 		bt.frac = 0;
698 		bintime_addx(&bt, scale * tcount);
699 		bintime2timespec(&bt, &ts);
700 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
701 	}
702 #endif
703 }
704 
705 /*
706  * Timecounters need to be updated every so often to prevent the hardware
707  * counter from overflowing.  Updating also recalculates the cached values
708  * used by the get*() family of functions, so their precision depends on
709  * the update frequency.
710  */
711 
712 static int tc_tick;
713 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
714 
715 void
716 tc_ticktock(void)
717 {
718 	static int count;
719 
720 	if (++count < tc_tick)
721 		return;
722 	count = 0;
723 	tc_windup();
724 }
725 
726 static void
727 inittimecounter(void *dummy)
728 {
729 	u_int p;
730 
731 	/*
732 	 * Set the initial timeout to
733 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
734 	 * People should probably not use the sysctl to set the timeout
735 	 * to smaller than its inital value, since that value is the
736 	 * smallest reasonable one.  If they want better timestamps they
737 	 * should use the non-"get"* functions.
738 	 */
739 	if (hz > 1000)
740 		tc_tick = (hz + 500) / 1000;
741 	else
742 		tc_tick = 1;
743 	p = (tc_tick * 1000000) / hz;
744 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
745 
746 	/* warm up new timecounter (again) and get rolling. */
747 	(void)timecounter->tc_get_timecount(timecounter);
748 	(void)timecounter->tc_get_timecount(timecounter);
749 }
750 
751 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL)
752