kern_tc.c revision 282424
12061Sjkh/*-
214119Speter * ----------------------------------------------------------------------------
32061Sjkh * "THE BEER-WARE LICENSE" (Revision 42):
42061Sjkh * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
58854Srgrimes * can do whatever you want with this stuff. If we meet some day, and you think
62061Sjkh * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
72061Sjkh * ----------------------------------------------------------------------------
83197Scsgr *
93197Scsgr * Copyright (c) 2011 The FreeBSD Foundation
102061Sjkh * All rights reserved.
1112483Speter *
122160Scsgr * Portions of this software were developed by Julien Ridoux at the University
132834Swollman * of Melbourne under sponsorship from the FreeBSD Foundation.
142061Sjkh */
152061Sjkh
162160Scsgr#include <sys/cdefs.h>
171594Srgrimes__FBSDID("$FreeBSD: head/sys/kern/kern_tc.c 282424 2015-05-04 17:59:39Z ian $");
182061Sjkh
192061Sjkh#include "opt_compat.h"
201594Srgrimes#include "opt_ntp.h"
217407Srgrimes#include "opt_ffclock.h"
227407Srgrimes
237108Sphk#include <sys/param.h>
247108Sphk#include <sys/kernel.h>
257108Sphk#include <sys/limits.h>
267407Srgrimes#include <sys/lock.h>
277407Srgrimes#include <sys/mutex.h>
287407Srgrimes#include <sys/sbuf.h>
297108Sphk#include <sys/sysctl.h>
302061Sjkh#include <sys/syslog.h>
312061Sjkh#include <sys/systm.h>
322061Sjkh#include <sys/timeffc.h>
332061Sjkh#include <sys/timepps.h>
342061Sjkh#include <sys/timetc.h>
352061Sjkh#include <sys/timex.h>
362061Sjkh#include <sys/vdso.h>
372061Sjkh
382061Sjkh/*
392061Sjkh * A large step happens on boot.  This constant detects such steps.
402061Sjkh * It is relatively small so that ntp_update_second gets called enough
412061Sjkh * in the typical 'missed a couple of seconds' case, but doesn't loop
423197Scsgr * forever when the time step is large.
432626Scsgr */
442626Scsgr#define LARGE_STEP	200
452061Sjkh
462061Sjkh/*
472061Sjkh * Implement a dummy timecounter which we can use until we get a real one
482061Sjkh * in the air.  This allows the console and other early stuff to use
492061Sjkh * time services.
502061Sjkh */
512061Sjkh
522061Sjkhstatic u_int
532061Sjkhdummy_get_timecount(struct timecounter *tc)
542061Sjkh{
552061Sjkh	static u_int now;
562061Sjkh
572061Sjkh	return (++now);
582061Sjkh}
592061Sjkh
602061Sjkhstatic struct timecounter dummy_timecounter = {
612061Sjkh	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
622061Sjkh};
632834Swollman
642834Swollmanstruct timehands {
652834Swollman	/* These fields must be initialized by the driver. */
662834Swollman	struct timecounter	*th_counter;
672834Swollman	int64_t			th_adjustment;
682834Swollman	uint64_t		th_scale;
691594Srgrimes	u_int	 		th_offset_count;
704486Sphk	struct bintime		th_offset;
714486Sphk	struct timeval		th_microtime;
724486Sphk	struct timespec		th_nanotime;
734486Sphk	/* Fields not to be copied in tc_windup start with th_generation. */
744486Sphk	volatile u_int		th_generation;
752061Sjkh	struct timehands	*th_next;
762061Sjkh};
772061Sjkh
782061Sjkhstatic struct timehands th0;
792061Sjkhstatic struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
802061Sjkhstatic struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
812061Sjkhstatic struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
822061Sjkhstatic struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
832061Sjkhstatic struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
842061Sjkhstatic struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
852061Sjkhstatic struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
862061Sjkhstatic struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
872061Sjkhstatic struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
882061Sjkhstatic struct timehands th0 = {
892061Sjkh	&dummy_timecounter,
9012483Speter	0,
9112483Speter	(uint64_t)-1 / 1000000,
9212483Speter	0,
9312483Speter	{1, 0},
9412483Speter	{0, 0},
9512483Speter	{0, 0},
962061Sjkh	1,
972061Sjkh	&th1
988854Srgrimes};
992061Sjkh
1002061Sjkhstatic struct timehands *volatile timehands = &th0;
10112483Speterstruct timecounter *timecounter = &dummy_timecounter;
1022061Sjkhstatic struct timecounter *timecounters = &dummy_timecounter;
10314119Speter
1042061Sjkhint tc_min_ticktock_freq = 1;
1052061Sjkh
1062061Sjkhvolatile time_t time_second = 1;
1072061Sjkhvolatile time_t time_uptime = 1;
1082061Sjkh
1092061Sjkhstruct bintime boottimebin;
1102061Sjkhstruct timeval boottime;
11112483Speterstatic int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
11212483SpeterSYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
11312483Speter    NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
11412483Speter
11512483SpeterSYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
11612483Speterstatic SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
11712483Speter
11812483Speterstatic int timestepwarnings;
1193030SrgrimesSYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
1202061Sjkh    &timestepwarnings, 0, "Log time steps");
1213030Srgrimes
1222061Sjkhstruct bintime bt_timethreshold;
1236722Sphkstruct bintime bt_tickthreshold;
1242061Sjkhsbintime_t sbt_timethreshold;
1252302Spaulsbintime_t sbt_tickthreshold;
1262302Spaulstruct bintime tc_tick_bt;
1272302Spaulsbintime_t tc_tick_sbt;
1282302Spaulint tc_precexp;
1292302Spaulint tc_timepercentage = TC_DEFAULTPERC;
1302302Spaulstatic int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS);
13110760SacheSYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation,
13210760Sache    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
1332302Spaul    sysctl_kern_timecounter_adjprecision, "I",
13410760Sache    "Allowed time interval deviation in percents");
13510760Sache
13610760Sachestatic void tc_windup(void);
13710760Sachestatic void cpu_tick_calibrate(int);
1382302Spaul
1392302Spaulvoid dtrace_getnanotime(struct timespec *tsp);
1402302Spaul
1412302Spaulstatic int
1422302Spaulsysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
1432302Spaul{
1442302Spaul#ifndef __mips__
1452061Sjkh#ifdef SCTL_MASK32
1462061Sjkh	int tv[2];
1472061Sjkh
1482061Sjkh	if (req->flags & SCTL_MASK32) {
1492061Sjkh		tv[0] = boottime.tv_sec;
1502061Sjkh		tv[1] = boottime.tv_usec;
1512061Sjkh		return SYSCTL_OUT(req, tv, sizeof(tv));
1522061Sjkh	} else
1532061Sjkh#endif
1542061Sjkh#endif
1552061Sjkh		return SYSCTL_OUT(req, &boottime, sizeof(boottime));
1562061Sjkh}
1572061Sjkh
1582061Sjkhstatic int
1592061Sjkhsysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
1602061Sjkh{
1612061Sjkh	u_int ncount;
1622061Sjkh	struct timecounter *tc = arg1;
1632061Sjkh
1642061Sjkh	ncount = tc->tc_get_timecount(tc);
1652061Sjkh	return sysctl_handle_int(oidp, &ncount, 0, req);
1662061Sjkh}
1672061Sjkh
1682061Sjkhstatic int
1692061Sjkhsysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
1702061Sjkh{
1713626Swollman	uint64_t freq;
1723626Swollman	struct timecounter *tc = arg1;
1733626Swollman
1743626Swollman	freq = tc->tc_frequency;
1753626Swollman	return sysctl_handle_64(oidp, &freq, 0, req);
1763626Swollman}
1773626Swollman
1783626Swollman/*
1793626Swollman * Return the difference between the timehands' counter value now and what
1803626Swollman * was when we copied it to the timehands' offset_count.
1813626Swollman */
1827059Srobertostatic __inline u_int
1833626Swollmantc_delta(struct timehands *th)
1843626Swollman{
1853626Swollman	struct timecounter *tc;
1863626Swollman
1873626Swollman	tc = th->th_counter;
1883626Swollman	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
1893626Swollman	    tc->tc_counter_mask);
1903626Swollman}
1913626Swollman
1923626Swollman/*
1933626Swollman * Functions for reading the time.  We have to loop until we are sure that
1943626Swollman * the timehands that we operated on was not updated under our feet.  See
1953626Swollman * the comment in <sys/time.h> for a description of these 12 functions.
1963626Swollman */
1973626Swollman
1983626Swollman#ifdef FFCLOCK
1993626Swollmanvoid
2003626Swollmanfbclock_binuptime(struct bintime *bt)
2017446Ssos{
2023626Swollman	struct timehands *th;
2033626Swollman	unsigned int gen;
2043626Swollman
2053626Swollman	do {
2063626Swollman		th = timehands;
2073626Swollman		gen = th->th_generation;
2083626Swollman		*bt = th->th_offset;
2092061Sjkh		bintime_addx(bt, th->th_scale * tc_delta(th));
2102061Sjkh	} while (gen == 0 || gen != th->th_generation);
2112061Sjkh}
2122061Sjkh
2132061Sjkhvoid
2142061Sjkhfbclock_nanouptime(struct timespec *tsp)
21514119Speter{
2162061Sjkh	struct bintime bt;
2172061Sjkh
2182061Sjkh	fbclock_binuptime(&bt);
2192061Sjkh	bintime2timespec(&bt, tsp);
2202061Sjkh}
2217130Srgrimes
2227130Srgrimesvoid
2237130Srgrimesfbclock_microuptime(struct timeval *tvp)
2242061Sjkh{
2252061Sjkh	struct bintime bt;
2264249Sache
2272685Srgrimes	fbclock_binuptime(&bt);
2286927Snate	bintime2timeval(&bt, tvp);
2292685Srgrimes}
2303518Sache
2313197Scsgrvoid
2323197Scsgrfbclock_bintime(struct bintime *bt)
23312166Sjkh{
23412485Sjkh
2353197Scsgr	fbclock_binuptime(bt);
2362061Sjkh	bintime_add(bt, &boottimebin);
2372061Sjkh}
2382061Sjkh
2392883Sphkvoid
2403429Sachefbclock_nanotime(struct timespec *tsp)
2413429Sache{
2427281Srgrimes	struct bintime bt;
2433242Spaul
2443242Spaul	fbclock_bintime(&bt);
2457171Sats	bintime2timespec(&bt, tsp);
2462061Sjkh}
2473213Spst
2484942Sachevoid
2495749Swollmanfbclock_microtime(struct timeval *tvp)
2505772Swollman{
2515865Sache	struct bintime bt;
2525866Sache
25313138Speter	fbclock_bintime(&bt);
2542061Sjkh	bintime2timeval(&bt, tvp);
2555366Snate}
2565366Snate
2576934Ssevoid
2585366Snatefbclock_getbinuptime(struct bintime *bt)
2595366Snate{
26012507Snate	struct timehands *th;
26112507Snate	unsigned int gen;
2627292Srgrimes
2637292Srgrimes	do {
2645366Snate		th = timehands;
2655366Snate		gen = th->th_generation;
2665366Snate		*bt = th->th_offset;
2675366Snate	} while (gen == 0 || gen != th->th_generation);
2685366Snate}
2695366Snate
2705772Swollmanvoid
2715772Swollmanfbclock_getnanouptime(struct timespec *tsp)
2725728Swollman{
2735728Swollman	struct timehands *th;
2745728Swollman	unsigned int gen;
2755728Swollman
2765728Swollman	do {
2775366Snate		th = timehands;
2782061Sjkh		gen = th->th_generation;
2792061Sjkh		bintime2timespec(&th->th_offset, tsp);
2802061Sjkh	} while (gen == 0 || gen != th->th_generation);
2812061Sjkh}
2822061Sjkh
2832061Sjkhvoid
2842061Sjkhfbclock_getmicrouptime(struct timeval *tvp)
2852061Sjkh{
2862061Sjkh	struct timehands *th;
2878295Srgrimes	unsigned int gen;
2888295Srgrimes
28911772Snate	do {
2908295Srgrimes		th = timehands;
2918489Srgrimes		gen = th->th_generation;
2928489Srgrimes		bintime2timeval(&th->th_offset, tvp);
29311772Snate	} while (gen == 0 || gen != th->th_generation);
2948489Srgrimes}
2958489Srgrimes
2968489Srgrimesvoid
29711772Snatefbclock_getbintime(struct bintime *bt)
2988489Srgrimes{
2998295Srgrimes	struct timehands *th;
3002468Spaul	unsigned int gen;
3012061Sjkh
3022273Spaul	do {
3032061Sjkh		th = timehands;
3048295Srgrimes		gen = th->th_generation;
3052160Scsgr		*bt = th->th_offset;
3062160Scsgr	} while (gen == 0 || gen != th->th_generation);
3072160Scsgr	bintime_add(bt, &boottimebin);
3082160Scsgr}
3092279Spaul
3104054Spstvoid
3114054Spstfbclock_getnanotime(struct timespec *tsp)
3122061Sjkh{
3132061Sjkh	struct timehands *th;
3142279Spaul	unsigned int gen;
31511772Snate
3162468Spaul	do {
3172468Spaul		th = timehands;
31811772Snate		gen = th->th_generation;
3193197Scsgr		*tsp = th->th_nanotime;
32010838Sjkh	} while (gen == 0 || gen != th->th_generation);
3212626Scsgr}
3222626Scsgr
3238304Srgrimesvoid
3248304Srgrimesfbclock_getmicrotime(struct timeval *tvp)
32511772Snate{
3268304Srgrimes	struct timehands *th;
3272061Sjkh	unsigned int gen;
32813725Snate
32913725Snate	do {
33013725Snate		th = timehands;
33113725Snate		gen = th->th_generation;
33213725Snate		*tvp = th->th_microtime;
33313726Snate	} while (gen == 0 || gen != th->th_generation);
33413726Snate}
33513725Snate#else /* !FFCLOCK */
33613725Snatevoid
33713725Snatebinuptime(struct bintime *bt)
33811806Sphk{
3392061Sjkh	struct timehands *th;
34012106Sjfieber	u_int gen;
3412061Sjkh
3422061Sjkh	do {
3432273Spaul		th = timehands;
3442061Sjkh		gen = th->th_generation;
3452061Sjkh		*bt = th->th_offset;
3462061Sjkh		bintime_addx(bt, th->th_scale * tc_delta(th));
34711769Sphk	} while (gen == 0 || gen != th->th_generation);
34811769Sphk}
34912106Sjfieber
35012106Sjfiebervoid
35112106Sjfiebernanouptime(struct timespec *tsp)
35212106Sjfieber{
35310479Sdg	struct bintime bt;
35410479Sdg
3552061Sjkh	binuptime(&bt);
3561594Srgrimes	bintime2timespec(&bt, tsp);
357}
358
359void
360microuptime(struct timeval *tvp)
361{
362	struct bintime bt;
363
364	binuptime(&bt);
365	bintime2timeval(&bt, tvp);
366}
367
368void
369bintime(struct bintime *bt)
370{
371
372	binuptime(bt);
373	bintime_add(bt, &boottimebin);
374}
375
376void
377nanotime(struct timespec *tsp)
378{
379	struct bintime bt;
380
381	bintime(&bt);
382	bintime2timespec(&bt, tsp);
383}
384
385void
386microtime(struct timeval *tvp)
387{
388	struct bintime bt;
389
390	bintime(&bt);
391	bintime2timeval(&bt, tvp);
392}
393
394void
395getbinuptime(struct bintime *bt)
396{
397	struct timehands *th;
398	u_int gen;
399
400	do {
401		th = timehands;
402		gen = th->th_generation;
403		*bt = th->th_offset;
404	} while (gen == 0 || gen != th->th_generation);
405}
406
407void
408getnanouptime(struct timespec *tsp)
409{
410	struct timehands *th;
411	u_int gen;
412
413	do {
414		th = timehands;
415		gen = th->th_generation;
416		bintime2timespec(&th->th_offset, tsp);
417	} while (gen == 0 || gen != th->th_generation);
418}
419
420void
421getmicrouptime(struct timeval *tvp)
422{
423	struct timehands *th;
424	u_int gen;
425
426	do {
427		th = timehands;
428		gen = th->th_generation;
429		bintime2timeval(&th->th_offset, tvp);
430	} while (gen == 0 || gen != th->th_generation);
431}
432
433void
434getbintime(struct bintime *bt)
435{
436	struct timehands *th;
437	u_int gen;
438
439	do {
440		th = timehands;
441		gen = th->th_generation;
442		*bt = th->th_offset;
443	} while (gen == 0 || gen != th->th_generation);
444	bintime_add(bt, &boottimebin);
445}
446
447void
448getnanotime(struct timespec *tsp)
449{
450	struct timehands *th;
451	u_int gen;
452
453	do {
454		th = timehands;
455		gen = th->th_generation;
456		*tsp = th->th_nanotime;
457	} while (gen == 0 || gen != th->th_generation);
458}
459
460void
461getmicrotime(struct timeval *tvp)
462{
463	struct timehands *th;
464	u_int gen;
465
466	do {
467		th = timehands;
468		gen = th->th_generation;
469		*tvp = th->th_microtime;
470	} while (gen == 0 || gen != th->th_generation);
471}
472#endif /* FFCLOCK */
473
474#ifdef FFCLOCK
475/*
476 * Support for feed-forward synchronization algorithms. This is heavily inspired
477 * by the timehands mechanism but kept independent from it. *_windup() functions
478 * have some connection to avoid accessing the timecounter hardware more than
479 * necessary.
480 */
481
482/* Feed-forward clock estimates kept updated by the synchronization daemon. */
483struct ffclock_estimate ffclock_estimate;
484struct bintime ffclock_boottime;	/* Feed-forward boot time estimate. */
485uint32_t ffclock_status;		/* Feed-forward clock status. */
486int8_t ffclock_updated;			/* New estimates are available. */
487struct mtx ffclock_mtx;			/* Mutex on ffclock_estimate. */
488
489struct fftimehands {
490	struct ffclock_estimate	cest;
491	struct bintime		tick_time;
492	struct bintime		tick_time_lerp;
493	ffcounter		tick_ffcount;
494	uint64_t		period_lerp;
495	volatile uint8_t	gen;
496	struct fftimehands	*next;
497};
498
499#define	NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
500
501static struct fftimehands ffth[10];
502static struct fftimehands *volatile fftimehands = ffth;
503
504static void
505ffclock_init(void)
506{
507	struct fftimehands *cur;
508	struct fftimehands *last;
509
510	memset(ffth, 0, sizeof(ffth));
511
512	last = ffth + NUM_ELEMENTS(ffth) - 1;
513	for (cur = ffth; cur < last; cur++)
514		cur->next = cur + 1;
515	last->next = ffth;
516
517	ffclock_updated = 0;
518	ffclock_status = FFCLOCK_STA_UNSYNC;
519	mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
520}
521
522/*
523 * Reset the feed-forward clock estimates. Called from inittodr() to get things
524 * kick started and uses the timecounter nominal frequency as a first period
525 * estimate. Note: this function may be called several time just after boot.
526 * Note: this is the only function that sets the value of boot time for the
527 * monotonic (i.e. uptime) version of the feed-forward clock.
528 */
529void
530ffclock_reset_clock(struct timespec *ts)
531{
532	struct timecounter *tc;
533	struct ffclock_estimate cest;
534
535	tc = timehands->th_counter;
536	memset(&cest, 0, sizeof(struct ffclock_estimate));
537
538	timespec2bintime(ts, &ffclock_boottime);
539	timespec2bintime(ts, &(cest.update_time));
540	ffclock_read_counter(&cest.update_ffcount);
541	cest.leapsec_next = 0;
542	cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
543	cest.errb_abs = 0;
544	cest.errb_rate = 0;
545	cest.status = FFCLOCK_STA_UNSYNC;
546	cest.leapsec_total = 0;
547	cest.leapsec = 0;
548
549	mtx_lock(&ffclock_mtx);
550	bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
551	ffclock_updated = INT8_MAX;
552	mtx_unlock(&ffclock_mtx);
553
554	printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
555	    (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
556	    (unsigned long)ts->tv_nsec);
557}
558
559/*
560 * Sub-routine to convert a time interval measured in RAW counter units to time
561 * in seconds stored in bintime format.
562 * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
563 * larger than the max value of u_int (on 32 bit architecture). Loop to consume
564 * extra cycles.
565 */
566static void
567ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
568{
569	struct bintime bt2;
570	ffcounter delta, delta_max;
571
572	delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
573	bintime_clear(bt);
574	do {
575		if (ffdelta > delta_max)
576			delta = delta_max;
577		else
578			delta = ffdelta;
579		bt2.sec = 0;
580		bt2.frac = period;
581		bintime_mul(&bt2, (unsigned int)delta);
582		bintime_add(bt, &bt2);
583		ffdelta -= delta;
584	} while (ffdelta > 0);
585}
586
587/*
588 * Update the fftimehands.
589 * Push the tick ffcount and time(s) forward based on current clock estimate.
590 * The conversion from ffcounter to bintime relies on the difference clock
591 * principle, whose accuracy relies on computing small time intervals. If a new
592 * clock estimate has been passed by the synchronisation daemon, make it
593 * current, and compute the linear interpolation for monotonic time if needed.
594 */
595static void
596ffclock_windup(unsigned int delta)
597{
598	struct ffclock_estimate *cest;
599	struct fftimehands *ffth;
600	struct bintime bt, gap_lerp;
601	ffcounter ffdelta;
602	uint64_t frac;
603	unsigned int polling;
604	uint8_t forward_jump, ogen;
605
606	/*
607	 * Pick the next timehand, copy current ffclock estimates and move tick
608	 * times and counter forward.
609	 */
610	forward_jump = 0;
611	ffth = fftimehands->next;
612	ogen = ffth->gen;
613	ffth->gen = 0;
614	cest = &ffth->cest;
615	bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
616	ffdelta = (ffcounter)delta;
617	ffth->period_lerp = fftimehands->period_lerp;
618
619	ffth->tick_time = fftimehands->tick_time;
620	ffclock_convert_delta(ffdelta, cest->period, &bt);
621	bintime_add(&ffth->tick_time, &bt);
622
623	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
624	ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
625	bintime_add(&ffth->tick_time_lerp, &bt);
626
627	ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
628
629	/*
630	 * Assess the status of the clock, if the last update is too old, it is
631	 * likely the synchronisation daemon is dead and the clock is free
632	 * running.
633	 */
634	if (ffclock_updated == 0) {
635		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
636		ffclock_convert_delta(ffdelta, cest->period, &bt);
637		if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
638			ffclock_status |= FFCLOCK_STA_UNSYNC;
639	}
640
641	/*
642	 * If available, grab updated clock estimates and make them current.
643	 * Recompute time at this tick using the updated estimates. The clock
644	 * estimates passed the feed-forward synchronisation daemon may result
645	 * in time conversion that is not monotonically increasing (just after
646	 * the update). time_lerp is a particular linear interpolation over the
647	 * synchronisation algo polling period that ensures monotonicity for the
648	 * clock ids requesting it.
649	 */
650	if (ffclock_updated > 0) {
651		bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
652		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
653		ffth->tick_time = cest->update_time;
654		ffclock_convert_delta(ffdelta, cest->period, &bt);
655		bintime_add(&ffth->tick_time, &bt);
656
657		/* ffclock_reset sets ffclock_updated to INT8_MAX */
658		if (ffclock_updated == INT8_MAX)
659			ffth->tick_time_lerp = ffth->tick_time;
660
661		if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
662			forward_jump = 1;
663		else
664			forward_jump = 0;
665
666		bintime_clear(&gap_lerp);
667		if (forward_jump) {
668			gap_lerp = ffth->tick_time;
669			bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
670		} else {
671			gap_lerp = ffth->tick_time_lerp;
672			bintime_sub(&gap_lerp, &ffth->tick_time);
673		}
674
675		/*
676		 * The reset from the RTC clock may be far from accurate, and
677		 * reducing the gap between real time and interpolated time
678		 * could take a very long time if the interpolated clock insists
679		 * on strict monotonicity. The clock is reset under very strict
680		 * conditions (kernel time is known to be wrong and
681		 * synchronization daemon has been restarted recently.
682		 * ffclock_boottime absorbs the jump to ensure boot time is
683		 * correct and uptime functions stay consistent.
684		 */
685		if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
686		    ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
687		    ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
688			if (forward_jump)
689				bintime_add(&ffclock_boottime, &gap_lerp);
690			else
691				bintime_sub(&ffclock_boottime, &gap_lerp);
692			ffth->tick_time_lerp = ffth->tick_time;
693			bintime_clear(&gap_lerp);
694		}
695
696		ffclock_status = cest->status;
697		ffth->period_lerp = cest->period;
698
699		/*
700		 * Compute corrected period used for the linear interpolation of
701		 * time. The rate of linear interpolation is capped to 5000PPM
702		 * (5ms/s).
703		 */
704		if (bintime_isset(&gap_lerp)) {
705			ffdelta = cest->update_ffcount;
706			ffdelta -= fftimehands->cest.update_ffcount;
707			ffclock_convert_delta(ffdelta, cest->period, &bt);
708			polling = bt.sec;
709			bt.sec = 0;
710			bt.frac = 5000000 * (uint64_t)18446744073LL;
711			bintime_mul(&bt, polling);
712			if (bintime_cmp(&gap_lerp, &bt, >))
713				gap_lerp = bt;
714
715			/* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
716			frac = 0;
717			if (gap_lerp.sec > 0) {
718				frac -= 1;
719				frac /= ffdelta / gap_lerp.sec;
720			}
721			frac += gap_lerp.frac / ffdelta;
722
723			if (forward_jump)
724				ffth->period_lerp += frac;
725			else
726				ffth->period_lerp -= frac;
727		}
728
729		ffclock_updated = 0;
730	}
731	if (++ogen == 0)
732		ogen = 1;
733	ffth->gen = ogen;
734	fftimehands = ffth;
735}
736
737/*
738 * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
739 * the old and new hardware counter cannot be read simultaneously. tc_windup()
740 * does read the two counters 'back to back', but a few cycles are effectively
741 * lost, and not accumulated in tick_ffcount. This is a fairly radical
742 * operation for a feed-forward synchronization daemon, and it is its job to not
743 * pushing irrelevant data to the kernel. Because there is no locking here,
744 * simply force to ignore pending or next update to give daemon a chance to
745 * realize the counter has changed.
746 */
747static void
748ffclock_change_tc(struct timehands *th)
749{
750	struct fftimehands *ffth;
751	struct ffclock_estimate *cest;
752	struct timecounter *tc;
753	uint8_t ogen;
754
755	tc = th->th_counter;
756	ffth = fftimehands->next;
757	ogen = ffth->gen;
758	ffth->gen = 0;
759
760	cest = &ffth->cest;
761	bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
762	cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
763	cest->errb_abs = 0;
764	cest->errb_rate = 0;
765	cest->status |= FFCLOCK_STA_UNSYNC;
766
767	ffth->tick_ffcount = fftimehands->tick_ffcount;
768	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
769	ffth->tick_time = fftimehands->tick_time;
770	ffth->period_lerp = cest->period;
771
772	/* Do not lock but ignore next update from synchronization daemon. */
773	ffclock_updated--;
774
775	if (++ogen == 0)
776		ogen = 1;
777	ffth->gen = ogen;
778	fftimehands = ffth;
779}
780
781/*
782 * Retrieve feed-forward counter and time of last kernel tick.
783 */
784void
785ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
786{
787	struct fftimehands *ffth;
788	uint8_t gen;
789
790	/*
791	 * No locking but check generation has not changed. Also need to make
792	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
793	 */
794	do {
795		ffth = fftimehands;
796		gen = ffth->gen;
797		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
798			*bt = ffth->tick_time_lerp;
799		else
800			*bt = ffth->tick_time;
801		*ffcount = ffth->tick_ffcount;
802	} while (gen == 0 || gen != ffth->gen);
803}
804
805/*
806 * Absolute clock conversion. Low level function to convert ffcounter to
807 * bintime. The ffcounter is converted using the current ffclock period estimate
808 * or the "interpolated period" to ensure monotonicity.
809 * NOTE: this conversion may have been deferred, and the clock updated since the
810 * hardware counter has been read.
811 */
812void
813ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
814{
815	struct fftimehands *ffth;
816	struct bintime bt2;
817	ffcounter ffdelta;
818	uint8_t gen;
819
820	/*
821	 * No locking but check generation has not changed. Also need to make
822	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
823	 */
824	do {
825		ffth = fftimehands;
826		gen = ffth->gen;
827		if (ffcount > ffth->tick_ffcount)
828			ffdelta = ffcount - ffth->tick_ffcount;
829		else
830			ffdelta = ffth->tick_ffcount - ffcount;
831
832		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
833			*bt = ffth->tick_time_lerp;
834			ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
835		} else {
836			*bt = ffth->tick_time;
837			ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
838		}
839
840		if (ffcount > ffth->tick_ffcount)
841			bintime_add(bt, &bt2);
842		else
843			bintime_sub(bt, &bt2);
844	} while (gen == 0 || gen != ffth->gen);
845}
846
847/*
848 * Difference clock conversion.
849 * Low level function to Convert a time interval measured in RAW counter units
850 * into bintime. The difference clock allows measuring small intervals much more
851 * reliably than the absolute clock.
852 */
853void
854ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
855{
856	struct fftimehands *ffth;
857	uint8_t gen;
858
859	/* No locking but check generation has not changed. */
860	do {
861		ffth = fftimehands;
862		gen = ffth->gen;
863		ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
864	} while (gen == 0 || gen != ffth->gen);
865}
866
867/*
868 * Access to current ffcounter value.
869 */
870void
871ffclock_read_counter(ffcounter *ffcount)
872{
873	struct timehands *th;
874	struct fftimehands *ffth;
875	unsigned int gen, delta;
876
877	/*
878	 * ffclock_windup() called from tc_windup(), safe to rely on
879	 * th->th_generation only, for correct delta and ffcounter.
880	 */
881	do {
882		th = timehands;
883		gen = th->th_generation;
884		ffth = fftimehands;
885		delta = tc_delta(th);
886		*ffcount = ffth->tick_ffcount;
887	} while (gen == 0 || gen != th->th_generation);
888
889	*ffcount += delta;
890}
891
892void
893binuptime(struct bintime *bt)
894{
895
896	binuptime_fromclock(bt, sysclock_active);
897}
898
899void
900nanouptime(struct timespec *tsp)
901{
902
903	nanouptime_fromclock(tsp, sysclock_active);
904}
905
906void
907microuptime(struct timeval *tvp)
908{
909
910	microuptime_fromclock(tvp, sysclock_active);
911}
912
913void
914bintime(struct bintime *bt)
915{
916
917	bintime_fromclock(bt, sysclock_active);
918}
919
920void
921nanotime(struct timespec *tsp)
922{
923
924	nanotime_fromclock(tsp, sysclock_active);
925}
926
927void
928microtime(struct timeval *tvp)
929{
930
931	microtime_fromclock(tvp, sysclock_active);
932}
933
934void
935getbinuptime(struct bintime *bt)
936{
937
938	getbinuptime_fromclock(bt, sysclock_active);
939}
940
941void
942getnanouptime(struct timespec *tsp)
943{
944
945	getnanouptime_fromclock(tsp, sysclock_active);
946}
947
948void
949getmicrouptime(struct timeval *tvp)
950{
951
952	getmicrouptime_fromclock(tvp, sysclock_active);
953}
954
955void
956getbintime(struct bintime *bt)
957{
958
959	getbintime_fromclock(bt, sysclock_active);
960}
961
962void
963getnanotime(struct timespec *tsp)
964{
965
966	getnanotime_fromclock(tsp, sysclock_active);
967}
968
969void
970getmicrotime(struct timeval *tvp)
971{
972
973	getmicrouptime_fromclock(tvp, sysclock_active);
974}
975
976#endif /* FFCLOCK */
977
978/*
979 * This is a clone of getnanotime and used for walltimestamps.
980 * The dtrace_ prefix prevents fbt from creating probes for
981 * it so walltimestamp can be safely used in all fbt probes.
982 */
983void
984dtrace_getnanotime(struct timespec *tsp)
985{
986	struct timehands *th;
987	u_int gen;
988
989	do {
990		th = timehands;
991		gen = th->th_generation;
992		*tsp = th->th_nanotime;
993	} while (gen == 0 || gen != th->th_generation);
994}
995
996/*
997 * System clock currently providing time to the system. Modifiable via sysctl
998 * when the FFCLOCK option is defined.
999 */
1000int sysclock_active = SYSCLOCK_FBCK;
1001
1002/* Internal NTP status and error estimates. */
1003extern int time_status;
1004extern long time_esterror;
1005
1006/*
1007 * Take a snapshot of sysclock data which can be used to compare system clocks
1008 * and generate timestamps after the fact.
1009 */
1010void
1011sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
1012{
1013	struct fbclock_info *fbi;
1014	struct timehands *th;
1015	struct bintime bt;
1016	unsigned int delta, gen;
1017#ifdef FFCLOCK
1018	ffcounter ffcount;
1019	struct fftimehands *ffth;
1020	struct ffclock_info *ffi;
1021	struct ffclock_estimate cest;
1022
1023	ffi = &clock_snap->ff_info;
1024#endif
1025
1026	fbi = &clock_snap->fb_info;
1027	delta = 0;
1028
1029	do {
1030		th = timehands;
1031		gen = th->th_generation;
1032		fbi->th_scale = th->th_scale;
1033		fbi->tick_time = th->th_offset;
1034#ifdef FFCLOCK
1035		ffth = fftimehands;
1036		ffi->tick_time = ffth->tick_time_lerp;
1037		ffi->tick_time_lerp = ffth->tick_time_lerp;
1038		ffi->period = ffth->cest.period;
1039		ffi->period_lerp = ffth->period_lerp;
1040		clock_snap->ffcount = ffth->tick_ffcount;
1041		cest = ffth->cest;
1042#endif
1043		if (!fast)
1044			delta = tc_delta(th);
1045	} while (gen == 0 || gen != th->th_generation);
1046
1047	clock_snap->delta = delta;
1048	clock_snap->sysclock_active = sysclock_active;
1049
1050	/* Record feedback clock status and error. */
1051	clock_snap->fb_info.status = time_status;
1052	/* XXX: Very crude estimate of feedback clock error. */
1053	bt.sec = time_esterror / 1000000;
1054	bt.frac = ((time_esterror - bt.sec) * 1000000) *
1055	    (uint64_t)18446744073709ULL;
1056	clock_snap->fb_info.error = bt;
1057
1058#ifdef FFCLOCK
1059	if (!fast)
1060		clock_snap->ffcount += delta;
1061
1062	/* Record feed-forward clock leap second adjustment. */
1063	ffi->leapsec_adjustment = cest.leapsec_total;
1064	if (clock_snap->ffcount > cest.leapsec_next)
1065		ffi->leapsec_adjustment -= cest.leapsec;
1066
1067	/* Record feed-forward clock status and error. */
1068	clock_snap->ff_info.status = cest.status;
1069	ffcount = clock_snap->ffcount - cest.update_ffcount;
1070	ffclock_convert_delta(ffcount, cest.period, &bt);
1071	/* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1072	bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1073	/* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1074	bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1075	clock_snap->ff_info.error = bt;
1076#endif
1077}
1078
1079/*
1080 * Convert a sysclock snapshot into a struct bintime based on the specified
1081 * clock source and flags.
1082 */
1083int
1084sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1085    int whichclock, uint32_t flags)
1086{
1087#ifdef FFCLOCK
1088	struct bintime bt2;
1089	uint64_t period;
1090#endif
1091
1092	switch (whichclock) {
1093	case SYSCLOCK_FBCK:
1094		*bt = cs->fb_info.tick_time;
1095
1096		/* If snapshot was created with !fast, delta will be >0. */
1097		if (cs->delta > 0)
1098			bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1099
1100		if ((flags & FBCLOCK_UPTIME) == 0)
1101			bintime_add(bt, &boottimebin);
1102		break;
1103#ifdef FFCLOCK
1104	case SYSCLOCK_FFWD:
1105		if (flags & FFCLOCK_LERP) {
1106			*bt = cs->ff_info.tick_time_lerp;
1107			period = cs->ff_info.period_lerp;
1108		} else {
1109			*bt = cs->ff_info.tick_time;
1110			period = cs->ff_info.period;
1111		}
1112
1113		/* If snapshot was created with !fast, delta will be >0. */
1114		if (cs->delta > 0) {
1115			ffclock_convert_delta(cs->delta, period, &bt2);
1116			bintime_add(bt, &bt2);
1117		}
1118
1119		/* Leap second adjustment. */
1120		if (flags & FFCLOCK_LEAPSEC)
1121			bt->sec -= cs->ff_info.leapsec_adjustment;
1122
1123		/* Boot time adjustment, for uptime/monotonic clocks. */
1124		if (flags & FFCLOCK_UPTIME)
1125			bintime_sub(bt, &ffclock_boottime);
1126		break;
1127#endif
1128	default:
1129		return (EINVAL);
1130		break;
1131	}
1132
1133	return (0);
1134}
1135
1136/*
1137 * Initialize a new timecounter and possibly use it.
1138 */
1139void
1140tc_init(struct timecounter *tc)
1141{
1142	u_int u;
1143	struct sysctl_oid *tc_root;
1144
1145	u = tc->tc_frequency / tc->tc_counter_mask;
1146	/* XXX: We need some margin here, 10% is a guess */
1147	u *= 11;
1148	u /= 10;
1149	if (u > hz && tc->tc_quality >= 0) {
1150		tc->tc_quality = -2000;
1151		if (bootverbose) {
1152			printf("Timecounter \"%s\" frequency %ju Hz",
1153			    tc->tc_name, (uintmax_t)tc->tc_frequency);
1154			printf(" -- Insufficient hz, needs at least %u\n", u);
1155		}
1156	} else if (tc->tc_quality >= 0 || bootverbose) {
1157		printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1158		    tc->tc_name, (uintmax_t)tc->tc_frequency,
1159		    tc->tc_quality);
1160	}
1161
1162	tc->tc_next = timecounters;
1163	timecounters = tc;
1164	/*
1165	 * Set up sysctl tree for this counter.
1166	 */
1167	tc_root = SYSCTL_ADD_NODE(NULL,
1168	    SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1169	    CTLFLAG_RW, 0, "timecounter description");
1170	SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1171	    "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1172	    "mask for implemented bits");
1173	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1174	    "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1175	    sysctl_kern_timecounter_get, "IU", "current timecounter value");
1176	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1177	    "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1178	     sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1179	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1180	    "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1181	    "goodness of time counter");
1182	/*
1183	 * Never automatically use a timecounter with negative quality.
1184	 * Even though we run on the dummy counter, switching here may be
1185	 * worse since this timecounter may not be monotonous.
1186	 */
1187	if (tc->tc_quality < 0)
1188		return;
1189	if (tc->tc_quality < timecounter->tc_quality)
1190		return;
1191	if (tc->tc_quality == timecounter->tc_quality &&
1192	    tc->tc_frequency < timecounter->tc_frequency)
1193		return;
1194	(void)tc->tc_get_timecount(tc);
1195	(void)tc->tc_get_timecount(tc);
1196	timecounter = tc;
1197}
1198
1199/* Report the frequency of the current timecounter. */
1200uint64_t
1201tc_getfrequency(void)
1202{
1203
1204	return (timehands->th_counter->tc_frequency);
1205}
1206
1207/*
1208 * Step our concept of UTC.  This is done by modifying our estimate of
1209 * when we booted.
1210 * XXX: not locked.
1211 */
1212void
1213tc_setclock(struct timespec *ts)
1214{
1215	struct timespec tbef, taft;
1216	struct bintime bt, bt2;
1217
1218	cpu_tick_calibrate(1);
1219	nanotime(&tbef);
1220	timespec2bintime(ts, &bt);
1221	binuptime(&bt2);
1222	bintime_sub(&bt, &bt2);
1223	bintime_add(&bt2, &boottimebin);
1224	boottimebin = bt;
1225	bintime2timeval(&bt, &boottime);
1226
1227	/* XXX fiddle all the little crinkly bits around the fiords... */
1228	tc_windup();
1229	nanotime(&taft);
1230	if (timestepwarnings) {
1231		log(LOG_INFO,
1232		    "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1233		    (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1234		    (intmax_t)taft.tv_sec, taft.tv_nsec,
1235		    (intmax_t)ts->tv_sec, ts->tv_nsec);
1236	}
1237	cpu_tick_calibrate(1);
1238}
1239
1240/*
1241 * Initialize the next struct timehands in the ring and make
1242 * it the active timehands.  Along the way we might switch to a different
1243 * timecounter and/or do seconds processing in NTP.  Slightly magic.
1244 */
1245static void
1246tc_windup(void)
1247{
1248	struct bintime bt;
1249	struct timehands *th, *tho;
1250	uint64_t scale;
1251	u_int delta, ncount, ogen;
1252	int i;
1253	time_t t;
1254
1255	/*
1256	 * Make the next timehands a copy of the current one, but do not
1257	 * overwrite the generation or next pointer.  While we update
1258	 * the contents, the generation must be zero.
1259	 */
1260	tho = timehands;
1261	th = tho->th_next;
1262	ogen = th->th_generation;
1263	th->th_generation = 0;
1264	bcopy(tho, th, offsetof(struct timehands, th_generation));
1265
1266	/*
1267	 * Capture a timecounter delta on the current timecounter and if
1268	 * changing timecounters, a counter value from the new timecounter.
1269	 * Update the offset fields accordingly.
1270	 */
1271	delta = tc_delta(th);
1272	if (th->th_counter != timecounter)
1273		ncount = timecounter->tc_get_timecount(timecounter);
1274	else
1275		ncount = 0;
1276#ifdef FFCLOCK
1277	ffclock_windup(delta);
1278#endif
1279	th->th_offset_count += delta;
1280	th->th_offset_count &= th->th_counter->tc_counter_mask;
1281	while (delta > th->th_counter->tc_frequency) {
1282		/* Eat complete unadjusted seconds. */
1283		delta -= th->th_counter->tc_frequency;
1284		th->th_offset.sec++;
1285	}
1286	if ((delta > th->th_counter->tc_frequency / 2) &&
1287	    (th->th_scale * delta < ((uint64_t)1 << 63))) {
1288		/* The product th_scale * delta just barely overflows. */
1289		th->th_offset.sec++;
1290	}
1291	bintime_addx(&th->th_offset, th->th_scale * delta);
1292
1293	/*
1294	 * Hardware latching timecounters may not generate interrupts on
1295	 * PPS events, so instead we poll them.  There is a finite risk that
1296	 * the hardware might capture a count which is later than the one we
1297	 * got above, and therefore possibly in the next NTP second which might
1298	 * have a different rate than the current NTP second.  It doesn't
1299	 * matter in practice.
1300	 */
1301	if (tho->th_counter->tc_poll_pps)
1302		tho->th_counter->tc_poll_pps(tho->th_counter);
1303
1304	/*
1305	 * Deal with NTP second processing.  The for loop normally
1306	 * iterates at most once, but in extreme situations it might
1307	 * keep NTP sane if timeouts are not run for several seconds.
1308	 * At boot, the time step can be large when the TOD hardware
1309	 * has been read, so on really large steps, we call
1310	 * ntp_update_second only twice.  We need to call it twice in
1311	 * case we missed a leap second.
1312	 */
1313	bt = th->th_offset;
1314	bintime_add(&bt, &boottimebin);
1315	i = bt.sec - tho->th_microtime.tv_sec;
1316	if (i > LARGE_STEP)
1317		i = 2;
1318	for (; i > 0; i--) {
1319		t = bt.sec;
1320		ntp_update_second(&th->th_adjustment, &bt.sec);
1321		if (bt.sec != t)
1322			boottimebin.sec += bt.sec - t;
1323	}
1324	/* Update the UTC timestamps used by the get*() functions. */
1325	/* XXX shouldn't do this here.  Should force non-`get' versions. */
1326	bintime2timeval(&bt, &th->th_microtime);
1327	bintime2timespec(&bt, &th->th_nanotime);
1328
1329	/* Now is a good time to change timecounters. */
1330	if (th->th_counter != timecounter) {
1331#ifndef __arm__
1332		if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0)
1333			cpu_disable_c2_sleep++;
1334		if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
1335			cpu_disable_c2_sleep--;
1336#endif
1337		th->th_counter = timecounter;
1338		th->th_offset_count = ncount;
1339		tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1340		    (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1341#ifdef FFCLOCK
1342		ffclock_change_tc(th);
1343#endif
1344	}
1345
1346	/*-
1347	 * Recalculate the scaling factor.  We want the number of 1/2^64
1348	 * fractions of a second per period of the hardware counter, taking
1349	 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1350	 * processing provides us with.
1351	 *
1352	 * The th_adjustment is nanoseconds per second with 32 bit binary
1353	 * fraction and we want 64 bit binary fraction of second:
1354	 *
1355	 *	 x = a * 2^32 / 10^9 = a * 4.294967296
1356	 *
1357	 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1358	 * we can only multiply by about 850 without overflowing, that
1359	 * leaves no suitably precise fractions for multiply before divide.
1360	 *
1361	 * Divide before multiply with a fraction of 2199/512 results in a
1362	 * systematic undercompensation of 10PPM of th_adjustment.  On a
1363	 * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
1364 	 *
1365	 * We happily sacrifice the lowest of the 64 bits of our result
1366	 * to the goddess of code clarity.
1367	 *
1368	 */
1369	scale = (uint64_t)1 << 63;
1370	scale += (th->th_adjustment / 1024) * 2199;
1371	scale /= th->th_counter->tc_frequency;
1372	th->th_scale = scale * 2;
1373
1374	/*
1375	 * Now that the struct timehands is again consistent, set the new
1376	 * generation number, making sure to not make it zero.
1377	 */
1378	if (++ogen == 0)
1379		ogen = 1;
1380	th->th_generation = ogen;
1381
1382	/* Go live with the new struct timehands. */
1383#ifdef FFCLOCK
1384	switch (sysclock_active) {
1385	case SYSCLOCK_FBCK:
1386#endif
1387		time_second = th->th_microtime.tv_sec;
1388		time_uptime = th->th_offset.sec;
1389#ifdef FFCLOCK
1390		break;
1391	case SYSCLOCK_FFWD:
1392		time_second = fftimehands->tick_time_lerp.sec;
1393		time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1394		break;
1395	}
1396#endif
1397
1398	timehands = th;
1399	timekeep_push_vdso();
1400}
1401
1402/* Report or change the active timecounter hardware. */
1403static int
1404sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1405{
1406	char newname[32];
1407	struct timecounter *newtc, *tc;
1408	int error;
1409
1410	tc = timecounter;
1411	strlcpy(newname, tc->tc_name, sizeof(newname));
1412
1413	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1414	if (error != 0 || req->newptr == NULL ||
1415	    strcmp(newname, tc->tc_name) == 0)
1416		return (error);
1417	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1418		if (strcmp(newname, newtc->tc_name) != 0)
1419			continue;
1420
1421		/* Warm up new timecounter. */
1422		(void)newtc->tc_get_timecount(newtc);
1423		(void)newtc->tc_get_timecount(newtc);
1424
1425		timecounter = newtc;
1426
1427		/*
1428		 * The vdso timehands update is deferred until the next
1429		 * 'tc_windup()'.
1430		 *
1431		 * This is prudent given that 'timekeep_push_vdso()' does not
1432		 * use any locking and that it can be called in hard interrupt
1433		 * context via 'tc_windup()'.
1434		 */
1435		return (0);
1436	}
1437	return (EINVAL);
1438}
1439
1440SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1441    0, 0, sysctl_kern_timecounter_hardware, "A",
1442    "Timecounter hardware selected");
1443
1444
1445/* Report or change the active timecounter hardware. */
1446static int
1447sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1448{
1449	struct sbuf sb;
1450	struct timecounter *tc;
1451	int error;
1452
1453	sbuf_new_for_sysctl(&sb, NULL, 0, req);
1454	for (tc = timecounters; tc != NULL; tc = tc->tc_next) {
1455		if (tc != timecounters)
1456			sbuf_putc(&sb, ' ');
1457		sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality);
1458	}
1459	error = sbuf_finish(&sb);
1460	sbuf_delete(&sb);
1461	return (error);
1462}
1463
1464SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1465    0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1466
1467/*
1468 * RFC 2783 PPS-API implementation.
1469 */
1470
1471/*
1472 *  Return true if the driver is aware of the abi version extensions in the
1473 *  pps_state structure, and it supports at least the given abi version number.
1474 */
1475static inline int
1476abi_aware(struct pps_state *pps, int vers)
1477{
1478
1479	return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers);
1480}
1481
1482static int
1483pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1484{
1485	int err, timo;
1486	pps_seq_t aseq, cseq;
1487	struct timeval tv;
1488
1489	if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1490		return (EINVAL);
1491
1492	/*
1493	 * If no timeout is requested, immediately return whatever values were
1494	 * most recently captured.  If timeout seconds is -1, that's a request
1495	 * to block without a timeout.  WITNESS won't let us sleep forever
1496	 * without a lock (we really don't need a lock), so just repeatedly
1497	 * sleep a long time.
1498	 */
1499	if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1500		if (fapi->timeout.tv_sec == -1)
1501			timo = 0x7fffffff;
1502		else {
1503			tv.tv_sec = fapi->timeout.tv_sec;
1504			tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1505			timo = tvtohz(&tv);
1506		}
1507		aseq = pps->ppsinfo.assert_sequence;
1508		cseq = pps->ppsinfo.clear_sequence;
1509		while (aseq == pps->ppsinfo.assert_sequence &&
1510		    cseq == pps->ppsinfo.clear_sequence) {
1511			if (abi_aware(pps, 1) && pps->driver_mtx != NULL) {
1512				if (pps->flags & PPSFLAG_MTX_SPIN) {
1513					err = msleep_spin(pps, pps->driver_mtx,
1514					    "ppsfch", timo);
1515				} else {
1516					err = msleep(pps, pps->driver_mtx, PCATCH,
1517					    "ppsfch", timo);
1518				}
1519			} else {
1520				err = tsleep(pps, PCATCH, "ppsfch", timo);
1521			}
1522			if (err == EWOULDBLOCK && fapi->timeout.tv_sec == -1) {
1523				continue;
1524			} else if (err != 0) {
1525				return (err);
1526			}
1527		}
1528	}
1529
1530	pps->ppsinfo.current_mode = pps->ppsparam.mode;
1531	fapi->pps_info_buf = pps->ppsinfo;
1532
1533	return (0);
1534}
1535
1536int
1537pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1538{
1539	pps_params_t *app;
1540	struct pps_fetch_args *fapi;
1541#ifdef FFCLOCK
1542	struct pps_fetch_ffc_args *fapi_ffc;
1543#endif
1544#ifdef PPS_SYNC
1545	struct pps_kcbind_args *kapi;
1546#endif
1547
1548	KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1549	switch (cmd) {
1550	case PPS_IOC_CREATE:
1551		return (0);
1552	case PPS_IOC_DESTROY:
1553		return (0);
1554	case PPS_IOC_SETPARAMS:
1555		app = (pps_params_t *)data;
1556		if (app->mode & ~pps->ppscap)
1557			return (EINVAL);
1558#ifdef FFCLOCK
1559		/* Ensure only a single clock is selected for ffc timestamp. */
1560		if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1561			return (EINVAL);
1562#endif
1563		pps->ppsparam = *app;
1564		return (0);
1565	case PPS_IOC_GETPARAMS:
1566		app = (pps_params_t *)data;
1567		*app = pps->ppsparam;
1568		app->api_version = PPS_API_VERS_1;
1569		return (0);
1570	case PPS_IOC_GETCAP:
1571		*(int*)data = pps->ppscap;
1572		return (0);
1573	case PPS_IOC_FETCH:
1574		fapi = (struct pps_fetch_args *)data;
1575		return (pps_fetch(fapi, pps));
1576#ifdef FFCLOCK
1577	case PPS_IOC_FETCH_FFCOUNTER:
1578		fapi_ffc = (struct pps_fetch_ffc_args *)data;
1579		if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1580		    PPS_TSFMT_TSPEC)
1581			return (EINVAL);
1582		if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1583			return (EOPNOTSUPP);
1584		pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1585		fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1586		/* Overwrite timestamps if feedback clock selected. */
1587		switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1588		case PPS_TSCLK_FBCK:
1589			fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1590			    pps->ppsinfo.assert_timestamp;
1591			fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1592			    pps->ppsinfo.clear_timestamp;
1593			break;
1594		case PPS_TSCLK_FFWD:
1595			break;
1596		default:
1597			break;
1598		}
1599		return (0);
1600#endif /* FFCLOCK */
1601	case PPS_IOC_KCBIND:
1602#ifdef PPS_SYNC
1603		kapi = (struct pps_kcbind_args *)data;
1604		/* XXX Only root should be able to do this */
1605		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1606			return (EINVAL);
1607		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1608			return (EINVAL);
1609		if (kapi->edge & ~pps->ppscap)
1610			return (EINVAL);
1611		pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) |
1612		    (pps->kcmode & KCMODE_ABIFLAG);
1613		return (0);
1614#else
1615		return (EOPNOTSUPP);
1616#endif
1617	default:
1618		return (ENOIOCTL);
1619	}
1620}
1621
1622void
1623pps_init(struct pps_state *pps)
1624{
1625	pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1626	if (pps->ppscap & PPS_CAPTUREASSERT)
1627		pps->ppscap |= PPS_OFFSETASSERT;
1628	if (pps->ppscap & PPS_CAPTURECLEAR)
1629		pps->ppscap |= PPS_OFFSETCLEAR;
1630#ifdef FFCLOCK
1631	pps->ppscap |= PPS_TSCLK_MASK;
1632#endif
1633	pps->kcmode &= ~KCMODE_ABIFLAG;
1634}
1635
1636void
1637pps_init_abi(struct pps_state *pps)
1638{
1639
1640	pps_init(pps);
1641	if (pps->driver_abi > 0) {
1642		pps->kcmode |= KCMODE_ABIFLAG;
1643		pps->kernel_abi = PPS_ABI_VERSION;
1644	}
1645}
1646
1647void
1648pps_capture(struct pps_state *pps)
1649{
1650	struct timehands *th;
1651
1652	KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1653	th = timehands;
1654	pps->capgen = th->th_generation;
1655	pps->capth = th;
1656#ifdef FFCLOCK
1657	pps->capffth = fftimehands;
1658#endif
1659	pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1660	if (pps->capgen != th->th_generation)
1661		pps->capgen = 0;
1662}
1663
1664void
1665pps_event(struct pps_state *pps, int event)
1666{
1667	struct bintime bt;
1668	struct timespec ts, *tsp, *osp;
1669	u_int tcount, *pcount;
1670	int foff, fhard;
1671	pps_seq_t *pseq;
1672#ifdef FFCLOCK
1673	struct timespec *tsp_ffc;
1674	pps_seq_t *pseq_ffc;
1675	ffcounter *ffcount;
1676#endif
1677
1678	KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1679	/* If the timecounter was wound up underneath us, bail out. */
1680	if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
1681		return;
1682
1683	/* Things would be easier with arrays. */
1684	if (event == PPS_CAPTUREASSERT) {
1685		tsp = &pps->ppsinfo.assert_timestamp;
1686		osp = &pps->ppsparam.assert_offset;
1687		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1688		fhard = pps->kcmode & PPS_CAPTUREASSERT;
1689		pcount = &pps->ppscount[0];
1690		pseq = &pps->ppsinfo.assert_sequence;
1691#ifdef FFCLOCK
1692		ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1693		tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1694		pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1695#endif
1696	} else {
1697		tsp = &pps->ppsinfo.clear_timestamp;
1698		osp = &pps->ppsparam.clear_offset;
1699		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1700		fhard = pps->kcmode & PPS_CAPTURECLEAR;
1701		pcount = &pps->ppscount[1];
1702		pseq = &pps->ppsinfo.clear_sequence;
1703#ifdef FFCLOCK
1704		ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1705		tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1706		pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1707#endif
1708	}
1709
1710	/*
1711	 * If the timecounter changed, we cannot compare the count values, so
1712	 * we have to drop the rest of the PPS-stuff until the next event.
1713	 */
1714	if (pps->ppstc != pps->capth->th_counter) {
1715		pps->ppstc = pps->capth->th_counter;
1716		*pcount = pps->capcount;
1717		pps->ppscount[2] = pps->capcount;
1718		return;
1719	}
1720
1721	/* Convert the count to a timespec. */
1722	tcount = pps->capcount - pps->capth->th_offset_count;
1723	tcount &= pps->capth->th_counter->tc_counter_mask;
1724	bt = pps->capth->th_offset;
1725	bintime_addx(&bt, pps->capth->th_scale * tcount);
1726	bintime_add(&bt, &boottimebin);
1727	bintime2timespec(&bt, &ts);
1728
1729	/* If the timecounter was wound up underneath us, bail out. */
1730	if (pps->capgen != pps->capth->th_generation)
1731		return;
1732
1733	*pcount = pps->capcount;
1734	(*pseq)++;
1735	*tsp = ts;
1736
1737	if (foff) {
1738		timespecadd(tsp, osp);
1739		if (tsp->tv_nsec < 0) {
1740			tsp->tv_nsec += 1000000000;
1741			tsp->tv_sec -= 1;
1742		}
1743	}
1744
1745#ifdef FFCLOCK
1746	*ffcount = pps->capffth->tick_ffcount + tcount;
1747	bt = pps->capffth->tick_time;
1748	ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
1749	bintime_add(&bt, &pps->capffth->tick_time);
1750	bintime2timespec(&bt, &ts);
1751	(*pseq_ffc)++;
1752	*tsp_ffc = ts;
1753#endif
1754
1755#ifdef PPS_SYNC
1756	if (fhard) {
1757		uint64_t scale;
1758
1759		/*
1760		 * Feed the NTP PLL/FLL.
1761		 * The FLL wants to know how many (hardware) nanoseconds
1762		 * elapsed since the previous event.
1763		 */
1764		tcount = pps->capcount - pps->ppscount[2];
1765		pps->ppscount[2] = pps->capcount;
1766		tcount &= pps->capth->th_counter->tc_counter_mask;
1767		scale = (uint64_t)1 << 63;
1768		scale /= pps->capth->th_counter->tc_frequency;
1769		scale *= 2;
1770		bt.sec = 0;
1771		bt.frac = 0;
1772		bintime_addx(&bt, scale * tcount);
1773		bintime2timespec(&bt, &ts);
1774		hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
1775	}
1776#endif
1777
1778	/* Wakeup anyone sleeping in pps_fetch().  */
1779	wakeup(pps);
1780}
1781
1782/*
1783 * Timecounters need to be updated every so often to prevent the hardware
1784 * counter from overflowing.  Updating also recalculates the cached values
1785 * used by the get*() family of functions, so their precision depends on
1786 * the update frequency.
1787 */
1788
1789static int tc_tick;
1790SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1791    "Approximate number of hardclock ticks in a millisecond");
1792
1793void
1794tc_ticktock(int cnt)
1795{
1796	static int count;
1797
1798	count += cnt;
1799	if (count < tc_tick)
1800		return;
1801	count = 0;
1802	tc_windup();
1803}
1804
1805static void __inline
1806tc_adjprecision(void)
1807{
1808	int t;
1809
1810	if (tc_timepercentage > 0) {
1811		t = (99 + tc_timepercentage) / tc_timepercentage;
1812		tc_precexp = fls(t + (t >> 1)) - 1;
1813		FREQ2BT(hz / tc_tick, &bt_timethreshold);
1814		FREQ2BT(hz, &bt_tickthreshold);
1815		bintime_shift(&bt_timethreshold, tc_precexp);
1816		bintime_shift(&bt_tickthreshold, tc_precexp);
1817	} else {
1818		tc_precexp = 31;
1819		bt_timethreshold.sec = INT_MAX;
1820		bt_timethreshold.frac = ~(uint64_t)0;
1821		bt_tickthreshold = bt_timethreshold;
1822	}
1823	sbt_timethreshold = bttosbt(bt_timethreshold);
1824	sbt_tickthreshold = bttosbt(bt_tickthreshold);
1825}
1826
1827static int
1828sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
1829{
1830	int error, val;
1831
1832	val = tc_timepercentage;
1833	error = sysctl_handle_int(oidp, &val, 0, req);
1834	if (error != 0 || req->newptr == NULL)
1835		return (error);
1836	tc_timepercentage = val;
1837	if (cold)
1838		goto done;
1839	tc_adjprecision();
1840done:
1841	return (0);
1842}
1843
1844static void
1845inittimecounter(void *dummy)
1846{
1847	u_int p;
1848	int tick_rate;
1849
1850	/*
1851	 * Set the initial timeout to
1852	 * max(1, <approx. number of hardclock ticks in a millisecond>).
1853	 * People should probably not use the sysctl to set the timeout
1854	 * to smaller than its inital value, since that value is the
1855	 * smallest reasonable one.  If they want better timestamps they
1856	 * should use the non-"get"* functions.
1857	 */
1858	if (hz > 1000)
1859		tc_tick = (hz + 500) / 1000;
1860	else
1861		tc_tick = 1;
1862	tc_adjprecision();
1863	FREQ2BT(hz, &tick_bt);
1864	tick_sbt = bttosbt(tick_bt);
1865	tick_rate = hz / tc_tick;
1866	FREQ2BT(tick_rate, &tc_tick_bt);
1867	tc_tick_sbt = bttosbt(tc_tick_bt);
1868	p = (tc_tick * 1000000) / hz;
1869	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
1870
1871#ifdef FFCLOCK
1872	ffclock_init();
1873#endif
1874	/* warm up new timecounter (again) and get rolling. */
1875	(void)timecounter->tc_get_timecount(timecounter);
1876	(void)timecounter->tc_get_timecount(timecounter);
1877	tc_windup();
1878}
1879
1880SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
1881
1882/* Cpu tick handling -------------------------------------------------*/
1883
1884static int cpu_tick_variable;
1885static uint64_t	cpu_tick_frequency;
1886
1887static uint64_t
1888tc_cpu_ticks(void)
1889{
1890	static uint64_t base;
1891	static unsigned last;
1892	unsigned u;
1893	struct timecounter *tc;
1894
1895	tc = timehands->th_counter;
1896	u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
1897	if (u < last)
1898		base += (uint64_t)tc->tc_counter_mask + 1;
1899	last = u;
1900	return (u + base);
1901}
1902
1903void
1904cpu_tick_calibration(void)
1905{
1906	static time_t last_calib;
1907
1908	if (time_uptime != last_calib && !(time_uptime & 0xf)) {
1909		cpu_tick_calibrate(0);
1910		last_calib = time_uptime;
1911	}
1912}
1913
1914/*
1915 * This function gets called every 16 seconds on only one designated
1916 * CPU in the system from hardclock() via cpu_tick_calibration()().
1917 *
1918 * Whenever the real time clock is stepped we get called with reset=1
1919 * to make sure we handle suspend/resume and similar events correctly.
1920 */
1921
1922static void
1923cpu_tick_calibrate(int reset)
1924{
1925	static uint64_t c_last;
1926	uint64_t c_this, c_delta;
1927	static struct bintime  t_last;
1928	struct bintime t_this, t_delta;
1929	uint32_t divi;
1930
1931	if (reset) {
1932		/* The clock was stepped, abort & reset */
1933		t_last.sec = 0;
1934		return;
1935	}
1936
1937	/* we don't calibrate fixed rate cputicks */
1938	if (!cpu_tick_variable)
1939		return;
1940
1941	getbinuptime(&t_this);
1942	c_this = cpu_ticks();
1943	if (t_last.sec != 0) {
1944		c_delta = c_this - c_last;
1945		t_delta = t_this;
1946		bintime_sub(&t_delta, &t_last);
1947		/*
1948		 * Headroom:
1949		 * 	2^(64-20) / 16[s] =
1950		 * 	2^(44) / 16[s] =
1951		 * 	17.592.186.044.416 / 16 =
1952		 * 	1.099.511.627.776 [Hz]
1953		 */
1954		divi = t_delta.sec << 20;
1955		divi |= t_delta.frac >> (64 - 20);
1956		c_delta <<= 20;
1957		c_delta /= divi;
1958		if (c_delta > cpu_tick_frequency) {
1959			if (0 && bootverbose)
1960				printf("cpu_tick increased to %ju Hz\n",
1961				    c_delta);
1962			cpu_tick_frequency = c_delta;
1963		}
1964	}
1965	c_last = c_this;
1966	t_last = t_this;
1967}
1968
1969void
1970set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
1971{
1972
1973	if (func == NULL) {
1974		cpu_ticks = tc_cpu_ticks;
1975	} else {
1976		cpu_tick_frequency = freq;
1977		cpu_tick_variable = var;
1978		cpu_ticks = func;
1979	}
1980}
1981
1982uint64_t
1983cpu_tickrate(void)
1984{
1985
1986	if (cpu_ticks == tc_cpu_ticks)
1987		return (tc_getfrequency());
1988	return (cpu_tick_frequency);
1989}
1990
1991/*
1992 * We need to be slightly careful converting cputicks to microseconds.
1993 * There is plenty of margin in 64 bits of microseconds (half a million
1994 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
1995 * before divide conversion (to retain precision) we find that the
1996 * margin shrinks to 1.5 hours (one millionth of 146y).
1997 * With a three prong approach we never lose significant bits, no
1998 * matter what the cputick rate and length of timeinterval is.
1999 */
2000
2001uint64_t
2002cputick2usec(uint64_t tick)
2003{
2004
2005	if (tick > 18446744073709551LL)		/* floor(2^64 / 1000) */
2006		return (tick / (cpu_tickrate() / 1000000LL));
2007	else if (tick > 18446744073709LL)	/* floor(2^64 / 1000000) */
2008		return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
2009	else
2010		return ((tick * 1000000LL) / cpu_tickrate());
2011}
2012
2013cpu_tick_f	*cpu_ticks = tc_cpu_ticks;
2014
2015static int vdso_th_enable = 1;
2016static int
2017sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
2018{
2019	int old_vdso_th_enable, error;
2020
2021	old_vdso_th_enable = vdso_th_enable;
2022	error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
2023	if (error != 0)
2024		return (error);
2025	vdso_th_enable = old_vdso_th_enable;
2026	return (0);
2027}
2028SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
2029    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2030    NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
2031
2032uint32_t
2033tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
2034{
2035	struct timehands *th;
2036	uint32_t enabled;
2037
2038	th = timehands;
2039	vdso_th->th_algo = VDSO_TH_ALGO_1;
2040	vdso_th->th_scale = th->th_scale;
2041	vdso_th->th_offset_count = th->th_offset_count;
2042	vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
2043	vdso_th->th_offset = th->th_offset;
2044	vdso_th->th_boottime = boottimebin;
2045	enabled = cpu_fill_vdso_timehands(vdso_th, th->th_counter);
2046	if (!vdso_th_enable)
2047		enabled = 0;
2048	return (enabled);
2049}
2050
2051#ifdef COMPAT_FREEBSD32
2052uint32_t
2053tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
2054{
2055	struct timehands *th;
2056	uint32_t enabled;
2057
2058	th = timehands;
2059	vdso_th32->th_algo = VDSO_TH_ALGO_1;
2060	*(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
2061	vdso_th32->th_offset_count = th->th_offset_count;
2062	vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
2063	vdso_th32->th_offset.sec = th->th_offset.sec;
2064	*(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
2065	vdso_th32->th_boottime.sec = boottimebin.sec;
2066	*(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
2067	enabled = cpu_fill_vdso_timehands32(vdso_th32, th->th_counter);
2068	if (!vdso_th_enable)
2069		enabled = 0;
2070	return (enabled);
2071}
2072#endif
2073