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