xref: /freebsd/sys/kern/kern_tc.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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  * Copyright (c) 2011 The FreeBSD Foundation
10  * All rights reserved.
11  *
12  * Portions of this software were developed by Julien Ridoux at the University
13  * of Melbourne under sponsorship from the FreeBSD Foundation.
14  */
15 
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
18 
19 #include "opt_compat.h"
20 #include "opt_ntp.h"
21 #include "opt_ffclock.h"
22 
23 #include <sys/param.h>
24 #include <sys/kernel.h>
25 #include <sys/limits.h>
26 #include <sys/lock.h>
27 #include <sys/mutex.h>
28 #include <sys/sbuf.h>
29 #include <sys/sysctl.h>
30 #include <sys/syslog.h>
31 #include <sys/systm.h>
32 #include <sys/timeffc.h>
33 #include <sys/timepps.h>
34 #include <sys/timetc.h>
35 #include <sys/timex.h>
36 #include <sys/vdso.h>
37 
38 /*
39  * A large step happens on boot.  This constant detects such steps.
40  * It is relatively small so that ntp_update_second gets called enough
41  * in the typical 'missed a couple of seconds' case, but doesn't loop
42  * forever when the time step is large.
43  */
44 #define LARGE_STEP	200
45 
46 /*
47  * Implement a dummy timecounter which we can use until we get a real one
48  * in the air.  This allows the console and other early stuff to use
49  * time services.
50  */
51 
52 static u_int
53 dummy_get_timecount(struct timecounter *tc)
54 {
55 	static u_int now;
56 
57 	return (++now);
58 }
59 
60 static struct timecounter dummy_timecounter = {
61 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
62 };
63 
64 struct timehands {
65 	/* These fields must be initialized by the driver. */
66 	struct timecounter	*th_counter;
67 	int64_t			th_adjustment;
68 	uint64_t		th_scale;
69 	u_int	 		th_offset_count;
70 	struct bintime		th_offset;
71 	struct timeval		th_microtime;
72 	struct timespec		th_nanotime;
73 	/* Fields not to be copied in tc_windup start with th_generation. */
74 	u_int			th_generation;
75 	struct timehands	*th_next;
76 };
77 
78 static struct timehands th0;
79 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
80 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
81 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
82 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
83 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
84 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
85 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
86 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
87 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
88 static struct timehands th0 = {
89 	&dummy_timecounter,
90 	0,
91 	(uint64_t)-1 / 1000000,
92 	0,
93 	{1, 0},
94 	{0, 0},
95 	{0, 0},
96 	1,
97 	&th1
98 };
99 
100 static struct timehands *volatile timehands = &th0;
101 struct timecounter *timecounter = &dummy_timecounter;
102 static struct timecounter *timecounters = &dummy_timecounter;
103 
104 int tc_min_ticktock_freq = 1;
105 
106 volatile time_t time_second = 1;
107 volatile time_t time_uptime = 1;
108 
109 struct bintime boottimebin;
110 struct timeval boottime;
111 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
112 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
113     NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
114 
115 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
116 static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
117 
118 static int timestepwarnings;
119 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
120     &timestepwarnings, 0, "Log time steps");
121 
122 struct bintime bt_timethreshold;
123 struct bintime bt_tickthreshold;
124 sbintime_t sbt_timethreshold;
125 sbintime_t sbt_tickthreshold;
126 struct bintime tc_tick_bt;
127 sbintime_t tc_tick_sbt;
128 int tc_precexp;
129 int tc_timepercentage = TC_DEFAULTPERC;
130 static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS);
131 SYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation,
132     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
133     sysctl_kern_timecounter_adjprecision, "I",
134     "Allowed time interval deviation in percents");
135 
136 static int tc_chosen;	/* Non-zero if a specific tc was chosen via sysctl. */
137 
138 static void tc_windup(void);
139 static void cpu_tick_calibrate(int);
140 
141 void dtrace_getnanotime(struct timespec *tsp);
142 
143 static int
144 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
145 {
146 #ifndef __mips__
147 #ifdef SCTL_MASK32
148 	int tv[2];
149 
150 	if (req->flags & SCTL_MASK32) {
151 		tv[0] = boottime.tv_sec;
152 		tv[1] = boottime.tv_usec;
153 		return SYSCTL_OUT(req, tv, sizeof(tv));
154 	} else
155 #endif
156 #endif
157 		return SYSCTL_OUT(req, &boottime, sizeof(boottime));
158 }
159 
160 static int
161 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
162 {
163 	u_int ncount;
164 	struct timecounter *tc = arg1;
165 
166 	ncount = tc->tc_get_timecount(tc);
167 	return sysctl_handle_int(oidp, &ncount, 0, req);
168 }
169 
170 static int
171 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
172 {
173 	uint64_t freq;
174 	struct timecounter *tc = arg1;
175 
176 	freq = tc->tc_frequency;
177 	return sysctl_handle_64(oidp, &freq, 0, req);
178 }
179 
180 /*
181  * Return the difference between the timehands' counter value now and what
182  * was when we copied it to the timehands' offset_count.
183  */
184 static __inline u_int
185 tc_delta(struct timehands *th)
186 {
187 	struct timecounter *tc;
188 
189 	tc = th->th_counter;
190 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
191 	    tc->tc_counter_mask);
192 }
193 
194 /*
195  * Functions for reading the time.  We have to loop until we are sure that
196  * the timehands that we operated on was not updated under our feet.  See
197  * the comment in <sys/time.h> for a description of these 12 functions.
198  */
199 
200 #ifdef FFCLOCK
201 void
202 fbclock_binuptime(struct bintime *bt)
203 {
204 	struct timehands *th;
205 	unsigned int gen;
206 
207 	do {
208 		th = timehands;
209 		gen = atomic_load_acq_int(&th->th_generation);
210 		*bt = th->th_offset;
211 		bintime_addx(bt, th->th_scale * tc_delta(th));
212 		atomic_thread_fence_acq();
213 	} while (gen == 0 || gen != th->th_generation);
214 }
215 
216 void
217 fbclock_nanouptime(struct timespec *tsp)
218 {
219 	struct bintime bt;
220 
221 	fbclock_binuptime(&bt);
222 	bintime2timespec(&bt, tsp);
223 }
224 
225 void
226 fbclock_microuptime(struct timeval *tvp)
227 {
228 	struct bintime bt;
229 
230 	fbclock_binuptime(&bt);
231 	bintime2timeval(&bt, tvp);
232 }
233 
234 void
235 fbclock_bintime(struct bintime *bt)
236 {
237 
238 	fbclock_binuptime(bt);
239 	bintime_add(bt, &boottimebin);
240 }
241 
242 void
243 fbclock_nanotime(struct timespec *tsp)
244 {
245 	struct bintime bt;
246 
247 	fbclock_bintime(&bt);
248 	bintime2timespec(&bt, tsp);
249 }
250 
251 void
252 fbclock_microtime(struct timeval *tvp)
253 {
254 	struct bintime bt;
255 
256 	fbclock_bintime(&bt);
257 	bintime2timeval(&bt, tvp);
258 }
259 
260 void
261 fbclock_getbinuptime(struct bintime *bt)
262 {
263 	struct timehands *th;
264 	unsigned int gen;
265 
266 	do {
267 		th = timehands;
268 		gen = atomic_load_acq_int(&th->th_generation);
269 		*bt = th->th_offset;
270 		atomic_thread_fence_acq();
271 	} while (gen == 0 || gen != th->th_generation);
272 }
273 
274 void
275 fbclock_getnanouptime(struct timespec *tsp)
276 {
277 	struct timehands *th;
278 	unsigned int gen;
279 
280 	do {
281 		th = timehands;
282 		gen = atomic_load_acq_int(&th->th_generation);
283 		bintime2timespec(&th->th_offset, tsp);
284 		atomic_thread_fence_acq();
285 	} while (gen == 0 || gen != th->th_generation);
286 }
287 
288 void
289 fbclock_getmicrouptime(struct timeval *tvp)
290 {
291 	struct timehands *th;
292 	unsigned int gen;
293 
294 	do {
295 		th = timehands;
296 		gen = atomic_load_acq_int(&th->th_generation);
297 		bintime2timeval(&th->th_offset, tvp);
298 		atomic_thread_fence_acq();
299 	} while (gen == 0 || gen != th->th_generation);
300 }
301 
302 void
303 fbclock_getbintime(struct bintime *bt)
304 {
305 	struct timehands *th;
306 	unsigned int gen;
307 
308 	do {
309 		th = timehands;
310 		gen = atomic_load_acq_int(&th->th_generation);
311 		*bt = th->th_offset;
312 		atomic_thread_fence_acq();
313 	} while (gen == 0 || gen != th->th_generation);
314 	bintime_add(bt, &boottimebin);
315 }
316 
317 void
318 fbclock_getnanotime(struct timespec *tsp)
319 {
320 	struct timehands *th;
321 	unsigned int gen;
322 
323 	do {
324 		th = timehands;
325 		gen = atomic_load_acq_int(&th->th_generation);
326 		*tsp = th->th_nanotime;
327 		atomic_thread_fence_acq();
328 	} while (gen == 0 || gen != th->th_generation);
329 }
330 
331 void
332 fbclock_getmicrotime(struct timeval *tvp)
333 {
334 	struct timehands *th;
335 	unsigned int gen;
336 
337 	do {
338 		th = timehands;
339 		gen = atomic_load_acq_int(&th->th_generation);
340 		*tvp = th->th_microtime;
341 		atomic_thread_fence_acq();
342 	} while (gen == 0 || gen != th->th_generation);
343 }
344 #else /* !FFCLOCK */
345 void
346 binuptime(struct bintime *bt)
347 {
348 	struct timehands *th;
349 	u_int gen;
350 
351 	do {
352 		th = timehands;
353 		gen = atomic_load_acq_int(&th->th_generation);
354 		*bt = th->th_offset;
355 		bintime_addx(bt, th->th_scale * tc_delta(th));
356 		atomic_thread_fence_acq();
357 	} while (gen == 0 || gen != th->th_generation);
358 }
359 
360 void
361 nanouptime(struct timespec *tsp)
362 {
363 	struct bintime bt;
364 
365 	binuptime(&bt);
366 	bintime2timespec(&bt, tsp);
367 }
368 
369 void
370 microuptime(struct timeval *tvp)
371 {
372 	struct bintime bt;
373 
374 	binuptime(&bt);
375 	bintime2timeval(&bt, tvp);
376 }
377 
378 void
379 bintime(struct bintime *bt)
380 {
381 
382 	binuptime(bt);
383 	bintime_add(bt, &boottimebin);
384 }
385 
386 void
387 nanotime(struct timespec *tsp)
388 {
389 	struct bintime bt;
390 
391 	bintime(&bt);
392 	bintime2timespec(&bt, tsp);
393 }
394 
395 void
396 microtime(struct timeval *tvp)
397 {
398 	struct bintime bt;
399 
400 	bintime(&bt);
401 	bintime2timeval(&bt, tvp);
402 }
403 
404 void
405 getbinuptime(struct bintime *bt)
406 {
407 	struct timehands *th;
408 	u_int gen;
409 
410 	do {
411 		th = timehands;
412 		gen = atomic_load_acq_int(&th->th_generation);
413 		*bt = th->th_offset;
414 		atomic_thread_fence_acq();
415 	} while (gen == 0 || gen != th->th_generation);
416 }
417 
418 void
419 getnanouptime(struct timespec *tsp)
420 {
421 	struct timehands *th;
422 	u_int gen;
423 
424 	do {
425 		th = timehands;
426 		gen = atomic_load_acq_int(&th->th_generation);
427 		bintime2timespec(&th->th_offset, tsp);
428 		atomic_thread_fence_acq();
429 	} while (gen == 0 || gen != th->th_generation);
430 }
431 
432 void
433 getmicrouptime(struct timeval *tvp)
434 {
435 	struct timehands *th;
436 	u_int gen;
437 
438 	do {
439 		th = timehands;
440 		gen = atomic_load_acq_int(&th->th_generation);
441 		bintime2timeval(&th->th_offset, tvp);
442 		atomic_thread_fence_acq();
443 	} while (gen == 0 || gen != th->th_generation);
444 }
445 
446 void
447 getbintime(struct bintime *bt)
448 {
449 	struct timehands *th;
450 	u_int gen;
451 
452 	do {
453 		th = timehands;
454 		gen = atomic_load_acq_int(&th->th_generation);
455 		*bt = th->th_offset;
456 		atomic_thread_fence_acq();
457 	} while (gen == 0 || gen != th->th_generation);
458 	bintime_add(bt, &boottimebin);
459 }
460 
461 void
462 getnanotime(struct timespec *tsp)
463 {
464 	struct timehands *th;
465 	u_int gen;
466 
467 	do {
468 		th = timehands;
469 		gen = atomic_load_acq_int(&th->th_generation);
470 		*tsp = th->th_nanotime;
471 		atomic_thread_fence_acq();
472 	} while (gen == 0 || gen != th->th_generation);
473 }
474 
475 void
476 getmicrotime(struct timeval *tvp)
477 {
478 	struct timehands *th;
479 	u_int gen;
480 
481 	do {
482 		th = timehands;
483 		gen = atomic_load_acq_int(&th->th_generation);
484 		*tvp = th->th_microtime;
485 		atomic_thread_fence_acq();
486 	} while (gen == 0 || gen != th->th_generation);
487 }
488 #endif /* FFCLOCK */
489 
490 #ifdef FFCLOCK
491 /*
492  * Support for feed-forward synchronization algorithms. This is heavily inspired
493  * by the timehands mechanism but kept independent from it. *_windup() functions
494  * have some connection to avoid accessing the timecounter hardware more than
495  * necessary.
496  */
497 
498 /* Feed-forward clock estimates kept updated by the synchronization daemon. */
499 struct ffclock_estimate ffclock_estimate;
500 struct bintime ffclock_boottime;	/* Feed-forward boot time estimate. */
501 uint32_t ffclock_status;		/* Feed-forward clock status. */
502 int8_t ffclock_updated;			/* New estimates are available. */
503 struct mtx ffclock_mtx;			/* Mutex on ffclock_estimate. */
504 
505 struct fftimehands {
506 	struct ffclock_estimate	cest;
507 	struct bintime		tick_time;
508 	struct bintime		tick_time_lerp;
509 	ffcounter		tick_ffcount;
510 	uint64_t		period_lerp;
511 	volatile uint8_t	gen;
512 	struct fftimehands	*next;
513 };
514 
515 #define	NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
516 
517 static struct fftimehands ffth[10];
518 static struct fftimehands *volatile fftimehands = ffth;
519 
520 static void
521 ffclock_init(void)
522 {
523 	struct fftimehands *cur;
524 	struct fftimehands *last;
525 
526 	memset(ffth, 0, sizeof(ffth));
527 
528 	last = ffth + NUM_ELEMENTS(ffth) - 1;
529 	for (cur = ffth; cur < last; cur++)
530 		cur->next = cur + 1;
531 	last->next = ffth;
532 
533 	ffclock_updated = 0;
534 	ffclock_status = FFCLOCK_STA_UNSYNC;
535 	mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
536 }
537 
538 /*
539  * Reset the feed-forward clock estimates. Called from inittodr() to get things
540  * kick started and uses the timecounter nominal frequency as a first period
541  * estimate. Note: this function may be called several time just after boot.
542  * Note: this is the only function that sets the value of boot time for the
543  * monotonic (i.e. uptime) version of the feed-forward clock.
544  */
545 void
546 ffclock_reset_clock(struct timespec *ts)
547 {
548 	struct timecounter *tc;
549 	struct ffclock_estimate cest;
550 
551 	tc = timehands->th_counter;
552 	memset(&cest, 0, sizeof(struct ffclock_estimate));
553 
554 	timespec2bintime(ts, &ffclock_boottime);
555 	timespec2bintime(ts, &(cest.update_time));
556 	ffclock_read_counter(&cest.update_ffcount);
557 	cest.leapsec_next = 0;
558 	cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
559 	cest.errb_abs = 0;
560 	cest.errb_rate = 0;
561 	cest.status = FFCLOCK_STA_UNSYNC;
562 	cest.leapsec_total = 0;
563 	cest.leapsec = 0;
564 
565 	mtx_lock(&ffclock_mtx);
566 	bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
567 	ffclock_updated = INT8_MAX;
568 	mtx_unlock(&ffclock_mtx);
569 
570 	printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
571 	    (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
572 	    (unsigned long)ts->tv_nsec);
573 }
574 
575 /*
576  * Sub-routine to convert a time interval measured in RAW counter units to time
577  * in seconds stored in bintime format.
578  * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
579  * larger than the max value of u_int (on 32 bit architecture). Loop to consume
580  * extra cycles.
581  */
582 static void
583 ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
584 {
585 	struct bintime bt2;
586 	ffcounter delta, delta_max;
587 
588 	delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
589 	bintime_clear(bt);
590 	do {
591 		if (ffdelta > delta_max)
592 			delta = delta_max;
593 		else
594 			delta = ffdelta;
595 		bt2.sec = 0;
596 		bt2.frac = period;
597 		bintime_mul(&bt2, (unsigned int)delta);
598 		bintime_add(bt, &bt2);
599 		ffdelta -= delta;
600 	} while (ffdelta > 0);
601 }
602 
603 /*
604  * Update the fftimehands.
605  * Push the tick ffcount and time(s) forward based on current clock estimate.
606  * The conversion from ffcounter to bintime relies on the difference clock
607  * principle, whose accuracy relies on computing small time intervals. If a new
608  * clock estimate has been passed by the synchronisation daemon, make it
609  * current, and compute the linear interpolation for monotonic time if needed.
610  */
611 static void
612 ffclock_windup(unsigned int delta)
613 {
614 	struct ffclock_estimate *cest;
615 	struct fftimehands *ffth;
616 	struct bintime bt, gap_lerp;
617 	ffcounter ffdelta;
618 	uint64_t frac;
619 	unsigned int polling;
620 	uint8_t forward_jump, ogen;
621 
622 	/*
623 	 * Pick the next timehand, copy current ffclock estimates and move tick
624 	 * times and counter forward.
625 	 */
626 	forward_jump = 0;
627 	ffth = fftimehands->next;
628 	ogen = ffth->gen;
629 	ffth->gen = 0;
630 	cest = &ffth->cest;
631 	bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
632 	ffdelta = (ffcounter)delta;
633 	ffth->period_lerp = fftimehands->period_lerp;
634 
635 	ffth->tick_time = fftimehands->tick_time;
636 	ffclock_convert_delta(ffdelta, cest->period, &bt);
637 	bintime_add(&ffth->tick_time, &bt);
638 
639 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
640 	ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
641 	bintime_add(&ffth->tick_time_lerp, &bt);
642 
643 	ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
644 
645 	/*
646 	 * Assess the status of the clock, if the last update is too old, it is
647 	 * likely the synchronisation daemon is dead and the clock is free
648 	 * running.
649 	 */
650 	if (ffclock_updated == 0) {
651 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
652 		ffclock_convert_delta(ffdelta, cest->period, &bt);
653 		if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
654 			ffclock_status |= FFCLOCK_STA_UNSYNC;
655 	}
656 
657 	/*
658 	 * If available, grab updated clock estimates and make them current.
659 	 * Recompute time at this tick using the updated estimates. The clock
660 	 * estimates passed the feed-forward synchronisation daemon may result
661 	 * in time conversion that is not monotonically increasing (just after
662 	 * the update). time_lerp is a particular linear interpolation over the
663 	 * synchronisation algo polling period that ensures monotonicity for the
664 	 * clock ids requesting it.
665 	 */
666 	if (ffclock_updated > 0) {
667 		bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
668 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
669 		ffth->tick_time = cest->update_time;
670 		ffclock_convert_delta(ffdelta, cest->period, &bt);
671 		bintime_add(&ffth->tick_time, &bt);
672 
673 		/* ffclock_reset sets ffclock_updated to INT8_MAX */
674 		if (ffclock_updated == INT8_MAX)
675 			ffth->tick_time_lerp = ffth->tick_time;
676 
677 		if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
678 			forward_jump = 1;
679 		else
680 			forward_jump = 0;
681 
682 		bintime_clear(&gap_lerp);
683 		if (forward_jump) {
684 			gap_lerp = ffth->tick_time;
685 			bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
686 		} else {
687 			gap_lerp = ffth->tick_time_lerp;
688 			bintime_sub(&gap_lerp, &ffth->tick_time);
689 		}
690 
691 		/*
692 		 * The reset from the RTC clock may be far from accurate, and
693 		 * reducing the gap between real time and interpolated time
694 		 * could take a very long time if the interpolated clock insists
695 		 * on strict monotonicity. The clock is reset under very strict
696 		 * conditions (kernel time is known to be wrong and
697 		 * synchronization daemon has been restarted recently.
698 		 * ffclock_boottime absorbs the jump to ensure boot time is
699 		 * correct and uptime functions stay consistent.
700 		 */
701 		if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
702 		    ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
703 		    ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
704 			if (forward_jump)
705 				bintime_add(&ffclock_boottime, &gap_lerp);
706 			else
707 				bintime_sub(&ffclock_boottime, &gap_lerp);
708 			ffth->tick_time_lerp = ffth->tick_time;
709 			bintime_clear(&gap_lerp);
710 		}
711 
712 		ffclock_status = cest->status;
713 		ffth->period_lerp = cest->period;
714 
715 		/*
716 		 * Compute corrected period used for the linear interpolation of
717 		 * time. The rate of linear interpolation is capped to 5000PPM
718 		 * (5ms/s).
719 		 */
720 		if (bintime_isset(&gap_lerp)) {
721 			ffdelta = cest->update_ffcount;
722 			ffdelta -= fftimehands->cest.update_ffcount;
723 			ffclock_convert_delta(ffdelta, cest->period, &bt);
724 			polling = bt.sec;
725 			bt.sec = 0;
726 			bt.frac = 5000000 * (uint64_t)18446744073LL;
727 			bintime_mul(&bt, polling);
728 			if (bintime_cmp(&gap_lerp, &bt, >))
729 				gap_lerp = bt;
730 
731 			/* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
732 			frac = 0;
733 			if (gap_lerp.sec > 0) {
734 				frac -= 1;
735 				frac /= ffdelta / gap_lerp.sec;
736 			}
737 			frac += gap_lerp.frac / ffdelta;
738 
739 			if (forward_jump)
740 				ffth->period_lerp += frac;
741 			else
742 				ffth->period_lerp -= frac;
743 		}
744 
745 		ffclock_updated = 0;
746 	}
747 	if (++ogen == 0)
748 		ogen = 1;
749 	ffth->gen = ogen;
750 	fftimehands = ffth;
751 }
752 
753 /*
754  * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
755  * the old and new hardware counter cannot be read simultaneously. tc_windup()
756  * does read the two counters 'back to back', but a few cycles are effectively
757  * lost, and not accumulated in tick_ffcount. This is a fairly radical
758  * operation for a feed-forward synchronization daemon, and it is its job to not
759  * pushing irrelevant data to the kernel. Because there is no locking here,
760  * simply force to ignore pending or next update to give daemon a chance to
761  * realize the counter has changed.
762  */
763 static void
764 ffclock_change_tc(struct timehands *th)
765 {
766 	struct fftimehands *ffth;
767 	struct ffclock_estimate *cest;
768 	struct timecounter *tc;
769 	uint8_t ogen;
770 
771 	tc = th->th_counter;
772 	ffth = fftimehands->next;
773 	ogen = ffth->gen;
774 	ffth->gen = 0;
775 
776 	cest = &ffth->cest;
777 	bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
778 	cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
779 	cest->errb_abs = 0;
780 	cest->errb_rate = 0;
781 	cest->status |= FFCLOCK_STA_UNSYNC;
782 
783 	ffth->tick_ffcount = fftimehands->tick_ffcount;
784 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
785 	ffth->tick_time = fftimehands->tick_time;
786 	ffth->period_lerp = cest->period;
787 
788 	/* Do not lock but ignore next update from synchronization daemon. */
789 	ffclock_updated--;
790 
791 	if (++ogen == 0)
792 		ogen = 1;
793 	ffth->gen = ogen;
794 	fftimehands = ffth;
795 }
796 
797 /*
798  * Retrieve feed-forward counter and time of last kernel tick.
799  */
800 void
801 ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
802 {
803 	struct fftimehands *ffth;
804 	uint8_t gen;
805 
806 	/*
807 	 * No locking but check generation has not changed. Also need to make
808 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
809 	 */
810 	do {
811 		ffth = fftimehands;
812 		gen = ffth->gen;
813 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
814 			*bt = ffth->tick_time_lerp;
815 		else
816 			*bt = ffth->tick_time;
817 		*ffcount = ffth->tick_ffcount;
818 	} while (gen == 0 || gen != ffth->gen);
819 }
820 
821 /*
822  * Absolute clock conversion. Low level function to convert ffcounter to
823  * bintime. The ffcounter is converted using the current ffclock period estimate
824  * or the "interpolated period" to ensure monotonicity.
825  * NOTE: this conversion may have been deferred, and the clock updated since the
826  * hardware counter has been read.
827  */
828 void
829 ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
830 {
831 	struct fftimehands *ffth;
832 	struct bintime bt2;
833 	ffcounter ffdelta;
834 	uint8_t gen;
835 
836 	/*
837 	 * No locking but check generation has not changed. Also need to make
838 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
839 	 */
840 	do {
841 		ffth = fftimehands;
842 		gen = ffth->gen;
843 		if (ffcount > ffth->tick_ffcount)
844 			ffdelta = ffcount - ffth->tick_ffcount;
845 		else
846 			ffdelta = ffth->tick_ffcount - ffcount;
847 
848 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
849 			*bt = ffth->tick_time_lerp;
850 			ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
851 		} else {
852 			*bt = ffth->tick_time;
853 			ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
854 		}
855 
856 		if (ffcount > ffth->tick_ffcount)
857 			bintime_add(bt, &bt2);
858 		else
859 			bintime_sub(bt, &bt2);
860 	} while (gen == 0 || gen != ffth->gen);
861 }
862 
863 /*
864  * Difference clock conversion.
865  * Low level function to Convert a time interval measured in RAW counter units
866  * into bintime. The difference clock allows measuring small intervals much more
867  * reliably than the absolute clock.
868  */
869 void
870 ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
871 {
872 	struct fftimehands *ffth;
873 	uint8_t gen;
874 
875 	/* No locking but check generation has not changed. */
876 	do {
877 		ffth = fftimehands;
878 		gen = ffth->gen;
879 		ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
880 	} while (gen == 0 || gen != ffth->gen);
881 }
882 
883 /*
884  * Access to current ffcounter value.
885  */
886 void
887 ffclock_read_counter(ffcounter *ffcount)
888 {
889 	struct timehands *th;
890 	struct fftimehands *ffth;
891 	unsigned int gen, delta;
892 
893 	/*
894 	 * ffclock_windup() called from tc_windup(), safe to rely on
895 	 * th->th_generation only, for correct delta and ffcounter.
896 	 */
897 	do {
898 		th = timehands;
899 		gen = atomic_load_acq_int(&th->th_generation);
900 		ffth = fftimehands;
901 		delta = tc_delta(th);
902 		*ffcount = ffth->tick_ffcount;
903 		atomic_thread_fence_acq();
904 	} while (gen == 0 || gen != th->th_generation);
905 
906 	*ffcount += delta;
907 }
908 
909 void
910 binuptime(struct bintime *bt)
911 {
912 
913 	binuptime_fromclock(bt, sysclock_active);
914 }
915 
916 void
917 nanouptime(struct timespec *tsp)
918 {
919 
920 	nanouptime_fromclock(tsp, sysclock_active);
921 }
922 
923 void
924 microuptime(struct timeval *tvp)
925 {
926 
927 	microuptime_fromclock(tvp, sysclock_active);
928 }
929 
930 void
931 bintime(struct bintime *bt)
932 {
933 
934 	bintime_fromclock(bt, sysclock_active);
935 }
936 
937 void
938 nanotime(struct timespec *tsp)
939 {
940 
941 	nanotime_fromclock(tsp, sysclock_active);
942 }
943 
944 void
945 microtime(struct timeval *tvp)
946 {
947 
948 	microtime_fromclock(tvp, sysclock_active);
949 }
950 
951 void
952 getbinuptime(struct bintime *bt)
953 {
954 
955 	getbinuptime_fromclock(bt, sysclock_active);
956 }
957 
958 void
959 getnanouptime(struct timespec *tsp)
960 {
961 
962 	getnanouptime_fromclock(tsp, sysclock_active);
963 }
964 
965 void
966 getmicrouptime(struct timeval *tvp)
967 {
968 
969 	getmicrouptime_fromclock(tvp, sysclock_active);
970 }
971 
972 void
973 getbintime(struct bintime *bt)
974 {
975 
976 	getbintime_fromclock(bt, sysclock_active);
977 }
978 
979 void
980 getnanotime(struct timespec *tsp)
981 {
982 
983 	getnanotime_fromclock(tsp, sysclock_active);
984 }
985 
986 void
987 getmicrotime(struct timeval *tvp)
988 {
989 
990 	getmicrouptime_fromclock(tvp, sysclock_active);
991 }
992 
993 #endif /* FFCLOCK */
994 
995 /*
996  * This is a clone of getnanotime and used for walltimestamps.
997  * The dtrace_ prefix prevents fbt from creating probes for
998  * it so walltimestamp can be safely used in all fbt probes.
999  */
1000 void
1001 dtrace_getnanotime(struct timespec *tsp)
1002 {
1003 	struct timehands *th;
1004 	u_int gen;
1005 
1006 	do {
1007 		th = timehands;
1008 		gen = atomic_load_acq_int(&th->th_generation);
1009 		*tsp = th->th_nanotime;
1010 		atomic_thread_fence_acq();
1011 	} while (gen == 0 || gen != th->th_generation);
1012 }
1013 
1014 /*
1015  * System clock currently providing time to the system. Modifiable via sysctl
1016  * when the FFCLOCK option is defined.
1017  */
1018 int sysclock_active = SYSCLOCK_FBCK;
1019 
1020 /* Internal NTP status and error estimates. */
1021 extern int time_status;
1022 extern long time_esterror;
1023 
1024 /*
1025  * Take a snapshot of sysclock data which can be used to compare system clocks
1026  * and generate timestamps after the fact.
1027  */
1028 void
1029 sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
1030 {
1031 	struct fbclock_info *fbi;
1032 	struct timehands *th;
1033 	struct bintime bt;
1034 	unsigned int delta, gen;
1035 #ifdef FFCLOCK
1036 	ffcounter ffcount;
1037 	struct fftimehands *ffth;
1038 	struct ffclock_info *ffi;
1039 	struct ffclock_estimate cest;
1040 
1041 	ffi = &clock_snap->ff_info;
1042 #endif
1043 
1044 	fbi = &clock_snap->fb_info;
1045 	delta = 0;
1046 
1047 	do {
1048 		th = timehands;
1049 		gen = atomic_load_acq_int(&th->th_generation);
1050 		fbi->th_scale = th->th_scale;
1051 		fbi->tick_time = th->th_offset;
1052 #ifdef FFCLOCK
1053 		ffth = fftimehands;
1054 		ffi->tick_time = ffth->tick_time_lerp;
1055 		ffi->tick_time_lerp = ffth->tick_time_lerp;
1056 		ffi->period = ffth->cest.period;
1057 		ffi->period_lerp = ffth->period_lerp;
1058 		clock_snap->ffcount = ffth->tick_ffcount;
1059 		cest = ffth->cest;
1060 #endif
1061 		if (!fast)
1062 			delta = tc_delta(th);
1063 		atomic_thread_fence_acq();
1064 	} while (gen == 0 || gen != th->th_generation);
1065 
1066 	clock_snap->delta = delta;
1067 	clock_snap->sysclock_active = sysclock_active;
1068 
1069 	/* Record feedback clock status and error. */
1070 	clock_snap->fb_info.status = time_status;
1071 	/* XXX: Very crude estimate of feedback clock error. */
1072 	bt.sec = time_esterror / 1000000;
1073 	bt.frac = ((time_esterror - bt.sec) * 1000000) *
1074 	    (uint64_t)18446744073709ULL;
1075 	clock_snap->fb_info.error = bt;
1076 
1077 #ifdef FFCLOCK
1078 	if (!fast)
1079 		clock_snap->ffcount += delta;
1080 
1081 	/* Record feed-forward clock leap second adjustment. */
1082 	ffi->leapsec_adjustment = cest.leapsec_total;
1083 	if (clock_snap->ffcount > cest.leapsec_next)
1084 		ffi->leapsec_adjustment -= cest.leapsec;
1085 
1086 	/* Record feed-forward clock status and error. */
1087 	clock_snap->ff_info.status = cest.status;
1088 	ffcount = clock_snap->ffcount - cest.update_ffcount;
1089 	ffclock_convert_delta(ffcount, cest.period, &bt);
1090 	/* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1091 	bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1092 	/* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1093 	bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1094 	clock_snap->ff_info.error = bt;
1095 #endif
1096 }
1097 
1098 /*
1099  * Convert a sysclock snapshot into a struct bintime based on the specified
1100  * clock source and flags.
1101  */
1102 int
1103 sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1104     int whichclock, uint32_t flags)
1105 {
1106 #ifdef FFCLOCK
1107 	struct bintime bt2;
1108 	uint64_t period;
1109 #endif
1110 
1111 	switch (whichclock) {
1112 	case SYSCLOCK_FBCK:
1113 		*bt = cs->fb_info.tick_time;
1114 
1115 		/* If snapshot was created with !fast, delta will be >0. */
1116 		if (cs->delta > 0)
1117 			bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1118 
1119 		if ((flags & FBCLOCK_UPTIME) == 0)
1120 			bintime_add(bt, &boottimebin);
1121 		break;
1122 #ifdef FFCLOCK
1123 	case SYSCLOCK_FFWD:
1124 		if (flags & FFCLOCK_LERP) {
1125 			*bt = cs->ff_info.tick_time_lerp;
1126 			period = cs->ff_info.period_lerp;
1127 		} else {
1128 			*bt = cs->ff_info.tick_time;
1129 			period = cs->ff_info.period;
1130 		}
1131 
1132 		/* If snapshot was created with !fast, delta will be >0. */
1133 		if (cs->delta > 0) {
1134 			ffclock_convert_delta(cs->delta, period, &bt2);
1135 			bintime_add(bt, &bt2);
1136 		}
1137 
1138 		/* Leap second adjustment. */
1139 		if (flags & FFCLOCK_LEAPSEC)
1140 			bt->sec -= cs->ff_info.leapsec_adjustment;
1141 
1142 		/* Boot time adjustment, for uptime/monotonic clocks. */
1143 		if (flags & FFCLOCK_UPTIME)
1144 			bintime_sub(bt, &ffclock_boottime);
1145 		break;
1146 #endif
1147 	default:
1148 		return (EINVAL);
1149 		break;
1150 	}
1151 
1152 	return (0);
1153 }
1154 
1155 /*
1156  * Initialize a new timecounter and possibly use it.
1157  */
1158 void
1159 tc_init(struct timecounter *tc)
1160 {
1161 	u_int u;
1162 	struct sysctl_oid *tc_root;
1163 
1164 	u = tc->tc_frequency / tc->tc_counter_mask;
1165 	/* XXX: We need some margin here, 10% is a guess */
1166 	u *= 11;
1167 	u /= 10;
1168 	if (u > hz && tc->tc_quality >= 0) {
1169 		tc->tc_quality = -2000;
1170 		if (bootverbose) {
1171 			printf("Timecounter \"%s\" frequency %ju Hz",
1172 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
1173 			printf(" -- Insufficient hz, needs at least %u\n", u);
1174 		}
1175 	} else if (tc->tc_quality >= 0 || bootverbose) {
1176 		printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1177 		    tc->tc_name, (uintmax_t)tc->tc_frequency,
1178 		    tc->tc_quality);
1179 	}
1180 
1181 	tc->tc_next = timecounters;
1182 	timecounters = tc;
1183 	/*
1184 	 * Set up sysctl tree for this counter.
1185 	 */
1186 	tc_root = SYSCTL_ADD_NODE(NULL,
1187 	    SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1188 	    CTLFLAG_RW, 0, "timecounter description");
1189 	SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1190 	    "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1191 	    "mask for implemented bits");
1192 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1193 	    "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1194 	    sysctl_kern_timecounter_get, "IU", "current timecounter value");
1195 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1196 	    "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1197 	     sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1198 	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1199 	    "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1200 	    "goodness of time counter");
1201 	/*
1202 	 * Do not automatically switch if the current tc was specifically
1203 	 * chosen.  Never automatically use a timecounter with negative quality.
1204 	 * Even though we run on the dummy counter, switching here may be
1205 	 * worse since this timecounter may not be monotonic.
1206 	 */
1207 	if (tc_chosen)
1208 		return;
1209 	if (tc->tc_quality < 0)
1210 		return;
1211 	if (tc->tc_quality < timecounter->tc_quality)
1212 		return;
1213 	if (tc->tc_quality == timecounter->tc_quality &&
1214 	    tc->tc_frequency < timecounter->tc_frequency)
1215 		return;
1216 	(void)tc->tc_get_timecount(tc);
1217 	(void)tc->tc_get_timecount(tc);
1218 	timecounter = tc;
1219 }
1220 
1221 /* Report the frequency of the current timecounter. */
1222 uint64_t
1223 tc_getfrequency(void)
1224 {
1225 
1226 	return (timehands->th_counter->tc_frequency);
1227 }
1228 
1229 /*
1230  * Step our concept of UTC.  This is done by modifying our estimate of
1231  * when we booted.
1232  * XXX: not locked.
1233  */
1234 void
1235 tc_setclock(struct timespec *ts)
1236 {
1237 	struct timespec tbef, taft;
1238 	struct bintime bt, bt2;
1239 
1240 	cpu_tick_calibrate(1);
1241 	nanotime(&tbef);
1242 	timespec2bintime(ts, &bt);
1243 	binuptime(&bt2);
1244 	bintime_sub(&bt, &bt2);
1245 	bintime_add(&bt2, &boottimebin);
1246 	boottimebin = bt;
1247 	bintime2timeval(&bt, &boottime);
1248 
1249 	/* XXX fiddle all the little crinkly bits around the fiords... */
1250 	tc_windup();
1251 	nanotime(&taft);
1252 	if (timestepwarnings) {
1253 		log(LOG_INFO,
1254 		    "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1255 		    (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1256 		    (intmax_t)taft.tv_sec, taft.tv_nsec,
1257 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
1258 	}
1259 	cpu_tick_calibrate(1);
1260 }
1261 
1262 /*
1263  * Initialize the next struct timehands in the ring and make
1264  * it the active timehands.  Along the way we might switch to a different
1265  * timecounter and/or do seconds processing in NTP.  Slightly magic.
1266  */
1267 static void
1268 tc_windup(void)
1269 {
1270 	struct bintime bt;
1271 	struct timehands *th, *tho;
1272 	uint64_t scale;
1273 	u_int delta, ncount, ogen;
1274 	int i;
1275 	time_t t;
1276 
1277 	/*
1278 	 * Make the next timehands a copy of the current one, but do
1279 	 * not overwrite the generation or next pointer.  While we
1280 	 * update the contents, the generation must be zero.  We need
1281 	 * to ensure that the zero generation is visible before the
1282 	 * data updates become visible, which requires release fence.
1283 	 * For similar reasons, re-reading of the generation after the
1284 	 * data is read should use acquire fence.
1285 	 */
1286 	tho = timehands;
1287 	th = tho->th_next;
1288 	ogen = th->th_generation;
1289 	th->th_generation = 0;
1290 	atomic_thread_fence_rel();
1291 	bcopy(tho, th, offsetof(struct timehands, th_generation));
1292 
1293 	/*
1294 	 * Capture a timecounter delta on the current timecounter and if
1295 	 * changing timecounters, a counter value from the new timecounter.
1296 	 * Update the offset fields accordingly.
1297 	 */
1298 	delta = tc_delta(th);
1299 	if (th->th_counter != timecounter)
1300 		ncount = timecounter->tc_get_timecount(timecounter);
1301 	else
1302 		ncount = 0;
1303 #ifdef FFCLOCK
1304 	ffclock_windup(delta);
1305 #endif
1306 	th->th_offset_count += delta;
1307 	th->th_offset_count &= th->th_counter->tc_counter_mask;
1308 	while (delta > th->th_counter->tc_frequency) {
1309 		/* Eat complete unadjusted seconds. */
1310 		delta -= th->th_counter->tc_frequency;
1311 		th->th_offset.sec++;
1312 	}
1313 	if ((delta > th->th_counter->tc_frequency / 2) &&
1314 	    (th->th_scale * delta < ((uint64_t)1 << 63))) {
1315 		/* The product th_scale * delta just barely overflows. */
1316 		th->th_offset.sec++;
1317 	}
1318 	bintime_addx(&th->th_offset, th->th_scale * delta);
1319 
1320 	/*
1321 	 * Hardware latching timecounters may not generate interrupts on
1322 	 * PPS events, so instead we poll them.  There is a finite risk that
1323 	 * the hardware might capture a count which is later than the one we
1324 	 * got above, and therefore possibly in the next NTP second which might
1325 	 * have a different rate than the current NTP second.  It doesn't
1326 	 * matter in practice.
1327 	 */
1328 	if (tho->th_counter->tc_poll_pps)
1329 		tho->th_counter->tc_poll_pps(tho->th_counter);
1330 
1331 	/*
1332 	 * Deal with NTP second processing.  The for loop normally
1333 	 * iterates at most once, but in extreme situations it might
1334 	 * keep NTP sane if timeouts are not run for several seconds.
1335 	 * At boot, the time step can be large when the TOD hardware
1336 	 * has been read, so on really large steps, we call
1337 	 * ntp_update_second only twice.  We need to call it twice in
1338 	 * case we missed a leap second.
1339 	 */
1340 	bt = th->th_offset;
1341 	bintime_add(&bt, &boottimebin);
1342 	i = bt.sec - tho->th_microtime.tv_sec;
1343 	if (i > LARGE_STEP)
1344 		i = 2;
1345 	for (; i > 0; i--) {
1346 		t = bt.sec;
1347 		ntp_update_second(&th->th_adjustment, &bt.sec);
1348 		if (bt.sec != t)
1349 			boottimebin.sec += bt.sec - t;
1350 	}
1351 	/* Update the UTC timestamps used by the get*() functions. */
1352 	/* XXX shouldn't do this here.  Should force non-`get' versions. */
1353 	bintime2timeval(&bt, &th->th_microtime);
1354 	bintime2timespec(&bt, &th->th_nanotime);
1355 
1356 	/* Now is a good time to change timecounters. */
1357 	if (th->th_counter != timecounter) {
1358 #ifndef __arm__
1359 		if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0)
1360 			cpu_disable_c2_sleep++;
1361 		if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
1362 			cpu_disable_c2_sleep--;
1363 #endif
1364 		th->th_counter = timecounter;
1365 		th->th_offset_count = ncount;
1366 		tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1367 		    (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1368 #ifdef FFCLOCK
1369 		ffclock_change_tc(th);
1370 #endif
1371 	}
1372 
1373 	/*-
1374 	 * Recalculate the scaling factor.  We want the number of 1/2^64
1375 	 * fractions of a second per period of the hardware counter, taking
1376 	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1377 	 * processing provides us with.
1378 	 *
1379 	 * The th_adjustment is nanoseconds per second with 32 bit binary
1380 	 * fraction and we want 64 bit binary fraction of second:
1381 	 *
1382 	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
1383 	 *
1384 	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1385 	 * we can only multiply by about 850 without overflowing, that
1386 	 * leaves no suitably precise fractions for multiply before divide.
1387 	 *
1388 	 * Divide before multiply with a fraction of 2199/512 results in a
1389 	 * systematic undercompensation of 10PPM of th_adjustment.  On a
1390 	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
1391  	 *
1392 	 * We happily sacrifice the lowest of the 64 bits of our result
1393 	 * to the goddess of code clarity.
1394 	 *
1395 	 */
1396 	scale = (uint64_t)1 << 63;
1397 	scale += (th->th_adjustment / 1024) * 2199;
1398 	scale /= th->th_counter->tc_frequency;
1399 	th->th_scale = scale * 2;
1400 
1401 	/*
1402 	 * Now that the struct timehands is again consistent, set the new
1403 	 * generation number, making sure to not make it zero.
1404 	 */
1405 	if (++ogen == 0)
1406 		ogen = 1;
1407 	atomic_store_rel_int(&th->th_generation, ogen);
1408 
1409 	/* Go live with the new struct timehands. */
1410 #ifdef FFCLOCK
1411 	switch (sysclock_active) {
1412 	case SYSCLOCK_FBCK:
1413 #endif
1414 		time_second = th->th_microtime.tv_sec;
1415 		time_uptime = th->th_offset.sec;
1416 #ifdef FFCLOCK
1417 		break;
1418 	case SYSCLOCK_FFWD:
1419 		time_second = fftimehands->tick_time_lerp.sec;
1420 		time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1421 		break;
1422 	}
1423 #endif
1424 
1425 	timehands = th;
1426 	timekeep_push_vdso();
1427 }
1428 
1429 /* Report or change the active timecounter hardware. */
1430 static int
1431 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1432 {
1433 	char newname[32];
1434 	struct timecounter *newtc, *tc;
1435 	int error;
1436 
1437 	tc = timecounter;
1438 	strlcpy(newname, tc->tc_name, sizeof(newname));
1439 
1440 	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1441 	if (error != 0 || req->newptr == NULL)
1442 		return (error);
1443 	/* Record that the tc in use now was specifically chosen. */
1444 	tc_chosen = 1;
1445 	if (strcmp(newname, tc->tc_name) == 0)
1446 		return (0);
1447 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1448 		if (strcmp(newname, newtc->tc_name) != 0)
1449 			continue;
1450 
1451 		/* Warm up new timecounter. */
1452 		(void)newtc->tc_get_timecount(newtc);
1453 		(void)newtc->tc_get_timecount(newtc);
1454 
1455 		timecounter = newtc;
1456 
1457 		/*
1458 		 * The vdso timehands update is deferred until the next
1459 		 * 'tc_windup()'.
1460 		 *
1461 		 * This is prudent given that 'timekeep_push_vdso()' does not
1462 		 * use any locking and that it can be called in hard interrupt
1463 		 * context via 'tc_windup()'.
1464 		 */
1465 		return (0);
1466 	}
1467 	return (EINVAL);
1468 }
1469 
1470 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1471     0, 0, sysctl_kern_timecounter_hardware, "A",
1472     "Timecounter hardware selected");
1473 
1474 
1475 /* Report the available timecounter hardware. */
1476 static int
1477 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1478 {
1479 	struct sbuf sb;
1480 	struct timecounter *tc;
1481 	int error;
1482 
1483 	sbuf_new_for_sysctl(&sb, NULL, 0, req);
1484 	for (tc = timecounters; tc != NULL; tc = tc->tc_next) {
1485 		if (tc != timecounters)
1486 			sbuf_putc(&sb, ' ');
1487 		sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality);
1488 	}
1489 	error = sbuf_finish(&sb);
1490 	sbuf_delete(&sb);
1491 	return (error);
1492 }
1493 
1494 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1495     0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1496 
1497 /*
1498  * RFC 2783 PPS-API implementation.
1499  */
1500 
1501 /*
1502  *  Return true if the driver is aware of the abi version extensions in the
1503  *  pps_state structure, and it supports at least the given abi version number.
1504  */
1505 static inline int
1506 abi_aware(struct pps_state *pps, int vers)
1507 {
1508 
1509 	return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers);
1510 }
1511 
1512 static int
1513 pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1514 {
1515 	int err, timo;
1516 	pps_seq_t aseq, cseq;
1517 	struct timeval tv;
1518 
1519 	if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1520 		return (EINVAL);
1521 
1522 	/*
1523 	 * If no timeout is requested, immediately return whatever values were
1524 	 * most recently captured.  If timeout seconds is -1, that's a request
1525 	 * to block without a timeout.  WITNESS won't let us sleep forever
1526 	 * without a lock (we really don't need a lock), so just repeatedly
1527 	 * sleep a long time.
1528 	 */
1529 	if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1530 		if (fapi->timeout.tv_sec == -1)
1531 			timo = 0x7fffffff;
1532 		else {
1533 			tv.tv_sec = fapi->timeout.tv_sec;
1534 			tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1535 			timo = tvtohz(&tv);
1536 		}
1537 		aseq = pps->ppsinfo.assert_sequence;
1538 		cseq = pps->ppsinfo.clear_sequence;
1539 		while (aseq == pps->ppsinfo.assert_sequence &&
1540 		    cseq == pps->ppsinfo.clear_sequence) {
1541 			if (abi_aware(pps, 1) && pps->driver_mtx != NULL) {
1542 				if (pps->flags & PPSFLAG_MTX_SPIN) {
1543 					err = msleep_spin(pps, pps->driver_mtx,
1544 					    "ppsfch", timo);
1545 				} else {
1546 					err = msleep(pps, pps->driver_mtx, PCATCH,
1547 					    "ppsfch", timo);
1548 				}
1549 			} else {
1550 				err = tsleep(pps, PCATCH, "ppsfch", timo);
1551 			}
1552 			if (err == EWOULDBLOCK) {
1553 				if (fapi->timeout.tv_sec == -1) {
1554 					continue;
1555 				} else {
1556 					return (ETIMEDOUT);
1557 				}
1558 			} else if (err != 0) {
1559 				return (err);
1560 			}
1561 		}
1562 	}
1563 
1564 	pps->ppsinfo.current_mode = pps->ppsparam.mode;
1565 	fapi->pps_info_buf = pps->ppsinfo;
1566 
1567 	return (0);
1568 }
1569 
1570 int
1571 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1572 {
1573 	pps_params_t *app;
1574 	struct pps_fetch_args *fapi;
1575 #ifdef FFCLOCK
1576 	struct pps_fetch_ffc_args *fapi_ffc;
1577 #endif
1578 #ifdef PPS_SYNC
1579 	struct pps_kcbind_args *kapi;
1580 #endif
1581 
1582 	KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1583 	switch (cmd) {
1584 	case PPS_IOC_CREATE:
1585 		return (0);
1586 	case PPS_IOC_DESTROY:
1587 		return (0);
1588 	case PPS_IOC_SETPARAMS:
1589 		app = (pps_params_t *)data;
1590 		if (app->mode & ~pps->ppscap)
1591 			return (EINVAL);
1592 #ifdef FFCLOCK
1593 		/* Ensure only a single clock is selected for ffc timestamp. */
1594 		if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1595 			return (EINVAL);
1596 #endif
1597 		pps->ppsparam = *app;
1598 		return (0);
1599 	case PPS_IOC_GETPARAMS:
1600 		app = (pps_params_t *)data;
1601 		*app = pps->ppsparam;
1602 		app->api_version = PPS_API_VERS_1;
1603 		return (0);
1604 	case PPS_IOC_GETCAP:
1605 		*(int*)data = pps->ppscap;
1606 		return (0);
1607 	case PPS_IOC_FETCH:
1608 		fapi = (struct pps_fetch_args *)data;
1609 		return (pps_fetch(fapi, pps));
1610 #ifdef FFCLOCK
1611 	case PPS_IOC_FETCH_FFCOUNTER:
1612 		fapi_ffc = (struct pps_fetch_ffc_args *)data;
1613 		if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1614 		    PPS_TSFMT_TSPEC)
1615 			return (EINVAL);
1616 		if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1617 			return (EOPNOTSUPP);
1618 		pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1619 		fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1620 		/* Overwrite timestamps if feedback clock selected. */
1621 		switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1622 		case PPS_TSCLK_FBCK:
1623 			fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1624 			    pps->ppsinfo.assert_timestamp;
1625 			fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1626 			    pps->ppsinfo.clear_timestamp;
1627 			break;
1628 		case PPS_TSCLK_FFWD:
1629 			break;
1630 		default:
1631 			break;
1632 		}
1633 		return (0);
1634 #endif /* FFCLOCK */
1635 	case PPS_IOC_KCBIND:
1636 #ifdef PPS_SYNC
1637 		kapi = (struct pps_kcbind_args *)data;
1638 		/* XXX Only root should be able to do this */
1639 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1640 			return (EINVAL);
1641 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1642 			return (EINVAL);
1643 		if (kapi->edge & ~pps->ppscap)
1644 			return (EINVAL);
1645 		pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) |
1646 		    (pps->kcmode & KCMODE_ABIFLAG);
1647 		return (0);
1648 #else
1649 		return (EOPNOTSUPP);
1650 #endif
1651 	default:
1652 		return (ENOIOCTL);
1653 	}
1654 }
1655 
1656 void
1657 pps_init(struct pps_state *pps)
1658 {
1659 	pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1660 	if (pps->ppscap & PPS_CAPTUREASSERT)
1661 		pps->ppscap |= PPS_OFFSETASSERT;
1662 	if (pps->ppscap & PPS_CAPTURECLEAR)
1663 		pps->ppscap |= PPS_OFFSETCLEAR;
1664 #ifdef FFCLOCK
1665 	pps->ppscap |= PPS_TSCLK_MASK;
1666 #endif
1667 	pps->kcmode &= ~KCMODE_ABIFLAG;
1668 }
1669 
1670 void
1671 pps_init_abi(struct pps_state *pps)
1672 {
1673 
1674 	pps_init(pps);
1675 	if (pps->driver_abi > 0) {
1676 		pps->kcmode |= KCMODE_ABIFLAG;
1677 		pps->kernel_abi = PPS_ABI_VERSION;
1678 	}
1679 }
1680 
1681 void
1682 pps_capture(struct pps_state *pps)
1683 {
1684 	struct timehands *th;
1685 
1686 	KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1687 	th = timehands;
1688 	pps->capgen = atomic_load_acq_int(&th->th_generation);
1689 	pps->capth = th;
1690 #ifdef FFCLOCK
1691 	pps->capffth = fftimehands;
1692 #endif
1693 	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1694 	atomic_thread_fence_acq();
1695 	if (pps->capgen != th->th_generation)
1696 		pps->capgen = 0;
1697 }
1698 
1699 void
1700 pps_event(struct pps_state *pps, int event)
1701 {
1702 	struct bintime bt;
1703 	struct timespec ts, *tsp, *osp;
1704 	u_int tcount, *pcount;
1705 	int foff;
1706 	pps_seq_t *pseq;
1707 #ifdef FFCLOCK
1708 	struct timespec *tsp_ffc;
1709 	pps_seq_t *pseq_ffc;
1710 	ffcounter *ffcount;
1711 #endif
1712 #ifdef PPS_SYNC
1713 	int fhard;
1714 #endif
1715 
1716 	KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1717 	/* Nothing to do if not currently set to capture this event type. */
1718 	if ((event & pps->ppsparam.mode) == 0)
1719 		return;
1720 	/* If the timecounter was wound up underneath us, bail out. */
1721 	if (pps->capgen == 0 || pps->capgen !=
1722 	    atomic_load_acq_int(&pps->capth->th_generation))
1723 		return;
1724 
1725 	/* Things would be easier with arrays. */
1726 	if (event == PPS_CAPTUREASSERT) {
1727 		tsp = &pps->ppsinfo.assert_timestamp;
1728 		osp = &pps->ppsparam.assert_offset;
1729 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1730 #ifdef PPS_SYNC
1731 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
1732 #endif
1733 		pcount = &pps->ppscount[0];
1734 		pseq = &pps->ppsinfo.assert_sequence;
1735 #ifdef FFCLOCK
1736 		ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1737 		tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1738 		pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1739 #endif
1740 	} else {
1741 		tsp = &pps->ppsinfo.clear_timestamp;
1742 		osp = &pps->ppsparam.clear_offset;
1743 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1744 #ifdef PPS_SYNC
1745 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
1746 #endif
1747 		pcount = &pps->ppscount[1];
1748 		pseq = &pps->ppsinfo.clear_sequence;
1749 #ifdef FFCLOCK
1750 		ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1751 		tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1752 		pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1753 #endif
1754 	}
1755 
1756 	/*
1757 	 * If the timecounter changed, we cannot compare the count values, so
1758 	 * we have to drop the rest of the PPS-stuff until the next event.
1759 	 */
1760 	if (pps->ppstc != pps->capth->th_counter) {
1761 		pps->ppstc = pps->capth->th_counter;
1762 		*pcount = pps->capcount;
1763 		pps->ppscount[2] = pps->capcount;
1764 		return;
1765 	}
1766 
1767 	/* Convert the count to a timespec. */
1768 	tcount = pps->capcount - pps->capth->th_offset_count;
1769 	tcount &= pps->capth->th_counter->tc_counter_mask;
1770 	bt = pps->capth->th_offset;
1771 	bintime_addx(&bt, pps->capth->th_scale * tcount);
1772 	bintime_add(&bt, &boottimebin);
1773 	bintime2timespec(&bt, &ts);
1774 
1775 	/* If the timecounter was wound up underneath us, bail out. */
1776 	atomic_thread_fence_acq();
1777 	if (pps->capgen != pps->capth->th_generation)
1778 		return;
1779 
1780 	*pcount = pps->capcount;
1781 	(*pseq)++;
1782 	*tsp = ts;
1783 
1784 	if (foff) {
1785 		timespecadd(tsp, osp);
1786 		if (tsp->tv_nsec < 0) {
1787 			tsp->tv_nsec += 1000000000;
1788 			tsp->tv_sec -= 1;
1789 		}
1790 	}
1791 
1792 #ifdef FFCLOCK
1793 	*ffcount = pps->capffth->tick_ffcount + tcount;
1794 	bt = pps->capffth->tick_time;
1795 	ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
1796 	bintime_add(&bt, &pps->capffth->tick_time);
1797 	bintime2timespec(&bt, &ts);
1798 	(*pseq_ffc)++;
1799 	*tsp_ffc = ts;
1800 #endif
1801 
1802 #ifdef PPS_SYNC
1803 	if (fhard) {
1804 		uint64_t scale;
1805 
1806 		/*
1807 		 * Feed the NTP PLL/FLL.
1808 		 * The FLL wants to know how many (hardware) nanoseconds
1809 		 * elapsed since the previous event.
1810 		 */
1811 		tcount = pps->capcount - pps->ppscount[2];
1812 		pps->ppscount[2] = pps->capcount;
1813 		tcount &= pps->capth->th_counter->tc_counter_mask;
1814 		scale = (uint64_t)1 << 63;
1815 		scale /= pps->capth->th_counter->tc_frequency;
1816 		scale *= 2;
1817 		bt.sec = 0;
1818 		bt.frac = 0;
1819 		bintime_addx(&bt, scale * tcount);
1820 		bintime2timespec(&bt, &ts);
1821 		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
1822 	}
1823 #endif
1824 
1825 	/* Wakeup anyone sleeping in pps_fetch().  */
1826 	wakeup(pps);
1827 }
1828 
1829 /*
1830  * Timecounters need to be updated every so often to prevent the hardware
1831  * counter from overflowing.  Updating also recalculates the cached values
1832  * used by the get*() family of functions, so their precision depends on
1833  * the update frequency.
1834  */
1835 
1836 static int tc_tick;
1837 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1838     "Approximate number of hardclock ticks in a millisecond");
1839 
1840 void
1841 tc_ticktock(int cnt)
1842 {
1843 	static int count;
1844 
1845 	count += cnt;
1846 	if (count < tc_tick)
1847 		return;
1848 	count = 0;
1849 	tc_windup();
1850 }
1851 
1852 static void __inline
1853 tc_adjprecision(void)
1854 {
1855 	int t;
1856 
1857 	if (tc_timepercentage > 0) {
1858 		t = (99 + tc_timepercentage) / tc_timepercentage;
1859 		tc_precexp = fls(t + (t >> 1)) - 1;
1860 		FREQ2BT(hz / tc_tick, &bt_timethreshold);
1861 		FREQ2BT(hz, &bt_tickthreshold);
1862 		bintime_shift(&bt_timethreshold, tc_precexp);
1863 		bintime_shift(&bt_tickthreshold, tc_precexp);
1864 	} else {
1865 		tc_precexp = 31;
1866 		bt_timethreshold.sec = INT_MAX;
1867 		bt_timethreshold.frac = ~(uint64_t)0;
1868 		bt_tickthreshold = bt_timethreshold;
1869 	}
1870 	sbt_timethreshold = bttosbt(bt_timethreshold);
1871 	sbt_tickthreshold = bttosbt(bt_tickthreshold);
1872 }
1873 
1874 static int
1875 sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
1876 {
1877 	int error, val;
1878 
1879 	val = tc_timepercentage;
1880 	error = sysctl_handle_int(oidp, &val, 0, req);
1881 	if (error != 0 || req->newptr == NULL)
1882 		return (error);
1883 	tc_timepercentage = val;
1884 	if (cold)
1885 		goto done;
1886 	tc_adjprecision();
1887 done:
1888 	return (0);
1889 }
1890 
1891 static void
1892 inittimecounter(void *dummy)
1893 {
1894 	u_int p;
1895 	int tick_rate;
1896 
1897 	/*
1898 	 * Set the initial timeout to
1899 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
1900 	 * People should probably not use the sysctl to set the timeout
1901 	 * to smaller than its initial value, since that value is the
1902 	 * smallest reasonable one.  If they want better timestamps they
1903 	 * should use the non-"get"* functions.
1904 	 */
1905 	if (hz > 1000)
1906 		tc_tick = (hz + 500) / 1000;
1907 	else
1908 		tc_tick = 1;
1909 	tc_adjprecision();
1910 	FREQ2BT(hz, &tick_bt);
1911 	tick_sbt = bttosbt(tick_bt);
1912 	tick_rate = hz / tc_tick;
1913 	FREQ2BT(tick_rate, &tc_tick_bt);
1914 	tc_tick_sbt = bttosbt(tc_tick_bt);
1915 	p = (tc_tick * 1000000) / hz;
1916 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
1917 
1918 #ifdef FFCLOCK
1919 	ffclock_init();
1920 #endif
1921 	/* warm up new timecounter (again) and get rolling. */
1922 	(void)timecounter->tc_get_timecount(timecounter);
1923 	(void)timecounter->tc_get_timecount(timecounter);
1924 	tc_windup();
1925 }
1926 
1927 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
1928 
1929 /* Cpu tick handling -------------------------------------------------*/
1930 
1931 static int cpu_tick_variable;
1932 static uint64_t	cpu_tick_frequency;
1933 
1934 static DPCPU_DEFINE(uint64_t, tc_cpu_ticks_base);
1935 static DPCPU_DEFINE(unsigned, tc_cpu_ticks_last);
1936 
1937 static uint64_t
1938 tc_cpu_ticks(void)
1939 {
1940 	struct timecounter *tc;
1941 	uint64_t res, *base;
1942 	unsigned u, *last;
1943 
1944 	critical_enter();
1945 	base = DPCPU_PTR(tc_cpu_ticks_base);
1946 	last = DPCPU_PTR(tc_cpu_ticks_last);
1947 	tc = timehands->th_counter;
1948 	u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
1949 	if (u < *last)
1950 		*base += (uint64_t)tc->tc_counter_mask + 1;
1951 	*last = u;
1952 	res = u + *base;
1953 	critical_exit();
1954 	return (res);
1955 }
1956 
1957 void
1958 cpu_tick_calibration(void)
1959 {
1960 	static time_t last_calib;
1961 
1962 	if (time_uptime != last_calib && !(time_uptime & 0xf)) {
1963 		cpu_tick_calibrate(0);
1964 		last_calib = time_uptime;
1965 	}
1966 }
1967 
1968 /*
1969  * This function gets called every 16 seconds on only one designated
1970  * CPU in the system from hardclock() via cpu_tick_calibration()().
1971  *
1972  * Whenever the real time clock is stepped we get called with reset=1
1973  * to make sure we handle suspend/resume and similar events correctly.
1974  */
1975 
1976 static void
1977 cpu_tick_calibrate(int reset)
1978 {
1979 	static uint64_t c_last;
1980 	uint64_t c_this, c_delta;
1981 	static struct bintime  t_last;
1982 	struct bintime t_this, t_delta;
1983 	uint32_t divi;
1984 
1985 	if (reset) {
1986 		/* The clock was stepped, abort & reset */
1987 		t_last.sec = 0;
1988 		return;
1989 	}
1990 
1991 	/* we don't calibrate fixed rate cputicks */
1992 	if (!cpu_tick_variable)
1993 		return;
1994 
1995 	getbinuptime(&t_this);
1996 	c_this = cpu_ticks();
1997 	if (t_last.sec != 0) {
1998 		c_delta = c_this - c_last;
1999 		t_delta = t_this;
2000 		bintime_sub(&t_delta, &t_last);
2001 		/*
2002 		 * Headroom:
2003 		 * 	2^(64-20) / 16[s] =
2004 		 * 	2^(44) / 16[s] =
2005 		 * 	17.592.186.044.416 / 16 =
2006 		 * 	1.099.511.627.776 [Hz]
2007 		 */
2008 		divi = t_delta.sec << 20;
2009 		divi |= t_delta.frac >> (64 - 20);
2010 		c_delta <<= 20;
2011 		c_delta /= divi;
2012 		if (c_delta > cpu_tick_frequency) {
2013 			if (0 && bootverbose)
2014 				printf("cpu_tick increased to %ju Hz\n",
2015 				    c_delta);
2016 			cpu_tick_frequency = c_delta;
2017 		}
2018 	}
2019 	c_last = c_this;
2020 	t_last = t_this;
2021 }
2022 
2023 void
2024 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
2025 {
2026 
2027 	if (func == NULL) {
2028 		cpu_ticks = tc_cpu_ticks;
2029 	} else {
2030 		cpu_tick_frequency = freq;
2031 		cpu_tick_variable = var;
2032 		cpu_ticks = func;
2033 	}
2034 }
2035 
2036 uint64_t
2037 cpu_tickrate(void)
2038 {
2039 
2040 	if (cpu_ticks == tc_cpu_ticks)
2041 		return (tc_getfrequency());
2042 	return (cpu_tick_frequency);
2043 }
2044 
2045 /*
2046  * We need to be slightly careful converting cputicks to microseconds.
2047  * There is plenty of margin in 64 bits of microseconds (half a million
2048  * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
2049  * before divide conversion (to retain precision) we find that the
2050  * margin shrinks to 1.5 hours (one millionth of 146y).
2051  * With a three prong approach we never lose significant bits, no
2052  * matter what the cputick rate and length of timeinterval is.
2053  */
2054 
2055 uint64_t
2056 cputick2usec(uint64_t tick)
2057 {
2058 
2059 	if (tick > 18446744073709551LL)		/* floor(2^64 / 1000) */
2060 		return (tick / (cpu_tickrate() / 1000000LL));
2061 	else if (tick > 18446744073709LL)	/* floor(2^64 / 1000000) */
2062 		return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
2063 	else
2064 		return ((tick * 1000000LL) / cpu_tickrate());
2065 }
2066 
2067 cpu_tick_f	*cpu_ticks = tc_cpu_ticks;
2068 
2069 static int vdso_th_enable = 1;
2070 static int
2071 sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
2072 {
2073 	int old_vdso_th_enable, error;
2074 
2075 	old_vdso_th_enable = vdso_th_enable;
2076 	error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
2077 	if (error != 0)
2078 		return (error);
2079 	vdso_th_enable = old_vdso_th_enable;
2080 	return (0);
2081 }
2082 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
2083     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2084     NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
2085 
2086 uint32_t
2087 tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
2088 {
2089 	struct timehands *th;
2090 	uint32_t enabled;
2091 
2092 	th = timehands;
2093 	vdso_th->th_algo = VDSO_TH_ALGO_1;
2094 	vdso_th->th_scale = th->th_scale;
2095 	vdso_th->th_offset_count = th->th_offset_count;
2096 	vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
2097 	vdso_th->th_offset = th->th_offset;
2098 	vdso_th->th_boottime = boottimebin;
2099 	enabled = cpu_fill_vdso_timehands(vdso_th, th->th_counter);
2100 	if (!vdso_th_enable)
2101 		enabled = 0;
2102 	return (enabled);
2103 }
2104 
2105 #ifdef COMPAT_FREEBSD32
2106 uint32_t
2107 tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
2108 {
2109 	struct timehands *th;
2110 	uint32_t enabled;
2111 
2112 	th = timehands;
2113 	vdso_th32->th_algo = VDSO_TH_ALGO_1;
2114 	*(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
2115 	vdso_th32->th_offset_count = th->th_offset_count;
2116 	vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
2117 	vdso_th32->th_offset.sec = th->th_offset.sec;
2118 	*(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
2119 	vdso_th32->th_boottime.sec = boottimebin.sec;
2120 	*(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
2121 	enabled = cpu_fill_vdso_timehands32(vdso_th32, th->th_counter);
2122 	if (!vdso_th_enable)
2123 		enabled = 0;
2124 	return (enabled);
2125 }
2126 #endif
2127