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