xref: /freebsd/sys/kern/kern_tc.c (revision af93fea710385b2b11f0cabd377e7ed6f3d97c34)
139acc78aSPoul-Henning Kamp /*-
264de3fddSPedro F. Giffuni  * SPDX-License-Identifier: Beerware
364de3fddSPedro F. Giffuni  *
491266b96SPoul-Henning Kamp  * ----------------------------------------------------------------------------
591266b96SPoul-Henning Kamp  * "THE BEER-WARE LICENSE" (Revision 42):
691266b96SPoul-Henning Kamp  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
791266b96SPoul-Henning Kamp  * can do whatever you want with this stuff. If we meet some day, and you think
891266b96SPoul-Henning Kamp  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
991266b96SPoul-Henning Kamp  * ----------------------------------------------------------------------------
10b0fdc837SLawrence Stewart  *
1116808549SKonstantin Belousov  * Copyright (c) 2011, 2015, 2016 The FreeBSD Foundation
12b0fdc837SLawrence Stewart  *
13b0fdc837SLawrence Stewart  * Portions of this software were developed by Julien Ridoux at the University
14b0fdc837SLawrence Stewart  * of Melbourne under sponsorship from the FreeBSD Foundation.
1516808549SKonstantin Belousov  *
1616808549SKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
1716808549SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
18df8bae1dSRodney W. Grimes  */
19df8bae1dSRodney W. Grimes 
20677b542eSDavid E. O'Brien #include <sys/cdefs.h>
2132c20357SPoul-Henning Kamp #include "opt_ntp.h"
22b0fdc837SLawrence Stewart #include "opt_ffclock.h"
2332c20357SPoul-Henning Kamp 
24df8bae1dSRodney W. Grimes #include <sys/param.h>
2591266b96SPoul-Henning Kamp #include <sys/kernel.h>
265b999a6bSDavide Italiano #include <sys/limits.h>
27b0fdc837SLawrence Stewart #include <sys/lock.h>
28b0fdc837SLawrence Stewart #include <sys/mutex.h>
299dbdf2a1SEric van Gyzen #include <sys/proc.h>
3091d9eda2SIan Lepore #include <sys/sbuf.h>
319dbdf2a1SEric van Gyzen #include <sys/sleepqueue.h>
3291266b96SPoul-Henning Kamp #include <sys/sysctl.h>
334e74721cSPoul-Henning Kamp #include <sys/syslog.h>
3491266b96SPoul-Henning Kamp #include <sys/systm.h>
35b0fdc837SLawrence Stewart #include <sys/timeffc.h>
3632c20357SPoul-Henning Kamp #include <sys/timepps.h>
37*af93fea7SJake Freeland #include <sys/timerfd.h>
3848e5da55SPoul-Henning Kamp #include <sys/timetc.h>
3939acc78aSPoul-Henning Kamp #include <sys/timex.h>
40aea81038SKonstantin Belousov #include <sys/vdso.h>
4139acc78aSPoul-Henning Kamp 
423bac064fSPoul-Henning Kamp /*
43c1cccd1eSWarner Losh  * A large step happens on boot.  This constant detects such steps.
44c1cccd1eSWarner Losh  * It is relatively small so that ntp_update_second gets called enough
45c1cccd1eSWarner Losh  * in the typical 'missed a couple of seconds' case, but doesn't loop
46c1cccd1eSWarner Losh  * forever when the time step is large.
474f2073fbSWarner Losh  */
484f2073fbSWarner Losh #define LARGE_STEP	200
494f2073fbSWarner Losh 
504f2073fbSWarner Losh /*
5162efba6aSPoul-Henning Kamp  * Implement a dummy timecounter which we can use until we get a real one
5262efba6aSPoul-Henning Kamp  * in the air.  This allows the console and other early stuff to use
5362efba6aSPoul-Henning Kamp  * time services.
543bac064fSPoul-Henning Kamp  */
553bac064fSPoul-Henning Kamp 
566b00cf46SPoul-Henning Kamp static u_int
dummy_get_timecount(struct timecounter * tc)5762efba6aSPoul-Henning Kamp dummy_get_timecount(struct timecounter *tc)
5862efba6aSPoul-Henning Kamp {
596b00cf46SPoul-Henning Kamp 	static u_int now;
6062efba6aSPoul-Henning Kamp 
6162efba6aSPoul-Henning Kamp 	return (++now);
6262efba6aSPoul-Henning Kamp }
6362efba6aSPoul-Henning Kamp 
6462efba6aSPoul-Henning Kamp static struct timecounter dummy_timecounter = {
6578a49a45SPoul-Henning Kamp 	dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
6662efba6aSPoul-Henning Kamp };
6762efba6aSPoul-Henning Kamp 
6862efba6aSPoul-Henning Kamp struct timehands {
6962efba6aSPoul-Henning Kamp 	/* These fields must be initialized by the driver. */
706b00cf46SPoul-Henning Kamp 	struct timecounter	*th_counter;
716b00cf46SPoul-Henning Kamp 	int64_t			th_adjustment;
7260ae52f7SEd Schouten 	uint64_t		th_scale;
736cf2362eSKonstantin Belousov 	u_int			th_large_delta;
746b00cf46SPoul-Henning Kamp 	u_int	 		th_offset_count;
756b00cf46SPoul-Henning Kamp 	struct bintime		th_offset;
7650c22263SKonstantin Belousov 	struct bintime		th_bintime;
776b00cf46SPoul-Henning Kamp 	struct timeval		th_microtime;
786b00cf46SPoul-Henning Kamp 	struct timespec		th_nanotime;
795760b029SKonstantin Belousov 	struct bintime		th_boottime;
8039acc78aSPoul-Henning Kamp 	/* Fields not to be copied in tc_windup start with th_generation. */
812c6946dcSKonstantin Belousov 	u_int			th_generation;
826b00cf46SPoul-Henning Kamp 	struct timehands	*th_next;
8362efba6aSPoul-Henning Kamp };
8462efba6aSPoul-Henning Kamp 
854b23dec4SKonstantin Belousov static struct timehands ths[16] = {
864b23dec4SKonstantin Belousov     [0] =  {
87a83c016fSKonstantin Belousov 	.th_counter = &dummy_timecounter,
88a83c016fSKonstantin Belousov 	.th_scale = (uint64_t)-1 / 1000000,
896cf2362eSKonstantin Belousov 	.th_large_delta = 1000000,
90a83c016fSKonstantin Belousov 	.th_offset = { .sec = 1 },
91a83c016fSKonstantin Belousov 	.th_generation = 1,
924b23dec4SKonstantin Belousov     },
93f5d157fbSPoul-Henning Kamp };
9462efba6aSPoul-Henning Kamp 
954b23dec4SKonstantin Belousov static struct timehands *volatile timehands = &ths[0];
9662efba6aSPoul-Henning Kamp struct timecounter *timecounter = &dummy_timecounter;
9762efba6aSPoul-Henning Kamp static struct timecounter *timecounters = &dummy_timecounter;
983bac064fSPoul-Henning Kamp 
99621fd9dcSMark Johnston /* Mutex to protect the timecounter list. */
100621fd9dcSMark Johnston static struct mtx tc_lock;
101621fd9dcSMark Johnston 
1020e189873SAlexander Motin int tc_min_ticktock_freq = 1;
1030e189873SAlexander Motin 
104a8df530dSJohn Baldwin volatile time_t time_second = 1;
105a8df530dSJohn Baldwin volatile time_t time_uptime = 1;
106227ee8a1SPoul-Henning Kamp 
107a512d0abSWarner Losh /*
108a512d0abSWarner Losh  * The system time is always computed by summing the estimated boot time and the
109a512d0abSWarner Losh  * system uptime. The timehands track boot time, but it changes when the system
110a512d0abSWarner Losh  * time is set by the user, stepped by ntpd or adjusted when resuming. It
111a512d0abSWarner Losh  * is set to new_time - uptime.
112a512d0abSWarner Losh  */
113a7bc3102SPeter Wemm static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
1147029da5cSPawel Biernacki SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime,
1157029da5cSPawel Biernacki     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1167029da5cSPawel Biernacki     sysctl_kern_boottime, "S,timeval",
117a512d0abSWarner Losh     "Estimated system boottime");
11837d38777SBruce Evans 
1197029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1207029da5cSPawel Biernacki     "");
1217029da5cSPawel Biernacki static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc,
1227029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1237029da5cSPawel Biernacki     "");
12491266b96SPoul-Henning Kamp 
1254e74721cSPoul-Henning Kamp static int timestepwarnings;
126fa9da1f5SMark Johnston SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RWTUN,
1272baa5cddSRebecca Cran     &timestepwarnings, 0, "Log time steps");
1284e74721cSPoul-Henning Kamp 
1294b23dec4SKonstantin Belousov static int timehands_count = 2;
1306c46ce7eSKonstantin Belousov SYSCTL_INT(_kern_timecounter, OID_AUTO, timehands_count,
1316c46ce7eSKonstantin Belousov     CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
1324b23dec4SKonstantin Belousov     &timehands_count, 0, "Count of timehands in rotation");
1334b23dec4SKonstantin Belousov 
1345b999a6bSDavide Italiano struct bintime bt_timethreshold;
1355b999a6bSDavide Italiano struct bintime bt_tickthreshold;
1365b999a6bSDavide Italiano sbintime_t sbt_timethreshold;
1375b999a6bSDavide Italiano sbintime_t sbt_tickthreshold;
1385b999a6bSDavide Italiano struct bintime tc_tick_bt;
1395b999a6bSDavide Italiano sbintime_t tc_tick_sbt;
1405b999a6bSDavide Italiano int tc_precexp;
1415b999a6bSDavide Italiano int tc_timepercentage = TC_DEFAULTPERC;
1425b999a6bSDavide Italiano static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS);
1435b999a6bSDavide Italiano SYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation,
144af3b2549SHans Petter Selasky     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
1455b999a6bSDavide Italiano     sysctl_kern_timecounter_adjprecision, "I",
1465b999a6bSDavide Italiano     "Allowed time interval deviation in percents");
1475b999a6bSDavide Italiano 
1489dbdf2a1SEric van Gyzen volatile int rtc_generation = 1;
1499dbdf2a1SEric van Gyzen 
150e8bac3f2SIan Lepore static int tc_chosen;	/* Non-zero if a specific tc was chosen via sysctl. */
15156b9bee6SKonstantin Belousov static char tc_from_tunable[16];
152e8bac3f2SIan Lepore 
1535760b029SKonstantin Belousov static void tc_windup(struct bintime *new_boottimebin);
154e8444a7eSPoul-Henning Kamp static void cpu_tick_calibrate(int);
1559e1b5510SPoul-Henning Kamp 
15657d025c3SGeorge V. Neville-Neil void dtrace_getnanotime(struct timespec *tsp);
15730b68ecdSRobert Watson void dtrace_getnanouptime(struct timespec *tsp);
15857d025c3SGeorge V. Neville-Neil 
159a7bc3102SPeter Wemm static int
sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)160a7bc3102SPeter Wemm sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
161a7bc3102SPeter Wemm {
162584b675eSKonstantin Belousov 	struct timeval boottime;
163584b675eSKonstantin Belousov 
164584b675eSKonstantin Belousov 	getboottime(&boottime);
165584b675eSKonstantin Belousov 
1667045ac43SOlivier Houchard /* i386 is the only arch which uses a 32bits time_t */
1677045ac43SOlivier Houchard #ifdef __amd64__
168a7bc3102SPeter Wemm #ifdef SCTL_MASK32
169a7bc3102SPeter Wemm 	int tv[2];
170a7bc3102SPeter Wemm 
171a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
172a7bc3102SPeter Wemm 		tv[0] = boottime.tv_sec;
173a7bc3102SPeter Wemm 		tv[1] = boottime.tv_usec;
174584b675eSKonstantin Belousov 		return (SYSCTL_OUT(req, tv, sizeof(tv)));
175584b675eSKonstantin Belousov 	}
176a7bc3102SPeter Wemm #endif
1779624d947SJuli Mallett #endif
178584b675eSKonstantin Belousov 	return (SYSCTL_OUT(req, &boottime, sizeof(boottime)));
179a7bc3102SPeter Wemm }
1805b1a8eb3SPoul-Henning Kamp 
18193ef14a7SDavid Malone static int
sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)18293ef14a7SDavid Malone sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
18393ef14a7SDavid Malone {
18493ef14a7SDavid Malone 	u_int ncount;
18593ef14a7SDavid Malone 	struct timecounter *tc = arg1;
18693ef14a7SDavid Malone 
18793ef14a7SDavid Malone 	ncount = tc->tc_get_timecount(tc);
1884d29106eSKonstantin Belousov 	return (sysctl_handle_int(oidp, &ncount, 0, req));
18993ef14a7SDavid Malone }
19093ef14a7SDavid Malone 
19193ef14a7SDavid Malone static int
sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)19293ef14a7SDavid Malone sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
19393ef14a7SDavid Malone {
19460ae52f7SEd Schouten 	uint64_t freq;
19593ef14a7SDavid Malone 	struct timecounter *tc = arg1;
19693ef14a7SDavid Malone 
19793ef14a7SDavid Malone 	freq = tc->tc_frequency;
1984d29106eSKonstantin Belousov 	return (sysctl_handle_64(oidp, &freq, 0, req));
19993ef14a7SDavid Malone }
20093ef14a7SDavid Malone 
20139acc78aSPoul-Henning Kamp /*
20239acc78aSPoul-Henning Kamp  * Return the difference between the timehands' counter value now and what
20339acc78aSPoul-Henning Kamp  * was when we copied it to the timehands' offset_count.
20439acc78aSPoul-Henning Kamp  */
2056b00cf46SPoul-Henning Kamp static __inline u_int
tc_delta(struct timehands * th)2066b00cf46SPoul-Henning Kamp tc_delta(struct timehands *th)
207e796e00dSPoul-Henning Kamp {
2086b00cf46SPoul-Henning Kamp 	struct timecounter *tc;
209e796e00dSPoul-Henning Kamp 
2106b00cf46SPoul-Henning Kamp 	tc = th->th_counter;
2116b00cf46SPoul-Henning Kamp 	return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
2126b00cf46SPoul-Henning Kamp 	    tc->tc_counter_mask);
213e796e00dSPoul-Henning Kamp }
214a0502b19SPoul-Henning Kamp 
2153d9d64aaSAndriy Gapon static __inline void
bintime_add_tc_delta(struct bintime * bt,uint64_t scale,uint64_t large_delta,uint64_t delta)2163d9d64aaSAndriy Gapon bintime_add_tc_delta(struct bintime *bt, uint64_t scale,
2173d9d64aaSAndriy Gapon     uint64_t large_delta, uint64_t delta)
2183d9d64aaSAndriy Gapon {
2193d9d64aaSAndriy Gapon 	uint64_t x;
2203d9d64aaSAndriy Gapon 
2213d9d64aaSAndriy Gapon 	if (__predict_false(delta >= large_delta)) {
2223d9d64aaSAndriy Gapon 		/* Avoid overflow for scale * delta. */
2233d9d64aaSAndriy Gapon 		x = (scale >> 32) * delta;
2243d9d64aaSAndriy Gapon 		bt->sec += x >> 32;
2253d9d64aaSAndriy Gapon 		bintime_addx(bt, x << 32);
2263d9d64aaSAndriy Gapon 		bintime_addx(bt, (scale & 0xffffffff) * delta);
2273d9d64aaSAndriy Gapon 	} else {
2283d9d64aaSAndriy Gapon 		bintime_addx(bt, scale * delta);
2293d9d64aaSAndriy Gapon 	}
2303d9d64aaSAndriy Gapon }
2313d9d64aaSAndriy Gapon 
23239acc78aSPoul-Henning Kamp /*
2336b00cf46SPoul-Henning Kamp  * Functions for reading the time.  We have to loop until we are sure that
23439acc78aSPoul-Henning Kamp  * the timehands that we operated on was not updated under our feet.  See
23539acc78aSPoul-Henning Kamp  * the comment in <sys/time.h> for a description of these 12 functions.
2366b00cf46SPoul-Henning Kamp  */
2376b00cf46SPoul-Henning Kamp 
2386cf2362eSKonstantin Belousov static __inline void
bintime_off(struct bintime * bt,u_int off)2396cf2362eSKonstantin Belousov bintime_off(struct bintime *bt, u_int off)
2409bce0f05SLawrence Stewart {
2419bce0f05SLawrence Stewart 	struct timehands *th;
2426cf2362eSKonstantin Belousov 	struct bintime *btp;
2433d9d64aaSAndriy Gapon 	uint64_t scale;
2446cf2362eSKonstantin Belousov 	u_int delta, gen, large_delta;
2459bce0f05SLawrence Stewart 
2469bce0f05SLawrence Stewart 	do {
2479bce0f05SLawrence Stewart 		th = timehands;
248f4b5a972SKonstantin Belousov 		gen = atomic_load_acq_int(&th->th_generation);
2496cf2362eSKonstantin Belousov 		btp = (struct bintime *)((vm_offset_t)th + off);
2506cf2362eSKonstantin Belousov 		*bt = *btp;
2516cf2362eSKonstantin Belousov 		scale = th->th_scale;
2526cf2362eSKonstantin Belousov 		delta = tc_delta(th);
2536cf2362eSKonstantin Belousov 		large_delta = th->th_large_delta;
254f4b5a972SKonstantin Belousov 		atomic_thread_fence_acq();
255f4b5a972SKonstantin Belousov 	} while (gen == 0 || gen != th->th_generation);
2566cf2362eSKonstantin Belousov 
2573d9d64aaSAndriy Gapon 	bintime_add_tc_delta(bt, scale, large_delta, delta);
2586cf2362eSKonstantin Belousov }
2596cf2362eSKonstantin Belousov #define	GETTHBINTIME(dst, member)					\
2606cf2362eSKonstantin Belousov do {									\
2616cf2362eSKonstantin Belousov 	_Static_assert(_Generic(((struct timehands *)NULL)->member,	\
2626cf2362eSKonstantin Belousov 	    struct bintime: 1, default: 0) == 1,			\
2636cf2362eSKonstantin Belousov 	    "struct timehands member is not of struct bintime type");	\
2646cf2362eSKonstantin Belousov 	bintime_off(dst, __offsetof(struct timehands, member));		\
2656cf2362eSKonstantin Belousov } while (0)
2666cf2362eSKonstantin Belousov 
2676cf2362eSKonstantin Belousov static __inline void
getthmember(void * out,size_t out_size,u_int off)2686cf2362eSKonstantin Belousov getthmember(void *out, size_t out_size, u_int off)
2696cf2362eSKonstantin Belousov {
2706cf2362eSKonstantin Belousov 	struct timehands *th;
2716cf2362eSKonstantin Belousov 	u_int gen;
2726cf2362eSKonstantin Belousov 
2736cf2362eSKonstantin Belousov 	do {
2746cf2362eSKonstantin Belousov 		th = timehands;
2756cf2362eSKonstantin Belousov 		gen = atomic_load_acq_int(&th->th_generation);
2766cf2362eSKonstantin Belousov 		memcpy(out, (char *)th + off, out_size);
2776cf2362eSKonstantin Belousov 		atomic_thread_fence_acq();
2786cf2362eSKonstantin Belousov 	} while (gen == 0 || gen != th->th_generation);
2796cf2362eSKonstantin Belousov }
2806cf2362eSKonstantin Belousov #define	GETTHMEMBER(dst, member)					\
2816cf2362eSKonstantin Belousov do {									\
2826cf2362eSKonstantin Belousov 	_Static_assert(_Generic(*dst,					\
2836cf2362eSKonstantin Belousov 	    __typeof(((struct timehands *)NULL)->member): 1,		\
2846cf2362eSKonstantin Belousov 	    default: 0) == 1,						\
2856cf2362eSKonstantin Belousov 	    "*dst and struct timehands member have different types");	\
2866cf2362eSKonstantin Belousov 	getthmember(dst, sizeof(*dst), __offsetof(struct timehands,	\
2876cf2362eSKonstantin Belousov 	    member));							\
2886cf2362eSKonstantin Belousov } while (0)
2896cf2362eSKonstantin Belousov 
2906cf2362eSKonstantin Belousov #ifdef FFCLOCK
2916cf2362eSKonstantin Belousov void
fbclock_binuptime(struct bintime * bt)2926cf2362eSKonstantin Belousov fbclock_binuptime(struct bintime *bt)
2936cf2362eSKonstantin Belousov {
2946cf2362eSKonstantin Belousov 
2956cf2362eSKonstantin Belousov 	GETTHBINTIME(bt, th_offset);
2969bce0f05SLawrence Stewart }
2979bce0f05SLawrence Stewart 
298e977bac3SLawrence Stewart void
fbclock_nanouptime(struct timespec * tsp)2999bce0f05SLawrence Stewart fbclock_nanouptime(struct timespec *tsp)
3009bce0f05SLawrence Stewart {
3019bce0f05SLawrence Stewart 	struct bintime bt;
3029bce0f05SLawrence Stewart 
303c2a4ee99SLawrence Stewart 	fbclock_binuptime(&bt);
3049bce0f05SLawrence Stewart 	bintime2timespec(&bt, tsp);
3059bce0f05SLawrence Stewart }
3069bce0f05SLawrence Stewart 
307e977bac3SLawrence Stewart void
fbclock_microuptime(struct timeval * tvp)3089bce0f05SLawrence Stewart fbclock_microuptime(struct timeval *tvp)
3099bce0f05SLawrence Stewart {
3109bce0f05SLawrence Stewart 	struct bintime bt;
3119bce0f05SLawrence Stewart 
312c2a4ee99SLawrence Stewart 	fbclock_binuptime(&bt);
3139bce0f05SLawrence Stewart 	bintime2timeval(&bt, tvp);
3149bce0f05SLawrence Stewart }
3159bce0f05SLawrence Stewart 
316e977bac3SLawrence Stewart void
fbclock_bintime(struct bintime * bt)3179bce0f05SLawrence Stewart fbclock_bintime(struct bintime *bt)
3189bce0f05SLawrence Stewart {
3199bce0f05SLawrence Stewart 
3206cf2362eSKonstantin Belousov 	GETTHBINTIME(bt, th_bintime);
3219bce0f05SLawrence Stewart }
3229bce0f05SLawrence Stewart 
323e977bac3SLawrence Stewart void
fbclock_nanotime(struct timespec * tsp)3249bce0f05SLawrence Stewart fbclock_nanotime(struct timespec *tsp)
3259bce0f05SLawrence Stewart {
3269bce0f05SLawrence Stewart 	struct bintime bt;
3279bce0f05SLawrence Stewart 
328c2a4ee99SLawrence Stewart 	fbclock_bintime(&bt);
3299bce0f05SLawrence Stewart 	bintime2timespec(&bt, tsp);
3309bce0f05SLawrence Stewart }
3319bce0f05SLawrence Stewart 
332e977bac3SLawrence Stewart void
fbclock_microtime(struct timeval * tvp)3339bce0f05SLawrence Stewart fbclock_microtime(struct timeval *tvp)
3349bce0f05SLawrence Stewart {
3359bce0f05SLawrence Stewart 	struct bintime bt;
3369bce0f05SLawrence Stewart 
337c2a4ee99SLawrence Stewart 	fbclock_bintime(&bt);
3389bce0f05SLawrence Stewart 	bintime2timeval(&bt, tvp);
3399bce0f05SLawrence Stewart }
3409bce0f05SLawrence Stewart 
341e977bac3SLawrence Stewart void
fbclock_getbinuptime(struct bintime * bt)3429bce0f05SLawrence Stewart fbclock_getbinuptime(struct bintime *bt)
3439bce0f05SLawrence Stewart {
3449bce0f05SLawrence Stewart 
3456cf2362eSKonstantin Belousov 	GETTHMEMBER(bt, th_offset);
3469bce0f05SLawrence Stewart }
3479bce0f05SLawrence Stewart 
348e977bac3SLawrence Stewart void
fbclock_getnanouptime(struct timespec * tsp)3499bce0f05SLawrence Stewart fbclock_getnanouptime(struct timespec *tsp)
3509bce0f05SLawrence Stewart {
3516cf2362eSKonstantin Belousov 	struct bintime bt;
3529bce0f05SLawrence Stewart 
3536cf2362eSKonstantin Belousov 	GETTHMEMBER(&bt, th_offset);
3546cf2362eSKonstantin Belousov 	bintime2timespec(&bt, tsp);
3559bce0f05SLawrence Stewart }
3569bce0f05SLawrence Stewart 
357e977bac3SLawrence Stewart void
fbclock_getmicrouptime(struct timeval * tvp)3589bce0f05SLawrence Stewart fbclock_getmicrouptime(struct timeval *tvp)
3599bce0f05SLawrence Stewart {
3606cf2362eSKonstantin Belousov 	struct bintime bt;
3619bce0f05SLawrence Stewart 
3626cf2362eSKonstantin Belousov 	GETTHMEMBER(&bt, th_offset);
3636cf2362eSKonstantin Belousov 	bintime2timeval(&bt, tvp);
3649bce0f05SLawrence Stewart }
3659bce0f05SLawrence Stewart 
366e977bac3SLawrence Stewart void
fbclock_getbintime(struct bintime * bt)3679bce0f05SLawrence Stewart fbclock_getbintime(struct bintime *bt)
3689bce0f05SLawrence Stewart {
3699bce0f05SLawrence Stewart 
3706cf2362eSKonstantin Belousov 	GETTHMEMBER(bt, th_bintime);
3719bce0f05SLawrence Stewart }
3729bce0f05SLawrence Stewart 
373e977bac3SLawrence Stewart void
fbclock_getnanotime(struct timespec * tsp)3749bce0f05SLawrence Stewart fbclock_getnanotime(struct timespec *tsp)
3759bce0f05SLawrence Stewart {
3769bce0f05SLawrence Stewart 
3776cf2362eSKonstantin Belousov 	GETTHMEMBER(tsp, th_nanotime);
3789bce0f05SLawrence Stewart }
3799bce0f05SLawrence Stewart 
380e977bac3SLawrence Stewart void
fbclock_getmicrotime(struct timeval * tvp)3819bce0f05SLawrence Stewart fbclock_getmicrotime(struct timeval *tvp)
3829bce0f05SLawrence Stewart {
3839bce0f05SLawrence Stewart 
3846cf2362eSKonstantin Belousov 	GETTHMEMBER(tvp, th_microtime);
3859bce0f05SLawrence Stewart }
3869bce0f05SLawrence Stewart #else /* !FFCLOCK */
3876cf2362eSKonstantin Belousov 
388a0502b19SPoul-Henning Kamp void
binuptime(struct bintime * bt)3892028c0cdSPoul-Henning Kamp binuptime(struct bintime *bt)
3902028c0cdSPoul-Henning Kamp {
3912028c0cdSPoul-Henning Kamp 
3926cf2362eSKonstantin Belousov 	GETTHBINTIME(bt, th_offset);
3932028c0cdSPoul-Henning Kamp }
3942028c0cdSPoul-Henning Kamp 
3952028c0cdSPoul-Henning Kamp void
nanouptime(struct timespec * tsp)39639acc78aSPoul-Henning Kamp nanouptime(struct timespec *tsp)
397056abcabSPoul-Henning Kamp {
398056abcabSPoul-Henning Kamp 	struct bintime bt;
399056abcabSPoul-Henning Kamp 
400056abcabSPoul-Henning Kamp 	binuptime(&bt);
40139acc78aSPoul-Henning Kamp 	bintime2timespec(&bt, tsp);
402056abcabSPoul-Henning Kamp }
403056abcabSPoul-Henning Kamp 
404056abcabSPoul-Henning Kamp void
microuptime(struct timeval * tvp)40539acc78aSPoul-Henning Kamp microuptime(struct timeval *tvp)
406056abcabSPoul-Henning Kamp {
407056abcabSPoul-Henning Kamp 	struct bintime bt;
408056abcabSPoul-Henning Kamp 
409056abcabSPoul-Henning Kamp 	binuptime(&bt);
41039acc78aSPoul-Henning Kamp 	bintime2timeval(&bt, tvp);
411056abcabSPoul-Henning Kamp }
412056abcabSPoul-Henning Kamp 
413056abcabSPoul-Henning Kamp void
bintime(struct bintime * bt)4142028c0cdSPoul-Henning Kamp bintime(struct bintime *bt)
4152028c0cdSPoul-Henning Kamp {
4162028c0cdSPoul-Henning Kamp 
4176cf2362eSKonstantin Belousov 	GETTHBINTIME(bt, th_bintime);
4182028c0cdSPoul-Henning Kamp }
4192028c0cdSPoul-Henning Kamp 
4202028c0cdSPoul-Henning Kamp void
nanotime(struct timespec * tsp)42139acc78aSPoul-Henning Kamp nanotime(struct timespec *tsp)
42200af9731SPoul-Henning Kamp {
4232028c0cdSPoul-Henning Kamp 	struct bintime bt;
42400af9731SPoul-Henning Kamp 
4252028c0cdSPoul-Henning Kamp 	bintime(&bt);
42639acc78aSPoul-Henning Kamp 	bintime2timespec(&bt, tsp);
42748115288SPoul-Henning Kamp }
42848115288SPoul-Henning Kamp 
42948115288SPoul-Henning Kamp void
microtime(struct timeval * tvp)43039acc78aSPoul-Henning Kamp microtime(struct timeval *tvp)
431056abcabSPoul-Henning Kamp {
432056abcabSPoul-Henning Kamp 	struct bintime bt;
433056abcabSPoul-Henning Kamp 
434056abcabSPoul-Henning Kamp 	bintime(&bt);
43539acc78aSPoul-Henning Kamp 	bintime2timeval(&bt, tvp);
436056abcabSPoul-Henning Kamp }
437056abcabSPoul-Henning Kamp 
438056abcabSPoul-Henning Kamp void
getbinuptime(struct bintime * bt)439056abcabSPoul-Henning Kamp getbinuptime(struct bintime *bt)
44000af9731SPoul-Henning Kamp {
44100af9731SPoul-Henning Kamp 
4426cf2362eSKonstantin Belousov 	GETTHMEMBER(bt, th_offset);
443a0502b19SPoul-Henning Kamp }
444a0502b19SPoul-Henning Kamp 
445a0502b19SPoul-Henning Kamp void
getnanouptime(struct timespec * tsp)446c21410e1SPoul-Henning Kamp getnanouptime(struct timespec *tsp)
447a0502b19SPoul-Henning Kamp {
4486cf2362eSKonstantin Belousov 	struct bintime bt;
449a0502b19SPoul-Henning Kamp 
4506cf2362eSKonstantin Belousov 	GETTHMEMBER(&bt, th_offset);
4516cf2362eSKonstantin Belousov 	bintime2timespec(&bt, tsp);
452a0502b19SPoul-Henning Kamp }
453a0502b19SPoul-Henning Kamp 
454c7c9a816SPoul-Henning Kamp void
getmicrouptime(struct timeval * tvp)455056abcabSPoul-Henning Kamp getmicrouptime(struct timeval *tvp)
456c7c9a816SPoul-Henning Kamp {
4576cf2362eSKonstantin Belousov 	struct bintime bt;
4587ec73f64SPoul-Henning Kamp 
4596cf2362eSKonstantin Belousov 	GETTHMEMBER(&bt, th_offset);
4606cf2362eSKonstantin Belousov 	bintime2timeval(&bt, tvp);
4617ec73f64SPoul-Henning Kamp }
4627ec73f64SPoul-Henning Kamp 
4637ec73f64SPoul-Henning Kamp void
getbintime(struct bintime * bt)464056abcabSPoul-Henning Kamp getbintime(struct bintime *bt)
4657ec73f64SPoul-Henning Kamp {
4667ec73f64SPoul-Henning Kamp 
4676cf2362eSKonstantin Belousov 	GETTHMEMBER(bt, th_bintime);
468056abcabSPoul-Henning Kamp }
469056abcabSPoul-Henning Kamp 
470056abcabSPoul-Henning Kamp void
getnanotime(struct timespec * tsp)471056abcabSPoul-Henning Kamp getnanotime(struct timespec *tsp)
472056abcabSPoul-Henning Kamp {
473056abcabSPoul-Henning Kamp 
4746cf2362eSKonstantin Belousov 	GETTHMEMBER(tsp, th_nanotime);
475056abcabSPoul-Henning Kamp }
476056abcabSPoul-Henning Kamp 
477056abcabSPoul-Henning Kamp void
getmicrotime(struct timeval * tvp)478056abcabSPoul-Henning Kamp getmicrotime(struct timeval *tvp)
479056abcabSPoul-Henning Kamp {
480056abcabSPoul-Henning Kamp 
4816cf2362eSKonstantin Belousov 	GETTHMEMBER(tvp, th_microtime);
4827ec73f64SPoul-Henning Kamp }
4839bce0f05SLawrence Stewart #endif /* FFCLOCK */
4847ec73f64SPoul-Henning Kamp 
485584b675eSKonstantin Belousov void
getboottime(struct timeval * boottime)486584b675eSKonstantin Belousov getboottime(struct timeval *boottime)
487584b675eSKonstantin Belousov {
4885760b029SKonstantin Belousov 	struct bintime boottimebin;
489584b675eSKonstantin Belousov 
4905760b029SKonstantin Belousov 	getboottimebin(&boottimebin);
4915760b029SKonstantin Belousov 	bintime2timeval(&boottimebin, boottime);
492584b675eSKonstantin Belousov }
493584b675eSKonstantin Belousov 
494584b675eSKonstantin Belousov void
getboottimebin(struct bintime * boottimebin)495584b675eSKonstantin Belousov getboottimebin(struct bintime *boottimebin)
496584b675eSKonstantin Belousov {
497584b675eSKonstantin Belousov 
4986cf2362eSKonstantin Belousov 	GETTHMEMBER(boottimebin, th_boottime);
499584b675eSKonstantin Belousov }
500584b675eSKonstantin Belousov 
501b0fdc837SLawrence Stewart #ifdef FFCLOCK
502b0fdc837SLawrence Stewart /*
503b0fdc837SLawrence Stewart  * Support for feed-forward synchronization algorithms. This is heavily inspired
504b0fdc837SLawrence Stewart  * by the timehands mechanism but kept independent from it. *_windup() functions
505b0fdc837SLawrence Stewart  * have some connection to avoid accessing the timecounter hardware more than
506b0fdc837SLawrence Stewart  * necessary.
507b0fdc837SLawrence Stewart  */
508b0fdc837SLawrence Stewart 
509b0fdc837SLawrence Stewart /* Feed-forward clock estimates kept updated by the synchronization daemon. */
510b0fdc837SLawrence Stewart struct ffclock_estimate ffclock_estimate;
511b0fdc837SLawrence Stewart struct bintime ffclock_boottime;	/* Feed-forward boot time estimate. */
512b0fdc837SLawrence Stewart uint32_t ffclock_status;		/* Feed-forward clock status. */
513b0fdc837SLawrence Stewart int8_t ffclock_updated;			/* New estimates are available. */
514b0fdc837SLawrence Stewart struct mtx ffclock_mtx;			/* Mutex on ffclock_estimate. */
515b0fdc837SLawrence Stewart 
516b0fdc837SLawrence Stewart struct fftimehands {
517b0fdc837SLawrence Stewart 	struct ffclock_estimate	cest;
518b0fdc837SLawrence Stewart 	struct bintime		tick_time;
519b0fdc837SLawrence Stewart 	struct bintime		tick_time_lerp;
520b0fdc837SLawrence Stewart 	ffcounter		tick_ffcount;
521b0fdc837SLawrence Stewart 	uint64_t		period_lerp;
522b0fdc837SLawrence Stewart 	volatile uint8_t	gen;
523b0fdc837SLawrence Stewart 	struct fftimehands	*next;
524b0fdc837SLawrence Stewart };
525b0fdc837SLawrence Stewart 
526b0fdc837SLawrence Stewart #define	NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
527b0fdc837SLawrence Stewart 
528b0fdc837SLawrence Stewart static struct fftimehands ffth[10];
529b0fdc837SLawrence Stewart static struct fftimehands *volatile fftimehands = ffth;
530b0fdc837SLawrence Stewart 
531b0fdc837SLawrence Stewart static void
ffclock_init(void)532b0fdc837SLawrence Stewart ffclock_init(void)
533b0fdc837SLawrence Stewart {
534b0fdc837SLawrence Stewart 	struct fftimehands *cur;
535b0fdc837SLawrence Stewart 	struct fftimehands *last;
536b0fdc837SLawrence Stewart 
537b0fdc837SLawrence Stewart 	memset(ffth, 0, sizeof(ffth));
538b0fdc837SLawrence Stewart 
539b0fdc837SLawrence Stewart 	last = ffth + NUM_ELEMENTS(ffth) - 1;
540b0fdc837SLawrence Stewart 	for (cur = ffth; cur < last; cur++)
541b0fdc837SLawrence Stewart 		cur->next = cur + 1;
542b0fdc837SLawrence Stewart 	last->next = ffth;
543b0fdc837SLawrence Stewart 
544b0fdc837SLawrence Stewart 	ffclock_updated = 0;
545b0fdc837SLawrence Stewart 	ffclock_status = FFCLOCK_STA_UNSYNC;
546b0fdc837SLawrence Stewart 	mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
547b0fdc837SLawrence Stewart }
548b0fdc837SLawrence Stewart 
549b0fdc837SLawrence Stewart /*
550b0fdc837SLawrence Stewart  * Reset the feed-forward clock estimates. Called from inittodr() to get things
551b0fdc837SLawrence Stewart  * kick started and uses the timecounter nominal frequency as a first period
552b0fdc837SLawrence Stewart  * estimate. Note: this function may be called several time just after boot.
553b0fdc837SLawrence Stewart  * Note: this is the only function that sets the value of boot time for the
554b0fdc837SLawrence Stewart  * monotonic (i.e. uptime) version of the feed-forward clock.
555b0fdc837SLawrence Stewart  */
556b0fdc837SLawrence Stewart void
ffclock_reset_clock(struct timespec * ts)557b0fdc837SLawrence Stewart ffclock_reset_clock(struct timespec *ts)
558b0fdc837SLawrence Stewart {
559b0fdc837SLawrence Stewart 	struct timecounter *tc;
560b0fdc837SLawrence Stewart 	struct ffclock_estimate cest;
561b0fdc837SLawrence Stewart 
562b0fdc837SLawrence Stewart 	tc = timehands->th_counter;
563b0fdc837SLawrence Stewart 	memset(&cest, 0, sizeof(struct ffclock_estimate));
564b0fdc837SLawrence Stewart 
565b0fdc837SLawrence Stewart 	timespec2bintime(ts, &ffclock_boottime);
566b0fdc837SLawrence Stewart 	timespec2bintime(ts, &(cest.update_time));
567b0fdc837SLawrence Stewart 	ffclock_read_counter(&cest.update_ffcount);
568b0fdc837SLawrence Stewart 	cest.leapsec_next = 0;
569b0fdc837SLawrence Stewart 	cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
570b0fdc837SLawrence Stewart 	cest.errb_abs = 0;
571b0fdc837SLawrence Stewart 	cest.errb_rate = 0;
572b0fdc837SLawrence Stewart 	cest.status = FFCLOCK_STA_UNSYNC;
573b0fdc837SLawrence Stewart 	cest.leapsec_total = 0;
574b0fdc837SLawrence Stewart 	cest.leapsec = 0;
575b0fdc837SLawrence Stewart 
576b0fdc837SLawrence Stewart 	mtx_lock(&ffclock_mtx);
577b0fdc837SLawrence Stewart 	bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
578b0fdc837SLawrence Stewart 	ffclock_updated = INT8_MAX;
579b0fdc837SLawrence Stewart 	mtx_unlock(&ffclock_mtx);
580b0fdc837SLawrence Stewart 
581b0fdc837SLawrence Stewart 	printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
582b0fdc837SLawrence Stewart 	    (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
583b0fdc837SLawrence Stewart 	    (unsigned long)ts->tv_nsec);
584b0fdc837SLawrence Stewart }
585b0fdc837SLawrence Stewart 
586b0fdc837SLawrence Stewart /*
587b0fdc837SLawrence Stewart  * Sub-routine to convert a time interval measured in RAW counter units to time
588b0fdc837SLawrence Stewart  * in seconds stored in bintime format.
589b0fdc837SLawrence Stewart  * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
590b0fdc837SLawrence Stewart  * larger than the max value of u_int (on 32 bit architecture). Loop to consume
591b0fdc837SLawrence Stewart  * extra cycles.
592b0fdc837SLawrence Stewart  */
593b0fdc837SLawrence Stewart static void
ffclock_convert_delta(ffcounter ffdelta,uint64_t period,struct bintime * bt)594b0fdc837SLawrence Stewart ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
595b0fdc837SLawrence Stewart {
596b0fdc837SLawrence Stewart 	struct bintime bt2;
597b0fdc837SLawrence Stewart 	ffcounter delta, delta_max;
598b0fdc837SLawrence Stewart 
599b0fdc837SLawrence Stewart 	delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
600b0fdc837SLawrence Stewart 	bintime_clear(bt);
601b0fdc837SLawrence Stewart 	do {
602b0fdc837SLawrence Stewart 		if (ffdelta > delta_max)
603b0fdc837SLawrence Stewart 			delta = delta_max;
604b0fdc837SLawrence Stewart 		else
605b0fdc837SLawrence Stewart 			delta = ffdelta;
606b0fdc837SLawrence Stewart 		bt2.sec = 0;
607b0fdc837SLawrence Stewart 		bt2.frac = period;
608b0fdc837SLawrence Stewart 		bintime_mul(&bt2, (unsigned int)delta);
609b0fdc837SLawrence Stewart 		bintime_add(bt, &bt2);
610b0fdc837SLawrence Stewart 		ffdelta -= delta;
611b0fdc837SLawrence Stewart 	} while (ffdelta > 0);
612b0fdc837SLawrence Stewart }
613b0fdc837SLawrence Stewart 
614b0fdc837SLawrence Stewart /*
615b0fdc837SLawrence Stewart  * Update the fftimehands.
616b0fdc837SLawrence Stewart  * Push the tick ffcount and time(s) forward based on current clock estimate.
617b0fdc837SLawrence Stewart  * The conversion from ffcounter to bintime relies on the difference clock
618b0fdc837SLawrence Stewart  * principle, whose accuracy relies on computing small time intervals. If a new
619b0fdc837SLawrence Stewart  * clock estimate has been passed by the synchronisation daemon, make it
620b0fdc837SLawrence Stewart  * current, and compute the linear interpolation for monotonic time if needed.
621b0fdc837SLawrence Stewart  */
622b0fdc837SLawrence Stewart static void
ffclock_windup(unsigned int delta)623b0fdc837SLawrence Stewart ffclock_windup(unsigned int delta)
624b0fdc837SLawrence Stewart {
625b0fdc837SLawrence Stewart 	struct ffclock_estimate *cest;
626b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
627b0fdc837SLawrence Stewart 	struct bintime bt, gap_lerp;
628b0fdc837SLawrence Stewart 	ffcounter ffdelta;
629b0fdc837SLawrence Stewart 	uint64_t frac;
630b0fdc837SLawrence Stewart 	unsigned int polling;
631b0fdc837SLawrence Stewart 	uint8_t forward_jump, ogen;
632b0fdc837SLawrence Stewart 
633b0fdc837SLawrence Stewart 	/*
634b0fdc837SLawrence Stewart 	 * Pick the next timehand, copy current ffclock estimates and move tick
635b0fdc837SLawrence Stewart 	 * times and counter forward.
636b0fdc837SLawrence Stewart 	 */
637b0fdc837SLawrence Stewart 	forward_jump = 0;
638b0fdc837SLawrence Stewart 	ffth = fftimehands->next;
639b0fdc837SLawrence Stewart 	ogen = ffth->gen;
640b0fdc837SLawrence Stewart 	ffth->gen = 0;
641b0fdc837SLawrence Stewart 	cest = &ffth->cest;
642b0fdc837SLawrence Stewart 	bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
643b0fdc837SLawrence Stewart 	ffdelta = (ffcounter)delta;
644b0fdc837SLawrence Stewart 	ffth->period_lerp = fftimehands->period_lerp;
645b0fdc837SLawrence Stewart 
646b0fdc837SLawrence Stewart 	ffth->tick_time = fftimehands->tick_time;
647b0fdc837SLawrence Stewart 	ffclock_convert_delta(ffdelta, cest->period, &bt);
648b0fdc837SLawrence Stewart 	bintime_add(&ffth->tick_time, &bt);
649b0fdc837SLawrence Stewart 
650b0fdc837SLawrence Stewart 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
651b0fdc837SLawrence Stewart 	ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
652b0fdc837SLawrence Stewart 	bintime_add(&ffth->tick_time_lerp, &bt);
653b0fdc837SLawrence Stewart 
654b0fdc837SLawrence Stewart 	ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
655b0fdc837SLawrence Stewart 
656b0fdc837SLawrence Stewart 	/*
657b0fdc837SLawrence Stewart 	 * Assess the status of the clock, if the last update is too old, it is
658b0fdc837SLawrence Stewart 	 * likely the synchronisation daemon is dead and the clock is free
659b0fdc837SLawrence Stewart 	 * running.
660b0fdc837SLawrence Stewart 	 */
661b0fdc837SLawrence Stewart 	if (ffclock_updated == 0) {
662b0fdc837SLawrence Stewart 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
663b0fdc837SLawrence Stewart 		ffclock_convert_delta(ffdelta, cest->period, &bt);
664b0fdc837SLawrence Stewart 		if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
665b0fdc837SLawrence Stewart 			ffclock_status |= FFCLOCK_STA_UNSYNC;
666b0fdc837SLawrence Stewart 	}
667b0fdc837SLawrence Stewart 
668b0fdc837SLawrence Stewart 	/*
669b0fdc837SLawrence Stewart 	 * If available, grab updated clock estimates and make them current.
670b0fdc837SLawrence Stewart 	 * Recompute time at this tick using the updated estimates. The clock
671b0fdc837SLawrence Stewart 	 * estimates passed the feed-forward synchronisation daemon may result
672b0fdc837SLawrence Stewart 	 * in time conversion that is not monotonically increasing (just after
673b0fdc837SLawrence Stewart 	 * the update). time_lerp is a particular linear interpolation over the
674b0fdc837SLawrence Stewart 	 * synchronisation algo polling period that ensures monotonicity for the
675b0fdc837SLawrence Stewart 	 * clock ids requesting it.
676b0fdc837SLawrence Stewart 	 */
677b0fdc837SLawrence Stewart 	if (ffclock_updated > 0) {
678b0fdc837SLawrence Stewart 		bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
679b0fdc837SLawrence Stewart 		ffdelta = ffth->tick_ffcount - cest->update_ffcount;
680b0fdc837SLawrence Stewart 		ffth->tick_time = cest->update_time;
681b0fdc837SLawrence Stewart 		ffclock_convert_delta(ffdelta, cest->period, &bt);
682b0fdc837SLawrence Stewart 		bintime_add(&ffth->tick_time, &bt);
683b0fdc837SLawrence Stewart 
684b0fdc837SLawrence Stewart 		/* ffclock_reset sets ffclock_updated to INT8_MAX */
685b0fdc837SLawrence Stewart 		if (ffclock_updated == INT8_MAX)
686b0fdc837SLawrence Stewart 			ffth->tick_time_lerp = ffth->tick_time;
687b0fdc837SLawrence Stewart 
688b0fdc837SLawrence Stewart 		if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
689b0fdc837SLawrence Stewart 			forward_jump = 1;
690b0fdc837SLawrence Stewart 		else
691b0fdc837SLawrence Stewart 			forward_jump = 0;
692b0fdc837SLawrence Stewart 
693b0fdc837SLawrence Stewart 		bintime_clear(&gap_lerp);
694b0fdc837SLawrence Stewart 		if (forward_jump) {
695b0fdc837SLawrence Stewart 			gap_lerp = ffth->tick_time;
696b0fdc837SLawrence Stewart 			bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
697b0fdc837SLawrence Stewart 		} else {
698b0fdc837SLawrence Stewart 			gap_lerp = ffth->tick_time_lerp;
699b0fdc837SLawrence Stewart 			bintime_sub(&gap_lerp, &ffth->tick_time);
700b0fdc837SLawrence Stewart 		}
701b0fdc837SLawrence Stewart 
702b0fdc837SLawrence Stewart 		/*
703b0fdc837SLawrence Stewart 		 * The reset from the RTC clock may be far from accurate, and
704b0fdc837SLawrence Stewart 		 * reducing the gap between real time and interpolated time
705b0fdc837SLawrence Stewart 		 * could take a very long time if the interpolated clock insists
706b0fdc837SLawrence Stewart 		 * on strict monotonicity. The clock is reset under very strict
707b0fdc837SLawrence Stewart 		 * conditions (kernel time is known to be wrong and
708b0fdc837SLawrence Stewart 		 * synchronization daemon has been restarted recently.
709b0fdc837SLawrence Stewart 		 * ffclock_boottime absorbs the jump to ensure boot time is
710b0fdc837SLawrence Stewart 		 * correct and uptime functions stay consistent.
711b0fdc837SLawrence Stewart 		 */
712b0fdc837SLawrence Stewart 		if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
713b0fdc837SLawrence Stewart 		    ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
714b0fdc837SLawrence Stewart 		    ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
715b0fdc837SLawrence Stewart 			if (forward_jump)
716b0fdc837SLawrence Stewart 				bintime_add(&ffclock_boottime, &gap_lerp);
717b0fdc837SLawrence Stewart 			else
718b0fdc837SLawrence Stewart 				bintime_sub(&ffclock_boottime, &gap_lerp);
719b0fdc837SLawrence Stewart 			ffth->tick_time_lerp = ffth->tick_time;
720b0fdc837SLawrence Stewart 			bintime_clear(&gap_lerp);
721b0fdc837SLawrence Stewart 		}
722b0fdc837SLawrence Stewart 
723b0fdc837SLawrence Stewart 		ffclock_status = cest->status;
724b0fdc837SLawrence Stewart 		ffth->period_lerp = cest->period;
725b0fdc837SLawrence Stewart 
726b0fdc837SLawrence Stewart 		/*
727b0fdc837SLawrence Stewart 		 * Compute corrected period used for the linear interpolation of
728b0fdc837SLawrence Stewart 		 * time. The rate of linear interpolation is capped to 5000PPM
729b0fdc837SLawrence Stewart 		 * (5ms/s).
730b0fdc837SLawrence Stewart 		 */
731b0fdc837SLawrence Stewart 		if (bintime_isset(&gap_lerp)) {
732b0fdc837SLawrence Stewart 			ffdelta = cest->update_ffcount;
733b0fdc837SLawrence Stewart 			ffdelta -= fftimehands->cest.update_ffcount;
734b0fdc837SLawrence Stewart 			ffclock_convert_delta(ffdelta, cest->period, &bt);
735b0fdc837SLawrence Stewart 			polling = bt.sec;
736b0fdc837SLawrence Stewart 			bt.sec = 0;
737b0fdc837SLawrence Stewart 			bt.frac = 5000000 * (uint64_t)18446744073LL;
738b0fdc837SLawrence Stewart 			bintime_mul(&bt, polling);
739b0fdc837SLawrence Stewart 			if (bintime_cmp(&gap_lerp, &bt, >))
740b0fdc837SLawrence Stewart 				gap_lerp = bt;
741b0fdc837SLawrence Stewart 
742b0fdc837SLawrence Stewart 			/* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
743b0fdc837SLawrence Stewart 			frac = 0;
744b0fdc837SLawrence Stewart 			if (gap_lerp.sec > 0) {
745b0fdc837SLawrence Stewart 				frac -= 1;
746b0fdc837SLawrence Stewart 				frac /= ffdelta / gap_lerp.sec;
747b0fdc837SLawrence Stewart 			}
748b0fdc837SLawrence Stewart 			frac += gap_lerp.frac / ffdelta;
749b0fdc837SLawrence Stewart 
750b0fdc837SLawrence Stewart 			if (forward_jump)
751b0fdc837SLawrence Stewart 				ffth->period_lerp += frac;
752b0fdc837SLawrence Stewart 			else
753b0fdc837SLawrence Stewart 				ffth->period_lerp -= frac;
754b0fdc837SLawrence Stewart 		}
755b0fdc837SLawrence Stewart 
756b0fdc837SLawrence Stewart 		ffclock_updated = 0;
757b0fdc837SLawrence Stewart 	}
758b0fdc837SLawrence Stewart 	if (++ogen == 0)
759b0fdc837SLawrence Stewart 		ogen = 1;
760b0fdc837SLawrence Stewart 	ffth->gen = ogen;
761b0fdc837SLawrence Stewart 	fftimehands = ffth;
762b0fdc837SLawrence Stewart }
763b0fdc837SLawrence Stewart 
764b0fdc837SLawrence Stewart /*
765b0fdc837SLawrence Stewart  * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
766b0fdc837SLawrence Stewart  * the old and new hardware counter cannot be read simultaneously. tc_windup()
767b0fdc837SLawrence Stewart  * does read the two counters 'back to back', but a few cycles are effectively
768b0fdc837SLawrence Stewart  * lost, and not accumulated in tick_ffcount. This is a fairly radical
769b0fdc837SLawrence Stewart  * operation for a feed-forward synchronization daemon, and it is its job to not
770b0fdc837SLawrence Stewart  * pushing irrelevant data to the kernel. Because there is no locking here,
771b0fdc837SLawrence Stewart  * simply force to ignore pending or next update to give daemon a chance to
772b0fdc837SLawrence Stewart  * realize the counter has changed.
773b0fdc837SLawrence Stewart  */
774b0fdc837SLawrence Stewart static void
ffclock_change_tc(struct timehands * th)775b0fdc837SLawrence Stewart ffclock_change_tc(struct timehands *th)
776b0fdc837SLawrence Stewart {
777b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
778b0fdc837SLawrence Stewart 	struct ffclock_estimate *cest;
779b0fdc837SLawrence Stewart 	struct timecounter *tc;
780b0fdc837SLawrence Stewart 	uint8_t ogen;
781b0fdc837SLawrence Stewart 
782b0fdc837SLawrence Stewart 	tc = th->th_counter;
783b0fdc837SLawrence Stewart 	ffth = fftimehands->next;
784b0fdc837SLawrence Stewart 	ogen = ffth->gen;
785b0fdc837SLawrence Stewart 	ffth->gen = 0;
786b0fdc837SLawrence Stewart 
787b0fdc837SLawrence Stewart 	cest = &ffth->cest;
788b0fdc837SLawrence Stewart 	bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
789b0fdc837SLawrence Stewart 	cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
790b0fdc837SLawrence Stewart 	cest->errb_abs = 0;
791b0fdc837SLawrence Stewart 	cest->errb_rate = 0;
792b0fdc837SLawrence Stewart 	cest->status |= FFCLOCK_STA_UNSYNC;
793b0fdc837SLawrence Stewart 
794b0fdc837SLawrence Stewart 	ffth->tick_ffcount = fftimehands->tick_ffcount;
795b0fdc837SLawrence Stewart 	ffth->tick_time_lerp = fftimehands->tick_time_lerp;
796b0fdc837SLawrence Stewart 	ffth->tick_time = fftimehands->tick_time;
797b0fdc837SLawrence Stewart 	ffth->period_lerp = cest->period;
798b0fdc837SLawrence Stewart 
799b0fdc837SLawrence Stewart 	/* Do not lock but ignore next update from synchronization daemon. */
800b0fdc837SLawrence Stewart 	ffclock_updated--;
801b0fdc837SLawrence Stewart 
802b0fdc837SLawrence Stewart 	if (++ogen == 0)
803b0fdc837SLawrence Stewart 		ogen = 1;
804b0fdc837SLawrence Stewart 	ffth->gen = ogen;
805b0fdc837SLawrence Stewart 	fftimehands = ffth;
806b0fdc837SLawrence Stewart }
807b0fdc837SLawrence Stewart 
808b0fdc837SLawrence Stewart /*
809b0fdc837SLawrence Stewart  * Retrieve feed-forward counter and time of last kernel tick.
810b0fdc837SLawrence Stewart  */
811b0fdc837SLawrence Stewart void
ffclock_last_tick(ffcounter * ffcount,struct bintime * bt,uint32_t flags)812b0fdc837SLawrence Stewart ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
813b0fdc837SLawrence Stewart {
814b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
815b0fdc837SLawrence Stewart 	uint8_t gen;
816b0fdc837SLawrence Stewart 
817b0fdc837SLawrence Stewart 	/*
818b0fdc837SLawrence Stewart 	 * No locking but check generation has not changed. Also need to make
819b0fdc837SLawrence Stewart 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
820b0fdc837SLawrence Stewart 	 */
821b0fdc837SLawrence Stewart 	do {
822b0fdc837SLawrence Stewart 		ffth = fftimehands;
823b0fdc837SLawrence Stewart 		gen = ffth->gen;
824b0fdc837SLawrence Stewart 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
825b0fdc837SLawrence Stewart 			*bt = ffth->tick_time_lerp;
826b0fdc837SLawrence Stewart 		else
827b0fdc837SLawrence Stewart 			*bt = ffth->tick_time;
828b0fdc837SLawrence Stewart 		*ffcount = ffth->tick_ffcount;
829b0fdc837SLawrence Stewart 	} while (gen == 0 || gen != ffth->gen);
830b0fdc837SLawrence Stewart }
831b0fdc837SLawrence Stewart 
832b0fdc837SLawrence Stewart /*
833b0fdc837SLawrence Stewart  * Absolute clock conversion. Low level function to convert ffcounter to
834b0fdc837SLawrence Stewart  * bintime. The ffcounter is converted using the current ffclock period estimate
835b0fdc837SLawrence Stewart  * or the "interpolated period" to ensure monotonicity.
836b0fdc837SLawrence Stewart  * NOTE: this conversion may have been deferred, and the clock updated since the
837b0fdc837SLawrence Stewart  * hardware counter has been read.
838b0fdc837SLawrence Stewart  */
839b0fdc837SLawrence Stewart void
ffclock_convert_abs(ffcounter ffcount,struct bintime * bt,uint32_t flags)840b0fdc837SLawrence Stewart ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
841b0fdc837SLawrence Stewart {
842b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
843b0fdc837SLawrence Stewart 	struct bintime bt2;
844b0fdc837SLawrence Stewart 	ffcounter ffdelta;
845b0fdc837SLawrence Stewart 	uint8_t gen;
846b0fdc837SLawrence Stewart 
847b0fdc837SLawrence Stewart 	/*
848b0fdc837SLawrence Stewart 	 * No locking but check generation has not changed. Also need to make
849b0fdc837SLawrence Stewart 	 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
850b0fdc837SLawrence Stewart 	 */
851b0fdc837SLawrence Stewart 	do {
852b0fdc837SLawrence Stewart 		ffth = fftimehands;
853b0fdc837SLawrence Stewart 		gen = ffth->gen;
854b0fdc837SLawrence Stewart 		if (ffcount > ffth->tick_ffcount)
855b0fdc837SLawrence Stewart 			ffdelta = ffcount - ffth->tick_ffcount;
856b0fdc837SLawrence Stewart 		else
857b0fdc837SLawrence Stewart 			ffdelta = ffth->tick_ffcount - ffcount;
858b0fdc837SLawrence Stewart 
859b0fdc837SLawrence Stewart 		if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
860b0fdc837SLawrence Stewart 			*bt = ffth->tick_time_lerp;
861b0fdc837SLawrence Stewart 			ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
862b0fdc837SLawrence Stewart 		} else {
863b0fdc837SLawrence Stewart 			*bt = ffth->tick_time;
864b0fdc837SLawrence Stewart 			ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
865b0fdc837SLawrence Stewart 		}
866b0fdc837SLawrence Stewart 
867b0fdc837SLawrence Stewart 		if (ffcount > ffth->tick_ffcount)
868b0fdc837SLawrence Stewart 			bintime_add(bt, &bt2);
869b0fdc837SLawrence Stewart 		else
870b0fdc837SLawrence Stewart 			bintime_sub(bt, &bt2);
871b0fdc837SLawrence Stewart 	} while (gen == 0 || gen != ffth->gen);
872b0fdc837SLawrence Stewart }
873b0fdc837SLawrence Stewart 
874b0fdc837SLawrence Stewart /*
875b0fdc837SLawrence Stewart  * Difference clock conversion.
876b0fdc837SLawrence Stewart  * Low level function to Convert a time interval measured in RAW counter units
877b0fdc837SLawrence Stewart  * into bintime. The difference clock allows measuring small intervals much more
878b0fdc837SLawrence Stewart  * reliably than the absolute clock.
879b0fdc837SLawrence Stewart  */
880b0fdc837SLawrence Stewart void
ffclock_convert_diff(ffcounter ffdelta,struct bintime * bt)881b0fdc837SLawrence Stewart ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
882b0fdc837SLawrence Stewart {
883b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
884b0fdc837SLawrence Stewart 	uint8_t gen;
885b0fdc837SLawrence Stewart 
886b0fdc837SLawrence Stewart 	/* No locking but check generation has not changed. */
887b0fdc837SLawrence Stewart 	do {
888b0fdc837SLawrence Stewart 		ffth = fftimehands;
889b0fdc837SLawrence Stewart 		gen = ffth->gen;
890b0fdc837SLawrence Stewart 		ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
891b0fdc837SLawrence Stewart 	} while (gen == 0 || gen != ffth->gen);
892b0fdc837SLawrence Stewart }
893b0fdc837SLawrence Stewart 
894b0fdc837SLawrence Stewart /*
895b0fdc837SLawrence Stewart  * Access to current ffcounter value.
896b0fdc837SLawrence Stewart  */
897b0fdc837SLawrence Stewart void
ffclock_read_counter(ffcounter * ffcount)898b0fdc837SLawrence Stewart ffclock_read_counter(ffcounter *ffcount)
899b0fdc837SLawrence Stewart {
900b0fdc837SLawrence Stewart 	struct timehands *th;
901b0fdc837SLawrence Stewart 	struct fftimehands *ffth;
902b0fdc837SLawrence Stewart 	unsigned int gen, delta;
903b0fdc837SLawrence Stewart 
904b0fdc837SLawrence Stewart 	/*
905b0fdc837SLawrence Stewart 	 * ffclock_windup() called from tc_windup(), safe to rely on
906b0fdc837SLawrence Stewart 	 * th->th_generation only, for correct delta and ffcounter.
907b0fdc837SLawrence Stewart 	 */
908b0fdc837SLawrence Stewart 	do {
909b0fdc837SLawrence Stewart 		th = timehands;
910f4b5a972SKonstantin Belousov 		gen = atomic_load_acq_int(&th->th_generation);
911b0fdc837SLawrence Stewart 		ffth = fftimehands;
912b0fdc837SLawrence Stewart 		delta = tc_delta(th);
913b0fdc837SLawrence Stewart 		*ffcount = ffth->tick_ffcount;
914f4b5a972SKonstantin Belousov 		atomic_thread_fence_acq();
915f4b5a972SKonstantin Belousov 	} while (gen == 0 || gen != th->th_generation);
916b0fdc837SLawrence Stewart 
917b0fdc837SLawrence Stewart 	*ffcount += delta;
918b0fdc837SLawrence Stewart }
9199bce0f05SLawrence Stewart 
9209bce0f05SLawrence Stewart void
binuptime(struct bintime * bt)9219bce0f05SLawrence Stewart binuptime(struct bintime *bt)
9229bce0f05SLawrence Stewart {
9239bce0f05SLawrence Stewart 
92488394fe4SLawrence Stewart 	binuptime_fromclock(bt, sysclock_active);
9259bce0f05SLawrence Stewart }
9269bce0f05SLawrence Stewart 
9279bce0f05SLawrence Stewart void
nanouptime(struct timespec * tsp)9289bce0f05SLawrence Stewart nanouptime(struct timespec *tsp)
9299bce0f05SLawrence Stewart {
9309bce0f05SLawrence Stewart 
93188394fe4SLawrence Stewart 	nanouptime_fromclock(tsp, sysclock_active);
9329bce0f05SLawrence Stewart }
9339bce0f05SLawrence Stewart 
9349bce0f05SLawrence Stewart void
microuptime(struct timeval * tvp)9359bce0f05SLawrence Stewart microuptime(struct timeval *tvp)
9369bce0f05SLawrence Stewart {
9379bce0f05SLawrence Stewart 
93888394fe4SLawrence Stewart 	microuptime_fromclock(tvp, sysclock_active);
9399bce0f05SLawrence Stewart }
9409bce0f05SLawrence Stewart 
9419bce0f05SLawrence Stewart void
bintime(struct bintime * bt)9429bce0f05SLawrence Stewart bintime(struct bintime *bt)
9439bce0f05SLawrence Stewart {
9449bce0f05SLawrence Stewart 
94588394fe4SLawrence Stewart 	bintime_fromclock(bt, sysclock_active);
9469bce0f05SLawrence Stewart }
9479bce0f05SLawrence Stewart 
9489bce0f05SLawrence Stewart void
nanotime(struct timespec * tsp)9499bce0f05SLawrence Stewart nanotime(struct timespec *tsp)
9509bce0f05SLawrence Stewart {
9519bce0f05SLawrence Stewart 
95288394fe4SLawrence Stewart 	nanotime_fromclock(tsp, sysclock_active);
9539bce0f05SLawrence Stewart }
9549bce0f05SLawrence Stewart 
9559bce0f05SLawrence Stewart void
microtime(struct timeval * tvp)9569bce0f05SLawrence Stewart microtime(struct timeval *tvp)
9579bce0f05SLawrence Stewart {
9589bce0f05SLawrence Stewart 
95988394fe4SLawrence Stewart 	microtime_fromclock(tvp, sysclock_active);
9609bce0f05SLawrence Stewart }
9619bce0f05SLawrence Stewart 
9629bce0f05SLawrence Stewart void
getbinuptime(struct bintime * bt)9639bce0f05SLawrence Stewart getbinuptime(struct bintime *bt)
9649bce0f05SLawrence Stewart {
9659bce0f05SLawrence Stewart 
96688394fe4SLawrence Stewart 	getbinuptime_fromclock(bt, sysclock_active);
9679bce0f05SLawrence Stewart }
9689bce0f05SLawrence Stewart 
9699bce0f05SLawrence Stewart void
getnanouptime(struct timespec * tsp)9709bce0f05SLawrence Stewart getnanouptime(struct timespec *tsp)
9719bce0f05SLawrence Stewart {
9729bce0f05SLawrence Stewart 
97388394fe4SLawrence Stewart 	getnanouptime_fromclock(tsp, sysclock_active);
9749bce0f05SLawrence Stewart }
9759bce0f05SLawrence Stewart 
9769bce0f05SLawrence Stewart void
getmicrouptime(struct timeval * tvp)9779bce0f05SLawrence Stewart getmicrouptime(struct timeval *tvp)
9789bce0f05SLawrence Stewart {
9799bce0f05SLawrence Stewart 
98088394fe4SLawrence Stewart 	getmicrouptime_fromclock(tvp, sysclock_active);
9819bce0f05SLawrence Stewart }
9829bce0f05SLawrence Stewart 
9839bce0f05SLawrence Stewart void
getbintime(struct bintime * bt)9849bce0f05SLawrence Stewart getbintime(struct bintime *bt)
9859bce0f05SLawrence Stewart {
9869bce0f05SLawrence Stewart 
98788394fe4SLawrence Stewart 	getbintime_fromclock(bt, sysclock_active);
9889bce0f05SLawrence Stewart }
9899bce0f05SLawrence Stewart 
9909bce0f05SLawrence Stewart void
getnanotime(struct timespec * tsp)9919bce0f05SLawrence Stewart getnanotime(struct timespec *tsp)
9929bce0f05SLawrence Stewart {
9939bce0f05SLawrence Stewart 
99488394fe4SLawrence Stewart 	getnanotime_fromclock(tsp, sysclock_active);
9959bce0f05SLawrence Stewart }
9969bce0f05SLawrence Stewart 
9979bce0f05SLawrence Stewart void
getmicrotime(struct timeval * tvp)9989bce0f05SLawrence Stewart getmicrotime(struct timeval *tvp)
9999bce0f05SLawrence Stewart {
10009bce0f05SLawrence Stewart 
100188394fe4SLawrence Stewart 	getmicrouptime_fromclock(tvp, sysclock_active);
10029bce0f05SLawrence Stewart }
10036cedd609SLawrence Stewart 
1004b0fdc837SLawrence Stewart #endif /* FFCLOCK */
1005b0fdc837SLawrence Stewart 
100639acc78aSPoul-Henning Kamp /*
100757d025c3SGeorge V. Neville-Neil  * This is a clone of getnanotime and used for walltimestamps.
100857d025c3SGeorge V. Neville-Neil  * The dtrace_ prefix prevents fbt from creating probes for
100957d025c3SGeorge V. Neville-Neil  * it so walltimestamp can be safely used in all fbt probes.
101057d025c3SGeorge V. Neville-Neil  */
101157d025c3SGeorge V. Neville-Neil void
dtrace_getnanotime(struct timespec * tsp)101257d025c3SGeorge V. Neville-Neil dtrace_getnanotime(struct timespec *tsp)
101357d025c3SGeorge V. Neville-Neil {
101457d025c3SGeorge V. Neville-Neil 
10156cf2362eSKonstantin Belousov 	GETTHMEMBER(tsp, th_nanotime);
101657d025c3SGeorge V. Neville-Neil }
101757d025c3SGeorge V. Neville-Neil 
101857d025c3SGeorge V. Neville-Neil /*
101930b68ecdSRobert Watson  * This is a clone of getnanouptime used for time since boot.
102030b68ecdSRobert Watson  * The dtrace_ prefix prevents fbt from creating probes for
102130b68ecdSRobert Watson  * it so an uptime that can be safely used in all fbt probes.
102230b68ecdSRobert Watson  */
102330b68ecdSRobert Watson void
dtrace_getnanouptime(struct timespec * tsp)102430b68ecdSRobert Watson dtrace_getnanouptime(struct timespec *tsp)
102530b68ecdSRobert Watson {
102630b68ecdSRobert Watson 	struct bintime bt;
102730b68ecdSRobert Watson 
102830b68ecdSRobert Watson 	GETTHMEMBER(&bt, th_offset);
102930b68ecdSRobert Watson 	bintime2timespec(&bt, tsp);
103030b68ecdSRobert Watson }
103130b68ecdSRobert Watson 
103230b68ecdSRobert Watson /*
10336cedd609SLawrence Stewart  * System clock currently providing time to the system. Modifiable via sysctl
10346cedd609SLawrence Stewart  * when the FFCLOCK option is defined.
10356cedd609SLawrence Stewart  */
10366cedd609SLawrence Stewart int sysclock_active = SYSCLOCK_FBCK;
10376cedd609SLawrence Stewart 
10386cedd609SLawrence Stewart /* Internal NTP status and error estimates. */
10396cedd609SLawrence Stewart extern int time_status;
10406cedd609SLawrence Stewart extern long time_esterror;
10416cedd609SLawrence Stewart 
10426cedd609SLawrence Stewart /*
10436cedd609SLawrence Stewart  * Take a snapshot of sysclock data which can be used to compare system clocks
10446cedd609SLawrence Stewart  * and generate timestamps after the fact.
10456cedd609SLawrence Stewart  */
10466cedd609SLawrence Stewart void
sysclock_getsnapshot(struct sysclock_snap * clock_snap,int fast)10476cedd609SLawrence Stewart sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
10486cedd609SLawrence Stewart {
10496cedd609SLawrence Stewart 	struct fbclock_info *fbi;
10506cedd609SLawrence Stewart 	struct timehands *th;
10516cedd609SLawrence Stewart 	struct bintime bt;
10526cedd609SLawrence Stewart 	unsigned int delta, gen;
10536cedd609SLawrence Stewart #ifdef FFCLOCK
10546cedd609SLawrence Stewart 	ffcounter ffcount;
10556cedd609SLawrence Stewart 	struct fftimehands *ffth;
10566cedd609SLawrence Stewart 	struct ffclock_info *ffi;
10576cedd609SLawrence Stewart 	struct ffclock_estimate cest;
10586cedd609SLawrence Stewart 
10596cedd609SLawrence Stewart 	ffi = &clock_snap->ff_info;
10606cedd609SLawrence Stewart #endif
10616cedd609SLawrence Stewart 
10626cedd609SLawrence Stewart 	fbi = &clock_snap->fb_info;
10636cedd609SLawrence Stewart 	delta = 0;
10646cedd609SLawrence Stewart 
10656cedd609SLawrence Stewart 	do {
10666cedd609SLawrence Stewart 		th = timehands;
1067f4b5a972SKonstantin Belousov 		gen = atomic_load_acq_int(&th->th_generation);
10686cedd609SLawrence Stewart 		fbi->th_scale = th->th_scale;
10696cedd609SLawrence Stewart 		fbi->tick_time = th->th_offset;
10706cedd609SLawrence Stewart #ifdef FFCLOCK
10716cedd609SLawrence Stewart 		ffth = fftimehands;
10726cedd609SLawrence Stewart 		ffi->tick_time = ffth->tick_time_lerp;
10736cedd609SLawrence Stewart 		ffi->tick_time_lerp = ffth->tick_time_lerp;
10746cedd609SLawrence Stewart 		ffi->period = ffth->cest.period;
10756cedd609SLawrence Stewart 		ffi->period_lerp = ffth->period_lerp;
10766cedd609SLawrence Stewart 		clock_snap->ffcount = ffth->tick_ffcount;
10776cedd609SLawrence Stewart 		cest = ffth->cest;
10786cedd609SLawrence Stewart #endif
10796cedd609SLawrence Stewart 		if (!fast)
10806cedd609SLawrence Stewart 			delta = tc_delta(th);
1081f4b5a972SKonstantin Belousov 		atomic_thread_fence_acq();
1082f4b5a972SKonstantin Belousov 	} while (gen == 0 || gen != th->th_generation);
10836cedd609SLawrence Stewart 
10846cedd609SLawrence Stewart 	clock_snap->delta = delta;
10856cedd609SLawrence Stewart 	clock_snap->sysclock_active = sysclock_active;
10866cedd609SLawrence Stewart 
10876cedd609SLawrence Stewart 	/* Record feedback clock status and error. */
10886cedd609SLawrence Stewart 	clock_snap->fb_info.status = time_status;
10896cedd609SLawrence Stewart 	/* XXX: Very crude estimate of feedback clock error. */
10906cedd609SLawrence Stewart 	bt.sec = time_esterror / 1000000;
10916cedd609SLawrence Stewart 	bt.frac = ((time_esterror - bt.sec) * 1000000) *
10926cedd609SLawrence Stewart 	    (uint64_t)18446744073709ULL;
10936cedd609SLawrence Stewart 	clock_snap->fb_info.error = bt;
10946cedd609SLawrence Stewart 
10956cedd609SLawrence Stewart #ifdef FFCLOCK
10966cedd609SLawrence Stewart 	if (!fast)
10976cedd609SLawrence Stewart 		clock_snap->ffcount += delta;
10986cedd609SLawrence Stewart 
10996cedd609SLawrence Stewart 	/* Record feed-forward clock leap second adjustment. */
11006cedd609SLawrence Stewart 	ffi->leapsec_adjustment = cest.leapsec_total;
11016cedd609SLawrence Stewart 	if (clock_snap->ffcount > cest.leapsec_next)
11026cedd609SLawrence Stewart 		ffi->leapsec_adjustment -= cest.leapsec;
11036cedd609SLawrence Stewart 
11046cedd609SLawrence Stewart 	/* Record feed-forward clock status and error. */
11056cedd609SLawrence Stewart 	clock_snap->ff_info.status = cest.status;
11066cedd609SLawrence Stewart 	ffcount = clock_snap->ffcount - cest.update_ffcount;
11076cedd609SLawrence Stewart 	ffclock_convert_delta(ffcount, cest.period, &bt);
11086cedd609SLawrence Stewart 	/* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
11096cedd609SLawrence Stewart 	bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
11106cedd609SLawrence Stewart 	/* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
11116cedd609SLawrence Stewart 	bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
11126cedd609SLawrence Stewart 	clock_snap->ff_info.error = bt;
11136cedd609SLawrence Stewart #endif
11146cedd609SLawrence Stewart }
11156cedd609SLawrence Stewart 
11166cedd609SLawrence Stewart /*
11176cedd609SLawrence Stewart  * Convert a sysclock snapshot into a struct bintime based on the specified
11186cedd609SLawrence Stewart  * clock source and flags.
11196cedd609SLawrence Stewart  */
11206cedd609SLawrence Stewart int
sysclock_snap2bintime(struct sysclock_snap * cs,struct bintime * bt,int whichclock,uint32_t flags)11216cedd609SLawrence Stewart sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
11226cedd609SLawrence Stewart     int whichclock, uint32_t flags)
11236cedd609SLawrence Stewart {
1124584b675eSKonstantin Belousov 	struct bintime boottimebin;
11256cedd609SLawrence Stewart #ifdef FFCLOCK
11266cedd609SLawrence Stewart 	struct bintime bt2;
11276cedd609SLawrence Stewart 	uint64_t period;
11286cedd609SLawrence Stewart #endif
11296cedd609SLawrence Stewart 
11306cedd609SLawrence Stewart 	switch (whichclock) {
11316cedd609SLawrence Stewart 	case SYSCLOCK_FBCK:
11326cedd609SLawrence Stewart 		*bt = cs->fb_info.tick_time;
11336cedd609SLawrence Stewart 
11346cedd609SLawrence Stewart 		/* If snapshot was created with !fast, delta will be >0. */
11356cedd609SLawrence Stewart 		if (cs->delta > 0)
11366cedd609SLawrence Stewart 			bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
11376cedd609SLawrence Stewart 
1138584b675eSKonstantin Belousov 		if ((flags & FBCLOCK_UPTIME) == 0) {
1139584b675eSKonstantin Belousov 			getboottimebin(&boottimebin);
11406cedd609SLawrence Stewart 			bintime_add(bt, &boottimebin);
1141584b675eSKonstantin Belousov 		}
11426cedd609SLawrence Stewart 		break;
11436cedd609SLawrence Stewart #ifdef FFCLOCK
11446cedd609SLawrence Stewart 	case SYSCLOCK_FFWD:
11456cedd609SLawrence Stewart 		if (flags & FFCLOCK_LERP) {
11466cedd609SLawrence Stewart 			*bt = cs->ff_info.tick_time_lerp;
11476cedd609SLawrence Stewart 			period = cs->ff_info.period_lerp;
11486cedd609SLawrence Stewart 		} else {
11496cedd609SLawrence Stewart 			*bt = cs->ff_info.tick_time;
11506cedd609SLawrence Stewart 			period = cs->ff_info.period;
11516cedd609SLawrence Stewart 		}
11526cedd609SLawrence Stewart 
11536cedd609SLawrence Stewart 		/* If snapshot was created with !fast, delta will be >0. */
11546cedd609SLawrence Stewart 		if (cs->delta > 0) {
11556cedd609SLawrence Stewart 			ffclock_convert_delta(cs->delta, period, &bt2);
11566cedd609SLawrence Stewart 			bintime_add(bt, &bt2);
11576cedd609SLawrence Stewart 		}
11586cedd609SLawrence Stewart 
11596cedd609SLawrence Stewart 		/* Leap second adjustment. */
11606cedd609SLawrence Stewart 		if (flags & FFCLOCK_LEAPSEC)
11616cedd609SLawrence Stewart 			bt->sec -= cs->ff_info.leapsec_adjustment;
11626cedd609SLawrence Stewart 
11636cedd609SLawrence Stewart 		/* Boot time adjustment, for uptime/monotonic clocks. */
11646cedd609SLawrence Stewart 		if (flags & FFCLOCK_UPTIME)
11656cedd609SLawrence Stewart 			bintime_sub(bt, &ffclock_boottime);
1166de02885aSKevin Lo 		break;
11676cedd609SLawrence Stewart #endif
11686cedd609SLawrence Stewart 	default:
11696cedd609SLawrence Stewart 		return (EINVAL);
11706cedd609SLawrence Stewart 		break;
11716cedd609SLawrence Stewart 	}
11726cedd609SLawrence Stewart 
11736cedd609SLawrence Stewart 	return (0);
11746cedd609SLawrence Stewart }
11756cedd609SLawrence Stewart 
11766cedd609SLawrence Stewart /*
117778a49a45SPoul-Henning Kamp  * Initialize a new timecounter and possibly use it.
11784e2befc0SPoul-Henning Kamp  */
11797ec73f64SPoul-Henning Kamp void
tc_init(struct timecounter * tc)118091266b96SPoul-Henning Kamp tc_init(struct timecounter *tc)
11817ec73f64SPoul-Henning Kamp {
1182555a5de2SPoul-Henning Kamp 	u_int u;
118393ef14a7SDavid Malone 	struct sysctl_oid *tc_root;
11847ec73f64SPoul-Henning Kamp 
1185c679c734SPoul-Henning Kamp 	u = tc->tc_frequency / tc->tc_counter_mask;
1186555a5de2SPoul-Henning Kamp 	/* XXX: We need some margin here, 10% is a guess */
1187555a5de2SPoul-Henning Kamp 	u *= 11;
1188555a5de2SPoul-Henning Kamp 	u /= 10;
1189c679c734SPoul-Henning Kamp 	if (u > hz && tc->tc_quality >= 0) {
1190c679c734SPoul-Henning Kamp 		tc->tc_quality = -2000;
1191c679c734SPoul-Henning Kamp 		if (bootverbose) {
1192c679c734SPoul-Henning Kamp 			printf("Timecounter \"%s\" frequency %ju Hz",
1193555a5de2SPoul-Henning Kamp 			    tc->tc_name, (uintmax_t)tc->tc_frequency);
1194c679c734SPoul-Henning Kamp 			printf(" -- Insufficient hz, needs at least %u\n", u);
1195c679c734SPoul-Henning Kamp 		}
1196c679c734SPoul-Henning Kamp 	} else if (tc->tc_quality >= 0 || bootverbose) {
1197555a5de2SPoul-Henning Kamp 		printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1198555a5de2SPoul-Henning Kamp 		    tc->tc_name, (uintmax_t)tc->tc_frequency,
119978a49a45SPoul-Henning Kamp 		    tc->tc_quality);
1200e46eeb89SPoul-Henning Kamp 	}
1201c679c734SPoul-Henning Kamp 
1202555a5de2SPoul-Henning Kamp 	/*
120393ef14a7SDavid Malone 	 * Set up sysctl tree for this counter.
120493ef14a7SDavid Malone 	 */
1205fd0f5970SEd Schouten 	tc_root = SYSCTL_ADD_NODE_WITH_LABEL(NULL,
120693ef14a7SDavid Malone 	    SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
12077029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
12087029da5cSPawel Biernacki 	    "timecounter description", "timecounter");
120993ef14a7SDavid Malone 	SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
121093ef14a7SDavid Malone 	    "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
121193ef14a7SDavid Malone 	    "mask for implemented bits");
121293ef14a7SDavid Malone 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
12137029da5cSPawel Biernacki 	    "counter", CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, tc,
12147029da5cSPawel Biernacki 	    sizeof(*tc), sysctl_kern_timecounter_get, "IU",
12157029da5cSPawel Biernacki 	    "current timecounter value");
121693ef14a7SDavid Malone 	SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
12177029da5cSPawel Biernacki 	    "frequency", CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, tc,
12187029da5cSPawel Biernacki 	    sizeof(*tc), sysctl_kern_timecounter_freq, "QU",
12197029da5cSPawel Biernacki 	    "timecounter frequency");
122093ef14a7SDavid Malone 	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
122193ef14a7SDavid Malone 	    "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
122293ef14a7SDavid Malone 	    "goodness of time counter");
1223621fd9dcSMark Johnston 
1224621fd9dcSMark Johnston 	mtx_lock(&tc_lock);
1225621fd9dcSMark Johnston 	tc->tc_next = timecounters;
1226621fd9dcSMark Johnston 	timecounters = tc;
1227621fd9dcSMark Johnston 
122893ef14a7SDavid Malone 	/*
1229e8bac3f2SIan Lepore 	 * Do not automatically switch if the current tc was specifically
1230e8bac3f2SIan Lepore 	 * chosen.  Never automatically use a timecounter with negative quality.
1231555a5de2SPoul-Henning Kamp 	 * Even though we run on the dummy counter, switching here may be
1232e8bac3f2SIan Lepore 	 * worse since this timecounter may not be monotonic.
1233555a5de2SPoul-Henning Kamp 	 */
1234e8bac3f2SIan Lepore 	if (tc_chosen)
1235621fd9dcSMark Johnston 		goto unlock;
123678a49a45SPoul-Henning Kamp 	if (tc->tc_quality < 0)
1237621fd9dcSMark Johnston 		goto unlock;
123856b9bee6SKonstantin Belousov 	if (tc_from_tunable[0] != '\0' &&
123956b9bee6SKonstantin Belousov 	    strcmp(tc->tc_name, tc_from_tunable) == 0) {
124056b9bee6SKonstantin Belousov 		tc_chosen = 1;
124156b9bee6SKonstantin Belousov 		tc_from_tunable[0] = '\0';
124256b9bee6SKonstantin Belousov 	} else {
124378a49a45SPoul-Henning Kamp 		if (tc->tc_quality < timecounter->tc_quality)
1244621fd9dcSMark Johnston 			goto unlock;
1245555a5de2SPoul-Henning Kamp 		if (tc->tc_quality == timecounter->tc_quality &&
1246555a5de2SPoul-Henning Kamp 		    tc->tc_frequency < timecounter->tc_frequency)
1247621fd9dcSMark Johnston 			goto unlock;
124856b9bee6SKonstantin Belousov 	}
1249555a5de2SPoul-Henning Kamp 	(void)tc->tc_get_timecount(tc);
12507ec73f64SPoul-Henning Kamp 	timecounter = tc;
1251621fd9dcSMark Johnston unlock:
1252621fd9dcSMark Johnston 	mtx_unlock(&tc_lock);
125362efba6aSPoul-Henning Kamp }
125462efba6aSPoul-Henning Kamp 
125539acc78aSPoul-Henning Kamp /* Report the frequency of the current timecounter. */
125660ae52f7SEd Schouten uint64_t
tc_getfrequency(void)125762efba6aSPoul-Henning Kamp tc_getfrequency(void)
125862efba6aSPoul-Henning Kamp {
125962efba6aSPoul-Henning Kamp 
12606b00cf46SPoul-Henning Kamp 	return (timehands->th_counter->tc_frequency);
12617ec73f64SPoul-Henning Kamp }
12627ec73f64SPoul-Henning Kamp 
12639dbdf2a1SEric van Gyzen static bool
sleeping_on_old_rtc(struct thread * td)12649dbdf2a1SEric van Gyzen sleeping_on_old_rtc(struct thread *td)
12659dbdf2a1SEric van Gyzen {
12669dbdf2a1SEric van Gyzen 
12678addc72bSEric van Gyzen 	/*
12688addc72bSEric van Gyzen 	 * td_rtcgen is modified by curthread when it is running,
12698addc72bSEric van Gyzen 	 * and by other threads in this function.  By finding the thread
12708addc72bSEric van Gyzen 	 * on a sleepqueue and holding the lock on the sleepqueue
12718addc72bSEric van Gyzen 	 * chain, we guarantee that the thread is not running and that
12728addc72bSEric van Gyzen 	 * modifying td_rtcgen is safe.  Setting td_rtcgen to zero informs
12738addc72bSEric van Gyzen 	 * the thread that it was woken due to a real-time clock adjustment.
12748addc72bSEric van Gyzen 	 * (The declaration of td_rtcgen refers to this comment.)
12758addc72bSEric van Gyzen 	 */
12769dbdf2a1SEric van Gyzen 	if (td->td_rtcgen != 0 && td->td_rtcgen != rtc_generation) {
12779dbdf2a1SEric van Gyzen 		td->td_rtcgen = 0;
12789dbdf2a1SEric van Gyzen 		return (true);
12799dbdf2a1SEric van Gyzen 	}
12809dbdf2a1SEric van Gyzen 	return (false);
12819dbdf2a1SEric van Gyzen }
12829dbdf2a1SEric van Gyzen 
12835760b029SKonstantin Belousov static struct mtx tc_setclock_mtx;
12845760b029SKonstantin Belousov MTX_SYSINIT(tc_setclock_init, &tc_setclock_mtx, "tcsetc", MTX_SPIN);
12855760b029SKonstantin Belousov 
128639acc78aSPoul-Henning Kamp /*
12874e82e5f6SWarner Losh  * Step our concept of UTC.  This is done by modifying our estimate of
12884e74721cSPoul-Henning Kamp  * when we booted.
12896b00cf46SPoul-Henning Kamp  */
12907ec73f64SPoul-Henning Kamp void
tc_setclock(struct timespec * ts)129191266b96SPoul-Henning Kamp tc_setclock(struct timespec *ts)
12927ec73f64SPoul-Henning Kamp {
12935b51d1deSPoul-Henning Kamp 	struct timespec tbef, taft;
12944e74721cSPoul-Henning Kamp 	struct bintime bt, bt2;
12957ec73f64SPoul-Henning Kamp 
12964e74721cSPoul-Henning Kamp 	timespec2bintime(ts, &bt);
12975760b029SKonstantin Belousov 	nanotime(&tbef);
12985760b029SKonstantin Belousov 	mtx_lock_spin(&tc_setclock_mtx);
12995760b029SKonstantin Belousov 	cpu_tick_calibrate(1);
13005b51d1deSPoul-Henning Kamp 	binuptime(&bt2);
13014e74721cSPoul-Henning Kamp 	bintime_sub(&bt, &bt2);
130239acc78aSPoul-Henning Kamp 
130339acc78aSPoul-Henning Kamp 	/* XXX fiddle all the little crinkly bits around the fiords... */
13045760b029SKonstantin Belousov 	tc_windup(&bt);
13055760b029SKonstantin Belousov 	mtx_unlock_spin(&tc_setclock_mtx);
13068addc72bSEric van Gyzen 
13079dbdf2a1SEric van Gyzen 	/* Avoid rtc_generation == 0, since td_rtcgen == 0 is special. */
13089dbdf2a1SEric van Gyzen 	atomic_add_rel_int(&rtc_generation, 2);
1309*af93fea7SJake Freeland 	timerfd_jumped();
13109dbdf2a1SEric van Gyzen 	sleepq_chains_remove_matching(sleeping_on_old_rtc);
13114e74721cSPoul-Henning Kamp 	if (timestepwarnings) {
13125760b029SKonstantin Belousov 		nanotime(&taft);
13135b51d1deSPoul-Henning Kamp 		log(LOG_INFO,
13145b51d1deSPoul-Henning Kamp 		    "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
13155b51d1deSPoul-Henning Kamp 		    (intmax_t)tbef.tv_sec, tbef.tv_nsec,
13165b51d1deSPoul-Henning Kamp 		    (intmax_t)taft.tv_sec, taft.tv_nsec,
1317ee57aeeaSPoul-Henning Kamp 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
13184e74721cSPoul-Henning Kamp 	}
13197ec73f64SPoul-Henning Kamp }
13207ec73f64SPoul-Henning Kamp 
132139acc78aSPoul-Henning Kamp /*
1322ae750fbaSSebastian Huber  * Recalculate the scaling factor.  We want the number of 1/2^64
1323ae750fbaSSebastian Huber  * fractions of a second per period of the hardware counter, taking
1324ae750fbaSSebastian Huber  * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1325ae750fbaSSebastian Huber  * processing provides us with.
1326ae750fbaSSebastian Huber  *
1327ae750fbaSSebastian Huber  * The th_adjustment is nanoseconds per second with 32 bit binary
1328ae750fbaSSebastian Huber  * fraction and we want 64 bit binary fraction of second:
1329ae750fbaSSebastian Huber  *
1330ae750fbaSSebastian Huber  *	 x = a * 2^32 / 10^9 = a * 4.294967296
1331ae750fbaSSebastian Huber  *
1332ae750fbaSSebastian Huber  * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1333ae750fbaSSebastian Huber  * we can only multiply by about 850 without overflowing, that
1334ae750fbaSSebastian Huber  * leaves no suitably precise fractions for multiply before divide.
1335ae750fbaSSebastian Huber  *
1336ae750fbaSSebastian Huber  * Divide before multiply with a fraction of 2199/512 results in a
1337ae750fbaSSebastian Huber  * systematic undercompensation of 10PPM of th_adjustment.  On a
1338ae750fbaSSebastian Huber  * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
1339ae750fbaSSebastian Huber  *
1340ae750fbaSSebastian Huber  * We happily sacrifice the lowest of the 64 bits of our result
1341ae750fbaSSebastian Huber  * to the goddess of code clarity.
1342ae750fbaSSebastian Huber  */
1343ae750fbaSSebastian Huber static void
recalculate_scaling_factor_and_large_delta(struct timehands * th)1344ae750fbaSSebastian Huber recalculate_scaling_factor_and_large_delta(struct timehands *th)
1345ae750fbaSSebastian Huber {
1346ae750fbaSSebastian Huber 	uint64_t scale;
1347ae750fbaSSebastian Huber 
1348ae750fbaSSebastian Huber 	scale = (uint64_t)1 << 63;
1349ae750fbaSSebastian Huber 	scale += (th->th_adjustment / 1024) * 2199;
1350ae750fbaSSebastian Huber 	scale /= th->th_counter->tc_frequency;
1351ae750fbaSSebastian Huber 	th->th_scale = scale * 2;
1352ae750fbaSSebastian Huber 	th->th_large_delta = MIN(((uint64_t)1 << 63) / scale, UINT_MAX);
1353ae750fbaSSebastian Huber }
1354ae750fbaSSebastian Huber 
1355ae750fbaSSebastian Huber /*
135639acc78aSPoul-Henning Kamp  * Initialize the next struct timehands in the ring and make
13576b00cf46SPoul-Henning Kamp  * it the active timehands.  Along the way we might switch to a different
13586b00cf46SPoul-Henning Kamp  * timecounter and/or do seconds processing in NTP.  Slightly magic.
13596b00cf46SPoul-Henning Kamp  */
13609e1b5510SPoul-Henning Kamp static void
tc_windup(struct bintime * new_boottimebin)13615760b029SKonstantin Belousov tc_windup(struct bintime *new_boottimebin)
13627ec73f64SPoul-Henning Kamp {
13632028c0cdSPoul-Henning Kamp 	struct bintime bt;
136426f76aeaSMark Johnston 	struct timecounter *tc;
136539acc78aSPoul-Henning Kamp 	struct timehands *th, *tho;
136639acc78aSPoul-Henning Kamp 	u_int delta, ncount, ogen;
136739acc78aSPoul-Henning Kamp 	int i;
13684f2073fbSWarner Losh 	time_t t;
13697ec73f64SPoul-Henning Kamp 
137039acc78aSPoul-Henning Kamp 	/*
1371f4b5a972SKonstantin Belousov 	 * Make the next timehands a copy of the current one, but do
1372f4b5a972SKonstantin Belousov 	 * not overwrite the generation or next pointer.  While we
1373f4b5a972SKonstantin Belousov 	 * update the contents, the generation must be zero.  We need
1374f4b5a972SKonstantin Belousov 	 * to ensure that the zero generation is visible before the
1375f4b5a972SKonstantin Belousov 	 * data updates become visible, which requires release fence.
1376f4b5a972SKonstantin Belousov 	 * For similar reasons, re-reading of the generation after the
1377f4b5a972SKonstantin Belousov 	 * data is read should use acquire fence.
13786b00cf46SPoul-Henning Kamp 	 */
13796b00cf46SPoul-Henning Kamp 	tho = timehands;
13806b00cf46SPoul-Henning Kamp 	th = tho->th_next;
13816b00cf46SPoul-Henning Kamp 	ogen = th->th_generation;
1382f4b5a972SKonstantin Belousov 	th->th_generation = 0;
1383f4b5a972SKonstantin Belousov 	atomic_thread_fence_rel();
13845ec2c936SMateusz Guzik 	memcpy(th, tho, offsetof(struct timehands, th_generation));
13855760b029SKonstantin Belousov 	if (new_boottimebin != NULL)
13865760b029SKonstantin Belousov 		th->th_boottime = *new_boottimebin;
13876b00cf46SPoul-Henning Kamp 
138839acc78aSPoul-Henning Kamp 	/*
13896b00cf46SPoul-Henning Kamp 	 * Capture a timecounter delta on the current timecounter and if
13906b00cf46SPoul-Henning Kamp 	 * changing timecounters, a counter value from the new timecounter.
13916b00cf46SPoul-Henning Kamp 	 * Update the offset fields accordingly.
13926b00cf46SPoul-Henning Kamp 	 */
139326f76aeaSMark Johnston 	tc = atomic_load_ptr(&timecounter);
13946b00cf46SPoul-Henning Kamp 	delta = tc_delta(th);
139526f76aeaSMark Johnston 	if (th->th_counter != tc)
139626f76aeaSMark Johnston 		ncount = tc->tc_get_timecount(tc);
139739acc78aSPoul-Henning Kamp 	else
139839acc78aSPoul-Henning Kamp 		ncount = 0;
1399b0fdc837SLawrence Stewart #ifdef FFCLOCK
1400b0fdc837SLawrence Stewart 	ffclock_windup(delta);
1401b0fdc837SLawrence Stewart #endif
14026b00cf46SPoul-Henning Kamp 	th->th_offset_count += delta;
14036b00cf46SPoul-Henning Kamp 	th->th_offset_count &= th->th_counter->tc_counter_mask;
14043d9d64aaSAndriy Gapon 	bintime_add_tc_delta(&th->th_offset, th->th_scale,
14053d9d64aaSAndriy Gapon 	    th->th_large_delta, delta);
14066b00cf46SPoul-Henning Kamp 
140739acc78aSPoul-Henning Kamp 	/*
14086b00cf46SPoul-Henning Kamp 	 * Hardware latching timecounters may not generate interrupts on
14096b00cf46SPoul-Henning Kamp 	 * PPS events, so instead we poll them.  There is a finite risk that
14106b00cf46SPoul-Henning Kamp 	 * the hardware might capture a count which is later than the one we
14116b00cf46SPoul-Henning Kamp 	 * got above, and therefore possibly in the next NTP second which might
14126b00cf46SPoul-Henning Kamp 	 * have a different rate than the current NTP second.  It doesn't
14136b00cf46SPoul-Henning Kamp 	 * matter in practice.
14146b00cf46SPoul-Henning Kamp 	 */
14156b00cf46SPoul-Henning Kamp 	if (tho->th_counter->tc_poll_pps)
14166b00cf46SPoul-Henning Kamp 		tho->th_counter->tc_poll_pps(tho->th_counter);
14176b00cf46SPoul-Henning Kamp 
141839acc78aSPoul-Henning Kamp 	/*
1419ae750fbaSSebastian Huber 	 * Deal with NTP second processing.  The loop normally
1420c1cccd1eSWarner Losh 	 * iterates at most once, but in extreme situations it might
1421c1cccd1eSWarner Losh 	 * keep NTP sane if timeouts are not run for several seconds.
1422c1cccd1eSWarner Losh 	 * At boot, the time step can be large when the TOD hardware
1423c1cccd1eSWarner Losh 	 * has been read, so on really large steps, we call
1424c1cccd1eSWarner Losh 	 * ntp_update_second only twice.  We need to call it twice in
1425c1cccd1eSWarner Losh 	 * case we missed a leap second.
14264f2073fbSWarner Losh 	 */
14274f2073fbSWarner Losh 	bt = th->th_offset;
14285760b029SKonstantin Belousov 	bintime_add(&bt, &th->th_boottime);
142945cc9f5fSWarner Losh 	i = bt.sec - tho->th_microtime.tv_sec;
1430ae750fbaSSebastian Huber 	if (i > 0) {
143145cc9f5fSWarner Losh 		if (i > LARGE_STEP)
143245cc9f5fSWarner Losh 			i = 2;
1433ae750fbaSSebastian Huber 
1434ae750fbaSSebastian Huber 		do {
14354f2073fbSWarner Losh 			t = bt.sec;
14364f2073fbSWarner Losh 			ntp_update_second(&th->th_adjustment, &bt.sec);
14374f2073fbSWarner Losh 			if (bt.sec != t)
14385760b029SKonstantin Belousov 				th->th_boottime.sec += bt.sec - t;
1439ae750fbaSSebastian Huber 			--i;
1440ae750fbaSSebastian Huber 		} while (i > 0);
1441ae750fbaSSebastian Huber 
1442ae750fbaSSebastian Huber 		recalculate_scaling_factor_and_large_delta(th);
14434f2073fbSWarner Losh 	}
1444ae750fbaSSebastian Huber 
1445c1cccd1eSWarner Losh 	/* Update the UTC timestamps used by the get*() functions. */
144670e3b262SKonstantin Belousov 	th->th_bintime = bt;
1447c1cccd1eSWarner Losh 	bintime2timeval(&bt, &th->th_microtime);
1448c1cccd1eSWarner Losh 	bintime2timespec(&bt, &th->th_nanotime);
14496b00cf46SPoul-Henning Kamp 
14506b00cf46SPoul-Henning Kamp 	/* Now is a good time to change timecounters. */
145126f76aeaSMark Johnston 	if (th->th_counter != tc) {
145208e1b4f4SJung-uk Kim #ifndef __arm__
145326f76aeaSMark Johnston 		if ((tc->tc_flags & TC_FLAGS_C2STOP) != 0)
145492597e06SJohn Baldwin 			cpu_disable_c2_sleep++;
145592597e06SJohn Baldwin 		if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
145692597e06SJohn Baldwin 			cpu_disable_c2_sleep--;
145708e1b4f4SJung-uk Kim #endif
145826f76aeaSMark Johnston 		th->th_counter = tc;
14596b00cf46SPoul-Henning Kamp 		th->th_offset_count = ncount;
146026f76aeaSMark Johnston 		tc_min_ticktock_freq = max(1, tc->tc_frequency /
146126f76aeaSMark Johnston 		    (((uint64_t)tc->tc_counter_mask + 1) / 3));
1462ae750fbaSSebastian Huber 		recalculate_scaling_factor_and_large_delta(th);
1463b0fdc837SLawrence Stewart #ifdef FFCLOCK
1464b0fdc837SLawrence Stewart 		ffclock_change_tc(th);
1465b0fdc837SLawrence Stewart #endif
14667ec73f64SPoul-Henning Kamp 	}
14677ec73f64SPoul-Henning Kamp 
146839acc78aSPoul-Henning Kamp 	/*
146939acc78aSPoul-Henning Kamp 	 * Now that the struct timehands is again consistent, set the new
14706b00cf46SPoul-Henning Kamp 	 * generation number, making sure to not make it zero.
14716b00cf46SPoul-Henning Kamp 	 */
14726b00cf46SPoul-Henning Kamp 	if (++ogen == 0)
147339acc78aSPoul-Henning Kamp 		ogen = 1;
1474f4b5a972SKonstantin Belousov 	atomic_store_rel_int(&th->th_generation, ogen);
14756b00cf46SPoul-Henning Kamp 
147639acc78aSPoul-Henning Kamp 	/* Go live with the new struct timehands. */
14779bce0f05SLawrence Stewart #ifdef FFCLOCK
14789bce0f05SLawrence Stewart 	switch (sysclock_active) {
14799bce0f05SLawrence Stewart 	case SYSCLOCK_FBCK:
14809bce0f05SLawrence Stewart #endif
14816b00cf46SPoul-Henning Kamp 		time_second = th->th_microtime.tv_sec;
148238b0884cSPoul-Henning Kamp 		time_uptime = th->th_offset.sec;
14839bce0f05SLawrence Stewart #ifdef FFCLOCK
14849bce0f05SLawrence Stewart 		break;
14859bce0f05SLawrence Stewart 	case SYSCLOCK_FFWD:
14869bce0f05SLawrence Stewart 		time_second = fftimehands->tick_time_lerp.sec;
14879bce0f05SLawrence Stewart 		time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
14889bce0f05SLawrence Stewart 		break;
14899bce0f05SLawrence Stewart 	}
14909bce0f05SLawrence Stewart #endif
14919bce0f05SLawrence Stewart 
14926b00cf46SPoul-Henning Kamp 	timehands = th;
149321c295efSKonstantin Belousov 	timekeep_push_vdso();
14946b00cf46SPoul-Henning Kamp }
14956b00cf46SPoul-Henning Kamp 
149639acc78aSPoul-Henning Kamp /* Report or change the active timecounter hardware. */
14976b6ef746SBruce Evans static int
sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)149882d9ae4eSPoul-Henning Kamp sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
14996b6ef746SBruce Evans {
15006b6ef746SBruce Evans 	char newname[32];
15016b6ef746SBruce Evans 	struct timecounter *newtc, *tc;
15026b6ef746SBruce Evans 	int error;
15036b6ef746SBruce Evans 
1504621fd9dcSMark Johnston 	mtx_lock(&tc_lock);
150562efba6aSPoul-Henning Kamp 	tc = timecounter;
1506e80fb434SRobert Drehmel 	strlcpy(newname, tc->tc_name, sizeof(newname));
1507621fd9dcSMark Johnston 	mtx_unlock(&tc_lock);
1508e80fb434SRobert Drehmel 
15096b6ef746SBruce Evans 	error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1510e8bac3f2SIan Lepore 	if (error != 0 || req->newptr == NULL)
151162efba6aSPoul-Henning Kamp 		return (error);
1512621fd9dcSMark Johnston 
1513621fd9dcSMark Johnston 	mtx_lock(&tc_lock);
1514e8bac3f2SIan Lepore 	/* Record that the tc in use now was specifically chosen. */
1515e8bac3f2SIan Lepore 	tc_chosen = 1;
1516621fd9dcSMark Johnston 	if (strcmp(newname, tc->tc_name) == 0) {
1517621fd9dcSMark Johnston 		mtx_unlock(&tc_lock);
1518e8bac3f2SIan Lepore 		return (0);
1519621fd9dcSMark Johnston 	}
152062efba6aSPoul-Henning Kamp 	for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
152139acc78aSPoul-Henning Kamp 		if (strcmp(newname, newtc->tc_name) != 0)
152262efba6aSPoul-Henning Kamp 			continue;
152339acc78aSPoul-Henning Kamp 
15246b6ef746SBruce Evans 		/* Warm up new timecounter. */
15256b6ef746SBruce Evans 		(void)newtc->tc_get_timecount(newtc);
152639acc78aSPoul-Henning Kamp 
152762efba6aSPoul-Henning Kamp 		timecounter = newtc;
1528d1b1b600SNeel Natu 
1529d1b1b600SNeel Natu 		/*
1530d1b1b600SNeel Natu 		 * The vdso timehands update is deferred until the next
1531d1b1b600SNeel Natu 		 * 'tc_windup()'.
1532d1b1b600SNeel Natu 		 *
1533d1b1b600SNeel Natu 		 * This is prudent given that 'timekeep_push_vdso()' does not
1534d1b1b600SNeel Natu 		 * use any locking and that it can be called in hard interrupt
1535d1b1b600SNeel Natu 		 * context via 'tc_windup()'.
1536d1b1b600SNeel Natu 		 */
1537621fd9dcSMark Johnston 		break;
15386b6ef746SBruce Evans 	}
1539621fd9dcSMark Johnston 	mtx_unlock(&tc_lock);
1540621fd9dcSMark Johnston 	return (newtc != NULL ? 0 : EINVAL);
15416b6ef746SBruce Evans }
15427029da5cSPawel Biernacki SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware,
154356b9bee6SKonstantin Belousov     CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, 0, 0,
15447029da5cSPawel Biernacki     sysctl_kern_timecounter_hardware, "A",
1545b389be97SRebecca Cran     "Timecounter hardware selected");
15466b6ef746SBruce Evans 
1547e8bac3f2SIan Lepore /* Report the available timecounter hardware. */
154878a49a45SPoul-Henning Kamp static int
sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)154978a49a45SPoul-Henning Kamp sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
155078a49a45SPoul-Henning Kamp {
155191d9eda2SIan Lepore 	struct sbuf sb;
155278a49a45SPoul-Henning Kamp 	struct timecounter *tc;
155378a49a45SPoul-Henning Kamp 	int error;
155478a49a45SPoul-Henning Kamp 
1555621fd9dcSMark Johnston 	error = sysctl_wire_old_buffer(req, 0);
1556621fd9dcSMark Johnston 	if (error != 0)
1557621fd9dcSMark Johnston 		return (error);
155891d9eda2SIan Lepore 	sbuf_new_for_sysctl(&sb, NULL, 0, req);
1559621fd9dcSMark Johnston 	mtx_lock(&tc_lock);
156091d9eda2SIan Lepore 	for (tc = timecounters; tc != NULL; tc = tc->tc_next) {
156191d9eda2SIan Lepore 		if (tc != timecounters)
156291d9eda2SIan Lepore 			sbuf_putc(&sb, ' ');
156391d9eda2SIan Lepore 		sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality);
156478a49a45SPoul-Henning Kamp 	}
1565621fd9dcSMark Johnston 	mtx_unlock(&tc_lock);
156691d9eda2SIan Lepore 	error = sbuf_finish(&sb);
156791d9eda2SIan Lepore 	sbuf_delete(&sb);
156878a49a45SPoul-Henning Kamp 	return (error);
156978a49a45SPoul-Henning Kamp }
157078a49a45SPoul-Henning Kamp 
15717029da5cSPawel Biernacki SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice,
15727029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
15737029da5cSPawel Biernacki     sysctl_kern_timecounter_choice, "A",
15747029da5cSPawel Biernacki     "Timecounter hardware detected");
157578a49a45SPoul-Henning Kamp 
157639acc78aSPoul-Henning Kamp /*
15776b00cf46SPoul-Henning Kamp  * RFC 2783 PPS-API implementation.
15786b00cf46SPoul-Henning Kamp  */
15797ec73f64SPoul-Henning Kamp 
158028315e27SIan Lepore /*
158128315e27SIan Lepore  *  Return true if the driver is aware of the abi version extensions in the
158228315e27SIan Lepore  *  pps_state structure, and it supports at least the given abi version number.
158328315e27SIan Lepore  */
158428315e27SIan Lepore static inline int
abi_aware(struct pps_state * pps,int vers)158528315e27SIan Lepore abi_aware(struct pps_state *pps, int vers)
158628315e27SIan Lepore {
158728315e27SIan Lepore 
158828315e27SIan Lepore 	return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers);
158928315e27SIan Lepore }
159028315e27SIan Lepore 
1591a1137de9SIan Lepore static int
pps_fetch(struct pps_fetch_args * fapi,struct pps_state * pps)1592a1137de9SIan Lepore pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1593a1137de9SIan Lepore {
1594a1137de9SIan Lepore 	int err, timo;
1595a1137de9SIan Lepore 	pps_seq_t aseq, cseq;
1596a1137de9SIan Lepore 	struct timeval tv;
1597a1137de9SIan Lepore 
1598a1137de9SIan Lepore 	if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1599a1137de9SIan Lepore 		return (EINVAL);
1600a1137de9SIan Lepore 
1601a1137de9SIan Lepore 	/*
1602a1137de9SIan Lepore 	 * If no timeout is requested, immediately return whatever values were
1603a1137de9SIan Lepore 	 * most recently captured.  If timeout seconds is -1, that's a request
1604a1137de9SIan Lepore 	 * to block without a timeout.  WITNESS won't let us sleep forever
1605a1137de9SIan Lepore 	 * without a lock (we really don't need a lock), so just repeatedly
1606a1137de9SIan Lepore 	 * sleep a long time.
1607a1137de9SIan Lepore 	 */
1608a1137de9SIan Lepore 	if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1609a1137de9SIan Lepore 		if (fapi->timeout.tv_sec == -1)
1610a1137de9SIan Lepore 			timo = 0x7fffffff;
1611a1137de9SIan Lepore 		else {
1612a1137de9SIan Lepore 			tv.tv_sec = fapi->timeout.tv_sec;
1613a1137de9SIan Lepore 			tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1614a1137de9SIan Lepore 			timo = tvtohz(&tv);
1615a1137de9SIan Lepore 		}
16166f697994SKonstantin Belousov 		aseq = atomic_load_int(&pps->ppsinfo.assert_sequence);
16176f697994SKonstantin Belousov 		cseq = atomic_load_int(&pps->ppsinfo.clear_sequence);
16186f697994SKonstantin Belousov 		while (aseq == atomic_load_int(&pps->ppsinfo.assert_sequence) &&
16196f697994SKonstantin Belousov 		    cseq == atomic_load_int(&pps->ppsinfo.clear_sequence)) {
162028315e27SIan Lepore 			if (abi_aware(pps, 1) && pps->driver_mtx != NULL) {
162128315e27SIan Lepore 				if (pps->flags & PPSFLAG_MTX_SPIN) {
162228315e27SIan Lepore 					err = msleep_spin(pps, pps->driver_mtx,
162328315e27SIan Lepore 					    "ppsfch", timo);
162428315e27SIan Lepore 				} else {
162528315e27SIan Lepore 					err = msleep(pps, pps->driver_mtx, PCATCH,
162628315e27SIan Lepore 					    "ppsfch", timo);
162728315e27SIan Lepore 				}
162828315e27SIan Lepore 			} else {
1629a1137de9SIan Lepore 				err = tsleep(pps, PCATCH, "ppsfch", timo);
163028315e27SIan Lepore 			}
16316f7a9f7cSIan Lepore 			if (err == EWOULDBLOCK) {
16326f7a9f7cSIan Lepore 				if (fapi->timeout.tv_sec == -1) {
1633a1137de9SIan Lepore 					continue;
16346f7a9f7cSIan Lepore 				} else {
16356f7a9f7cSIan Lepore 					return (ETIMEDOUT);
16366f7a9f7cSIan Lepore 				}
1637a1137de9SIan Lepore 			} else if (err != 0) {
1638a1137de9SIan Lepore 				return (err);
1639a1137de9SIan Lepore 			}
1640a1137de9SIan Lepore 		}
1641a1137de9SIan Lepore 	}
1642a1137de9SIan Lepore 
1643a1137de9SIan Lepore 	pps->ppsinfo.current_mode = pps->ppsparam.mode;
1644a1137de9SIan Lepore 	fapi->pps_info_buf = pps->ppsinfo;
1645a1137de9SIan Lepore 
1646a1137de9SIan Lepore 	return (0);
1647a1137de9SIan Lepore }
1648a1137de9SIan Lepore 
164932c20357SPoul-Henning Kamp int
pps_ioctl(u_long cmd,caddr_t data,struct pps_state * pps)165032c20357SPoul-Henning Kamp pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
165132c20357SPoul-Henning Kamp {
165232c20357SPoul-Henning Kamp 	pps_params_t *app;
1653b7424f2dSJohn Hay 	struct pps_fetch_args *fapi;
165465e359a1SLawrence Stewart #ifdef FFCLOCK
165565e359a1SLawrence Stewart 	struct pps_fetch_ffc_args *fapi_ffc;
165665e359a1SLawrence Stewart #endif
1657de3f8889SPeter Wemm #ifdef PPS_SYNC
1658b7424f2dSJohn Hay 	struct pps_kcbind_args *kapi;
1659de3f8889SPeter Wemm #endif
166032c20357SPoul-Henning Kamp 
1661d8e8b675SPoul-Henning Kamp 	KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
166232c20357SPoul-Henning Kamp 	switch (cmd) {
166332c20357SPoul-Henning Kamp 	case PPS_IOC_CREATE:
166432c20357SPoul-Henning Kamp 		return (0);
166532c20357SPoul-Henning Kamp 	case PPS_IOC_DESTROY:
166632c20357SPoul-Henning Kamp 		return (0);
166732c20357SPoul-Henning Kamp 	case PPS_IOC_SETPARAMS:
166832c20357SPoul-Henning Kamp 		app = (pps_params_t *)data;
166932c20357SPoul-Henning Kamp 		if (app->mode & ~pps->ppscap)
167032c20357SPoul-Henning Kamp 			return (EINVAL);
167165e359a1SLawrence Stewart #ifdef FFCLOCK
167265e359a1SLawrence Stewart 		/* Ensure only a single clock is selected for ffc timestamp. */
167365e359a1SLawrence Stewart 		if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
167465e359a1SLawrence Stewart 			return (EINVAL);
167565e359a1SLawrence Stewart #endif
167632c20357SPoul-Henning Kamp 		pps->ppsparam = *app;
167732c20357SPoul-Henning Kamp 		return (0);
167832c20357SPoul-Henning Kamp 	case PPS_IOC_GETPARAMS:
167932c20357SPoul-Henning Kamp 		app = (pps_params_t *)data;
168032c20357SPoul-Henning Kamp 		*app = pps->ppsparam;
1681b7424f2dSJohn Hay 		app->api_version = PPS_API_VERS_1;
168232c20357SPoul-Henning Kamp 		return (0);
168332c20357SPoul-Henning Kamp 	case PPS_IOC_GETCAP:
168432c20357SPoul-Henning Kamp 		*(int*)data = pps->ppscap;
168532c20357SPoul-Henning Kamp 		return (0);
168632c20357SPoul-Henning Kamp 	case PPS_IOC_FETCH:
1687b7424f2dSJohn Hay 		fapi = (struct pps_fetch_args *)data;
1688a1137de9SIan Lepore 		return (pps_fetch(fapi, pps));
168965e359a1SLawrence Stewart #ifdef FFCLOCK
169065e359a1SLawrence Stewart 	case PPS_IOC_FETCH_FFCOUNTER:
169165e359a1SLawrence Stewart 		fapi_ffc = (struct pps_fetch_ffc_args *)data;
169265e359a1SLawrence Stewart 		if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
169365e359a1SLawrence Stewart 		    PPS_TSFMT_TSPEC)
169465e359a1SLawrence Stewart 			return (EINVAL);
169565e359a1SLawrence Stewart 		if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
169665e359a1SLawrence Stewart 			return (EOPNOTSUPP);
169765e359a1SLawrence Stewart 		pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
169865e359a1SLawrence Stewart 		fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
169965e359a1SLawrence Stewart 		/* Overwrite timestamps if feedback clock selected. */
170065e359a1SLawrence Stewart 		switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
170165e359a1SLawrence Stewart 		case PPS_TSCLK_FBCK:
170265e359a1SLawrence Stewart 			fapi_ffc->pps_info_buf_ffc.assert_timestamp =
170365e359a1SLawrence Stewart 			    pps->ppsinfo.assert_timestamp;
170465e359a1SLawrence Stewart 			fapi_ffc->pps_info_buf_ffc.clear_timestamp =
170565e359a1SLawrence Stewart 			    pps->ppsinfo.clear_timestamp;
170665e359a1SLawrence Stewart 			break;
170765e359a1SLawrence Stewart 		case PPS_TSCLK_FFWD:
170865e359a1SLawrence Stewart 			break;
170965e359a1SLawrence Stewart 		default:
171065e359a1SLawrence Stewart 			break;
171165e359a1SLawrence Stewart 		}
171265e359a1SLawrence Stewart 		return (0);
171365e359a1SLawrence Stewart #endif /* FFCLOCK */
1714b7424f2dSJohn Hay 	case PPS_IOC_KCBIND:
1715b7424f2dSJohn Hay #ifdef PPS_SYNC
1716b7424f2dSJohn Hay 		kapi = (struct pps_kcbind_args *)data;
1717b7424f2dSJohn Hay 		/* XXX Only root should be able to do this */
1718b7424f2dSJohn Hay 		if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1719b7424f2dSJohn Hay 			return (EINVAL);
1720b7424f2dSJohn Hay 		if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1721b7424f2dSJohn Hay 			return (EINVAL);
1722b7424f2dSJohn Hay 		if (kapi->edge & ~pps->ppscap)
1723b7424f2dSJohn Hay 			return (EINVAL);
172428315e27SIan Lepore 		pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) |
172528315e27SIan Lepore 		    (pps->kcmode & KCMODE_ABIFLAG);
1726b7424f2dSJohn Hay 		return (0);
1727b7424f2dSJohn Hay #else
1728b7424f2dSJohn Hay 		return (EOPNOTSUPP);
1729b7424f2dSJohn Hay #endif
173032c20357SPoul-Henning Kamp 	default:
1731f8385624SPoul-Henning Kamp 		return (ENOIOCTL);
173232c20357SPoul-Henning Kamp 	}
173332c20357SPoul-Henning Kamp }
173432c20357SPoul-Henning Kamp 
173532c20357SPoul-Henning Kamp void
pps_init(struct pps_state * pps)173632c20357SPoul-Henning Kamp pps_init(struct pps_state *pps)
173732c20357SPoul-Henning Kamp {
1738a1137de9SIan Lepore 	pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
173932c20357SPoul-Henning Kamp 	if (pps->ppscap & PPS_CAPTUREASSERT)
174032c20357SPoul-Henning Kamp 		pps->ppscap |= PPS_OFFSETASSERT;
174132c20357SPoul-Henning Kamp 	if (pps->ppscap & PPS_CAPTURECLEAR)
174232c20357SPoul-Henning Kamp 		pps->ppscap |= PPS_OFFSETCLEAR;
174365e359a1SLawrence Stewart #ifdef FFCLOCK
174465e359a1SLawrence Stewart 	pps->ppscap |= PPS_TSCLK_MASK;
174565e359a1SLawrence Stewart #endif
174628315e27SIan Lepore 	pps->kcmode &= ~KCMODE_ABIFLAG;
174728315e27SIan Lepore }
174828315e27SIan Lepore 
174928315e27SIan Lepore void
pps_init_abi(struct pps_state * pps)175028315e27SIan Lepore pps_init_abi(struct pps_state *pps)
175128315e27SIan Lepore {
175228315e27SIan Lepore 
175328315e27SIan Lepore 	pps_init(pps);
175428315e27SIan Lepore 	if (pps->driver_abi > 0) {
175528315e27SIan Lepore 		pps->kcmode |= KCMODE_ABIFLAG;
175628315e27SIan Lepore 		pps->kernel_abi = PPS_ABI_VERSION;
175728315e27SIan Lepore 	}
175832c20357SPoul-Henning Kamp }
175932c20357SPoul-Henning Kamp 
176032c20357SPoul-Henning Kamp void
pps_capture(struct pps_state * pps)17617bf758bfSPoul-Henning Kamp pps_capture(struct pps_state *pps)
17627bf758bfSPoul-Henning Kamp {
17636b00cf46SPoul-Henning Kamp 	struct timehands *th;
1764cb2a028bSSebastian Huber 	struct timecounter *tc;
17657bf758bfSPoul-Henning Kamp 
1766d8e8b675SPoul-Henning Kamp 	KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
17676b00cf46SPoul-Henning Kamp 	th = timehands;
1768f4b5a972SKonstantin Belousov 	pps->capgen = atomic_load_acq_int(&th->th_generation);
17696b00cf46SPoul-Henning Kamp 	pps->capth = th;
177065e359a1SLawrence Stewart #ifdef FFCLOCK
177165e359a1SLawrence Stewart 	pps->capffth = fftimehands;
177265e359a1SLawrence Stewart #endif
1773cb2a028bSSebastian Huber 	tc = th->th_counter;
1774cb2a028bSSebastian Huber 	pps->capcount = tc->tc_get_timecount(tc);
17757bf758bfSPoul-Henning Kamp }
17767bf758bfSPoul-Henning Kamp 
17777bf758bfSPoul-Henning Kamp void
pps_event(struct pps_state * pps,int event)17787bf758bfSPoul-Henning Kamp pps_event(struct pps_state *pps, int event)
177932c20357SPoul-Henning Kamp {
1780fd88f4e1SSebastian Huber 	struct timehands *capth;
1781fd88f4e1SSebastian Huber 	struct timecounter *captc;
1782fd88f4e1SSebastian Huber 	uint64_t capth_scale;
178339acc78aSPoul-Henning Kamp 	struct bintime bt;
17841e48d9d3SSebastian Huber 	struct timespec *tsp, *osp;
17856b00cf46SPoul-Henning Kamp 	u_int tcount, *pcount;
1786aaca7045SEnji Cooper 	int foff;
178732c20357SPoul-Henning Kamp 	pps_seq_t *pseq;
178865e359a1SLawrence Stewart #ifdef FFCLOCK
178965e359a1SLawrence Stewart 	struct timespec *tsp_ffc;
179065e359a1SLawrence Stewart 	pps_seq_t *pseq_ffc;
179165e359a1SLawrence Stewart 	ffcounter *ffcount;
179265e359a1SLawrence Stewart #endif
1793aaca7045SEnji Cooper #ifdef PPS_SYNC
1794aaca7045SEnji Cooper 	int fhard;
1795aaca7045SEnji Cooper #endif
179632c20357SPoul-Henning Kamp 
1797d8e8b675SPoul-Henning Kamp 	KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1798721b5817SIan Lepore 	/* Nothing to do if not currently set to capture this event type. */
1799721b5817SIan Lepore 	if ((event & pps->ppsparam.mode) == 0)
1800721b5817SIan Lepore 		return;
1801fd88f4e1SSebastian Huber 
1802fd88f4e1SSebastian Huber 	/* Make a snapshot of the captured timehand */
1803fd88f4e1SSebastian Huber 	capth = pps->capth;
1804fd88f4e1SSebastian Huber 	captc = capth->th_counter;
1805fd88f4e1SSebastian Huber 	capth_scale = capth->th_scale;
1806fd88f4e1SSebastian Huber 	tcount = capth->th_offset_count;
1807fd88f4e1SSebastian Huber 	bt = capth->th_bintime;
1808fd88f4e1SSebastian Huber 
180939acc78aSPoul-Henning Kamp 	/* If the timecounter was wound up underneath us, bail out. */
1810fd88f4e1SSebastian Huber 	atomic_thread_fence_acq();
1811fd88f4e1SSebastian Huber 	if (pps->capgen == 0 || pps->capgen != capth->th_generation)
18127bf758bfSPoul-Henning Kamp 		return;
18137bf758bfSPoul-Henning Kamp 
181439acc78aSPoul-Henning Kamp 	/* Things would be easier with arrays. */
181532c20357SPoul-Henning Kamp 	if (event == PPS_CAPTUREASSERT) {
181632c20357SPoul-Henning Kamp 		tsp = &pps->ppsinfo.assert_timestamp;
181732c20357SPoul-Henning Kamp 		osp = &pps->ppsparam.assert_offset;
181832c20357SPoul-Henning Kamp 		foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1819aaca7045SEnji Cooper #ifdef PPS_SYNC
1820b7424f2dSJohn Hay 		fhard = pps->kcmode & PPS_CAPTUREASSERT;
1821aaca7045SEnji Cooper #endif
182232c20357SPoul-Henning Kamp 		pcount = &pps->ppscount[0];
182332c20357SPoul-Henning Kamp 		pseq = &pps->ppsinfo.assert_sequence;
182465e359a1SLawrence Stewart #ifdef FFCLOCK
182565e359a1SLawrence Stewart 		ffcount = &pps->ppsinfo_ffc.assert_ffcount;
182665e359a1SLawrence Stewart 		tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
182765e359a1SLawrence Stewart 		pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
182865e359a1SLawrence Stewart #endif
182932c20357SPoul-Henning Kamp 	} else {
183032c20357SPoul-Henning Kamp 		tsp = &pps->ppsinfo.clear_timestamp;
183132c20357SPoul-Henning Kamp 		osp = &pps->ppsparam.clear_offset;
183232c20357SPoul-Henning Kamp 		foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1833aaca7045SEnji Cooper #ifdef PPS_SYNC
1834b7424f2dSJohn Hay 		fhard = pps->kcmode & PPS_CAPTURECLEAR;
1835aaca7045SEnji Cooper #endif
183632c20357SPoul-Henning Kamp 		pcount = &pps->ppscount[1];
183732c20357SPoul-Henning Kamp 		pseq = &pps->ppsinfo.clear_sequence;
183865e359a1SLawrence Stewart #ifdef FFCLOCK
183965e359a1SLawrence Stewart 		ffcount = &pps->ppsinfo_ffc.clear_ffcount;
184065e359a1SLawrence Stewart 		tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
184165e359a1SLawrence Stewart 		pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
184265e359a1SLawrence Stewart #endif
184332c20357SPoul-Henning Kamp 	}
184432c20357SPoul-Henning Kamp 
18450448501fSSebastian Huber 	*pcount = pps->capcount;
18460448501fSSebastian Huber 
184739acc78aSPoul-Henning Kamp 	/*
18486b00cf46SPoul-Henning Kamp 	 * If the timecounter changed, we cannot compare the count values, so
18496b00cf46SPoul-Henning Kamp 	 * we have to drop the rest of the PPS-stuff until the next event.
18506b00cf46SPoul-Henning Kamp 	 */
1851fd88f4e1SSebastian Huber 	if (__predict_false(pps->ppstc != captc)) {
1852fd88f4e1SSebastian Huber 		pps->ppstc = captc;
18537bf758bfSPoul-Henning Kamp 		pps->ppscount[2] = pps->capcount;
185432c20357SPoul-Henning Kamp 		return;
185532c20357SPoul-Henning Kamp 	}
185632c20357SPoul-Henning Kamp 
18570448501fSSebastian Huber 	(*pseq)++;
18580448501fSSebastian Huber 
185939acc78aSPoul-Henning Kamp 	/* Convert the count to a timespec. */
1860fd88f4e1SSebastian Huber 	tcount = pps->capcount - tcount;
1861fd88f4e1SSebastian Huber 	tcount &= captc->tc_counter_mask;
1862fd88f4e1SSebastian Huber 	bintime_addx(&bt, capth_scale * tcount);
18638a142484SSebastian Huber 	bintime2timespec(&bt, tsp);
186432c20357SPoul-Henning Kamp 
186532c20357SPoul-Henning Kamp 	if (foff) {
18666040822cSAlan Somers 		timespecadd(tsp, osp, tsp);
186732c20357SPoul-Henning Kamp 		if (tsp->tv_nsec < 0) {
186832c20357SPoul-Henning Kamp 			tsp->tv_nsec += 1000000000;
186932c20357SPoul-Henning Kamp 			tsp->tv_sec -= 1;
187032c20357SPoul-Henning Kamp 		}
187132c20357SPoul-Henning Kamp 	}
187265e359a1SLawrence Stewart 
187365e359a1SLawrence Stewart #ifdef FFCLOCK
187465e359a1SLawrence Stewart 	*ffcount = pps->capffth->tick_ffcount + tcount;
187565e359a1SLawrence Stewart 	bt = pps->capffth->tick_time;
187665e359a1SLawrence Stewart 	ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
187765e359a1SLawrence Stewart 	bintime_add(&bt, &pps->capffth->tick_time);
187865e359a1SLawrence Stewart 	(*pseq_ffc)++;
18798a142484SSebastian Huber 	bintime2timespec(&bt, tsp_ffc);
188065e359a1SLawrence Stewart #endif
188165e359a1SLawrence Stewart 
188232c20357SPoul-Henning Kamp #ifdef PPS_SYNC
188332c20357SPoul-Henning Kamp 	if (fhard) {
18841e48d9d3SSebastian Huber 		uint64_t delta_nsec;
188528ed159fSSebastian Huber 		uint64_t freq;
1886ce9fac00SPoul-Henning Kamp 
188739acc78aSPoul-Henning Kamp 		/*
18886b00cf46SPoul-Henning Kamp 		 * Feed the NTP PLL/FLL.
1889b1e7e201SJohn Hay 		 * The FLL wants to know how many (hardware) nanoseconds
1890b1e7e201SJohn Hay 		 * elapsed since the previous event.
18916b00cf46SPoul-Henning Kamp 		 */
18927bf758bfSPoul-Henning Kamp 		tcount = pps->capcount - pps->ppscount[2];
18937bf758bfSPoul-Henning Kamp 		pps->ppscount[2] = pps->capcount;
1894fd88f4e1SSebastian Huber 		tcount &= captc->tc_counter_mask;
18951e48d9d3SSebastian Huber 		delta_nsec = 1000000000;
18961e48d9d3SSebastian Huber 		delta_nsec *= tcount;
189728ed159fSSebastian Huber 		freq = captc->tc_frequency;
189828ed159fSSebastian Huber 		delta_nsec = (delta_nsec + freq / 2) / freq;
18991e48d9d3SSebastian Huber 		hardpps(tsp, (long)delta_nsec);
190032c20357SPoul-Henning Kamp 	}
190132c20357SPoul-Henning Kamp #endif
1902a1137de9SIan Lepore 
1903a1137de9SIan Lepore 	/* Wakeup anyone sleeping in pps_fetch().  */
1904a1137de9SIan Lepore 	wakeup(pps);
190532c20357SPoul-Henning Kamp }
19069e1b5510SPoul-Henning Kamp 
190739acc78aSPoul-Henning Kamp /*
19089e1b5510SPoul-Henning Kamp  * Timecounters need to be updated every so often to prevent the hardware
19099e1b5510SPoul-Henning Kamp  * counter from overflowing.  Updating also recalculates the cached values
19109e1b5510SPoul-Henning Kamp  * used by the get*() family of functions, so their precision depends on
19119e1b5510SPoul-Henning Kamp  * the update frequency.
19129e1b5510SPoul-Henning Kamp  */
19139e1b5510SPoul-Henning Kamp 
19149e1b5510SPoul-Henning Kamp static int tc_tick;
1915b389be97SRebecca Cran SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1916b389be97SRebecca Cran     "Approximate number of hardclock ticks in a millisecond");
19179e1b5510SPoul-Henning Kamp 
1918e7fa55afSPoul-Henning Kamp void
tc_ticktock(int cnt)19190e189873SAlexander Motin tc_ticktock(int cnt)
19209e1b5510SPoul-Henning Kamp {
1921e7fa55afSPoul-Henning Kamp 	static int count;
19229e1b5510SPoul-Henning Kamp 
19235760b029SKonstantin Belousov 	if (mtx_trylock_spin(&tc_setclock_mtx)) {
19240e189873SAlexander Motin 		count += cnt;
19255760b029SKonstantin Belousov 		if (count >= tc_tick) {
1926e7fa55afSPoul-Henning Kamp 			count = 0;
19275760b029SKonstantin Belousov 			tc_windup(NULL);
19285760b029SKonstantin Belousov 		}
19295760b029SKonstantin Belousov 		mtx_unlock_spin(&tc_setclock_mtx);
19305760b029SKonstantin Belousov 	}
19319e1b5510SPoul-Henning Kamp }
19329e1b5510SPoul-Henning Kamp 
19335b999a6bSDavide Italiano static void __inline
tc_adjprecision(void)19345b999a6bSDavide Italiano tc_adjprecision(void)
19355b999a6bSDavide Italiano {
19365b999a6bSDavide Italiano 	int t;
19375b999a6bSDavide Italiano 
19385b999a6bSDavide Italiano 	if (tc_timepercentage > 0) {
19395b999a6bSDavide Italiano 		t = (99 + tc_timepercentage) / tc_timepercentage;
19405b999a6bSDavide Italiano 		tc_precexp = fls(t + (t >> 1)) - 1;
19415b999a6bSDavide Italiano 		FREQ2BT(hz / tc_tick, &bt_timethreshold);
19425b999a6bSDavide Italiano 		FREQ2BT(hz, &bt_tickthreshold);
19435b999a6bSDavide Italiano 		bintime_shift(&bt_timethreshold, tc_precexp);
19445b999a6bSDavide Italiano 		bintime_shift(&bt_tickthreshold, tc_precexp);
19455b999a6bSDavide Italiano 	} else {
19465b999a6bSDavide Italiano 		tc_precexp = 31;
19475b999a6bSDavide Italiano 		bt_timethreshold.sec = INT_MAX;
19485b999a6bSDavide Italiano 		bt_timethreshold.frac = ~(uint64_t)0;
19495b999a6bSDavide Italiano 		bt_tickthreshold = bt_timethreshold;
19505b999a6bSDavide Italiano 	}
19515b999a6bSDavide Italiano 	sbt_timethreshold = bttosbt(bt_timethreshold);
19525b999a6bSDavide Italiano 	sbt_tickthreshold = bttosbt(bt_tickthreshold);
19535b999a6bSDavide Italiano }
19545b999a6bSDavide Italiano 
19555b999a6bSDavide Italiano static int
sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)19565b999a6bSDavide Italiano sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
19575b999a6bSDavide Italiano {
19585b999a6bSDavide Italiano 	int error, val;
19595b999a6bSDavide Italiano 
19605b999a6bSDavide Italiano 	val = tc_timepercentage;
19615b999a6bSDavide Italiano 	error = sysctl_handle_int(oidp, &val, 0, req);
19625b999a6bSDavide Italiano 	if (error != 0 || req->newptr == NULL)
19635b999a6bSDavide Italiano 		return (error);
19645b999a6bSDavide Italiano 	tc_timepercentage = val;
1965af3b2549SHans Petter Selasky 	if (cold)
1966af3b2549SHans Petter Selasky 		goto done;
19675b999a6bSDavide Italiano 	tc_adjprecision();
1968af3b2549SHans Petter Selasky done:
19695b999a6bSDavide Italiano 	return (0);
19705b999a6bSDavide Italiano }
19715b999a6bSDavide Italiano 
19726c46ce7eSKonstantin Belousov /* Set up the requested number of timehands. */
19736c46ce7eSKonstantin Belousov static void
inittimehands(void * dummy)19746c46ce7eSKonstantin Belousov inittimehands(void *dummy)
19756c46ce7eSKonstantin Belousov {
19766c46ce7eSKonstantin Belousov 	struct timehands *thp;
19776c46ce7eSKonstantin Belousov 	int i;
19786c46ce7eSKonstantin Belousov 
19796c46ce7eSKonstantin Belousov 	TUNABLE_INT_FETCH("kern.timecounter.timehands_count",
19806c46ce7eSKonstantin Belousov 	    &timehands_count);
19816c46ce7eSKonstantin Belousov 	if (timehands_count < 1)
19826c46ce7eSKonstantin Belousov 		timehands_count = 1;
19836c46ce7eSKonstantin Belousov 	if (timehands_count > nitems(ths))
19846c46ce7eSKonstantin Belousov 		timehands_count = nitems(ths);
19856c46ce7eSKonstantin Belousov 	for (i = 1, thp = &ths[0]; i < timehands_count;  thp = &ths[i++])
19866c46ce7eSKonstantin Belousov 		thp->th_next = &ths[i];
19876c46ce7eSKonstantin Belousov 	thp->th_next = &ths[0];
198856b9bee6SKonstantin Belousov 
198956b9bee6SKonstantin Belousov 	TUNABLE_STR_FETCH("kern.timecounter.hardware", tc_from_tunable,
199056b9bee6SKonstantin Belousov 	    sizeof(tc_from_tunable));
199133399501SMark Johnston 
199233399501SMark Johnston 	mtx_init(&tc_lock, "tc", NULL, MTX_DEF);
19936c46ce7eSKonstantin Belousov }
19946c46ce7eSKonstantin Belousov SYSINIT(timehands, SI_SUB_TUNABLES, SI_ORDER_ANY, inittimehands, NULL);
19956c46ce7eSKonstantin Belousov 
19969e1b5510SPoul-Henning Kamp static void
inittimecounter(void * dummy)19979e1b5510SPoul-Henning Kamp inittimecounter(void *dummy)
19989e1b5510SPoul-Henning Kamp {
19999e1b5510SPoul-Henning Kamp 	u_int p;
20006c46ce7eSKonstantin Belousov 	int tick_rate;
20019e1b5510SPoul-Henning Kamp 
200239acc78aSPoul-Henning Kamp 	/*
200339acc78aSPoul-Henning Kamp 	 * Set the initial timeout to
200439acc78aSPoul-Henning Kamp 	 * max(1, <approx. number of hardclock ticks in a millisecond>).
200539acc78aSPoul-Henning Kamp 	 * People should probably not use the sysctl to set the timeout
2006e3043798SPedro F. Giffuni 	 * to smaller than its initial value, since that value is the
200739acc78aSPoul-Henning Kamp 	 * smallest reasonable one.  If they want better timestamps they
200839acc78aSPoul-Henning Kamp 	 * should use the non-"get"* functions.
200939acc78aSPoul-Henning Kamp 	 */
20109e1b5510SPoul-Henning Kamp 	if (hz > 1000)
20119e1b5510SPoul-Henning Kamp 		tc_tick = (hz + 500) / 1000;
20129e1b5510SPoul-Henning Kamp 	else
20139e1b5510SPoul-Henning Kamp 		tc_tick = 1;
20145b999a6bSDavide Italiano 	tc_adjprecision();
20155b999a6bSDavide Italiano 	FREQ2BT(hz, &tick_bt);
20165b999a6bSDavide Italiano 	tick_sbt = bttosbt(tick_bt);
20175b999a6bSDavide Italiano 	tick_rate = hz / tc_tick;
20185b999a6bSDavide Italiano 	FREQ2BT(tick_rate, &tc_tick_bt);
20195b999a6bSDavide Italiano 	tc_tick_sbt = bttosbt(tc_tick_bt);
20209e1b5510SPoul-Henning Kamp 	p = (tc_tick * 1000000) / hz;
20219e1b5510SPoul-Henning Kamp 	printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
202239acc78aSPoul-Henning Kamp 
2023b0fdc837SLawrence Stewart #ifdef FFCLOCK
2024b0fdc837SLawrence Stewart 	ffclock_init();
2025b0fdc837SLawrence Stewart #endif
20264b23dec4SKonstantin Belousov 
202748e5da55SPoul-Henning Kamp 	/* warm up new timecounter (again) and get rolling. */
202839acc78aSPoul-Henning Kamp 	(void)timecounter->tc_get_timecount(timecounter);
20295760b029SKonstantin Belousov 	mtx_lock_spin(&tc_setclock_mtx);
20305760b029SKonstantin Belousov 	tc_windup(NULL);
20315760b029SKonstantin Belousov 	mtx_unlock_spin(&tc_setclock_mtx);
20329e1b5510SPoul-Henning Kamp }
20339e1b5510SPoul-Henning Kamp 
2034237fdd78SRobert Watson SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
20355b1a8eb3SPoul-Henning Kamp 
2036e8444a7eSPoul-Henning Kamp /* Cpu tick handling -------------------------------------------------*/
2037e8444a7eSPoul-Henning Kamp 
20388701571dSMitchell Horne static bool cpu_tick_variable;
2039e8444a7eSPoul-Henning Kamp static uint64_t	cpu_tick_frequency;
2040e8444a7eSPoul-Henning Kamp 
20412bf95012SAndrew Turner DPCPU_DEFINE_STATIC(uint64_t, tc_cpu_ticks_base);
20422bf95012SAndrew Turner DPCPU_DEFINE_STATIC(unsigned, tc_cpu_ticks_last);
2043b2557db6SKonstantin Belousov 
204488ca07e7SJohn Baldwin static uint64_t
tc_cpu_ticks(void)20455b1a8eb3SPoul-Henning Kamp tc_cpu_ticks(void)
20465b1a8eb3SPoul-Henning Kamp {
20475b1a8eb3SPoul-Henning Kamp 	struct timecounter *tc;
2048b2557db6SKonstantin Belousov 	uint64_t res, *base;
2049b2557db6SKonstantin Belousov 	unsigned u, *last;
20505b1a8eb3SPoul-Henning Kamp 
2051b2557db6SKonstantin Belousov 	critical_enter();
2052b2557db6SKonstantin Belousov 	base = DPCPU_PTR(tc_cpu_ticks_base);
2053b2557db6SKonstantin Belousov 	last = DPCPU_PTR(tc_cpu_ticks_last);
20545b1a8eb3SPoul-Henning Kamp 	tc = timehands->th_counter;
20555b1a8eb3SPoul-Henning Kamp 	u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
2056b2557db6SKonstantin Belousov 	if (u < *last)
2057b2557db6SKonstantin Belousov 		*base += (uint64_t)tc->tc_counter_mask + 1;
2058b2557db6SKonstantin Belousov 	*last = u;
2059b2557db6SKonstantin Belousov 	res = u + *base;
2060b2557db6SKonstantin Belousov 	critical_exit();
2061b2557db6SKonstantin Belousov 	return (res);
20625b1a8eb3SPoul-Henning Kamp }
20635b1a8eb3SPoul-Henning Kamp 
2064a157e425SAlexander Motin void
cpu_tick_calibration(void)2065a157e425SAlexander Motin cpu_tick_calibration(void)
2066a157e425SAlexander Motin {
2067a157e425SAlexander Motin 	static time_t last_calib;
2068a157e425SAlexander Motin 
2069a157e425SAlexander Motin 	if (time_uptime != last_calib && !(time_uptime & 0xf)) {
2070a157e425SAlexander Motin 		cpu_tick_calibrate(0);
2071a157e425SAlexander Motin 		last_calib = time_uptime;
2072a157e425SAlexander Motin 	}
2073a157e425SAlexander Motin }
2074a157e425SAlexander Motin 
2075e8444a7eSPoul-Henning Kamp /*
20766b4d690cSWarner Losh  * This function gets called every 16 seconds on only one designated
2077a157e425SAlexander Motin  * CPU in the system from hardclock() via cpu_tick_calibration()().
2078e8444a7eSPoul-Henning Kamp  *
2079e8444a7eSPoul-Henning Kamp  * Whenever the real time clock is stepped we get called with reset=1
2080e8444a7eSPoul-Henning Kamp  * to make sure we handle suspend/resume and similar events correctly.
2081e8444a7eSPoul-Henning Kamp  */
2082e8444a7eSPoul-Henning Kamp 
2083e8444a7eSPoul-Henning Kamp static void
cpu_tick_calibrate(int reset)2084e8444a7eSPoul-Henning Kamp cpu_tick_calibrate(int reset)
2085e8444a7eSPoul-Henning Kamp {
2086e8444a7eSPoul-Henning Kamp 	static uint64_t c_last;
2087e8444a7eSPoul-Henning Kamp 	uint64_t c_this, c_delta;
2088e8444a7eSPoul-Henning Kamp 	static struct bintime  t_last;
2089e8444a7eSPoul-Henning Kamp 	struct bintime t_this, t_delta;
2090301af28aSPoul-Henning Kamp 	uint32_t divi;
2091e8444a7eSPoul-Henning Kamp 
2092e8444a7eSPoul-Henning Kamp 	if (reset) {
2093e8444a7eSPoul-Henning Kamp 		/* The clock was stepped, abort & reset */
2094e8444a7eSPoul-Henning Kamp 		t_last.sec = 0;
2095e8444a7eSPoul-Henning Kamp 		return;
2096e8444a7eSPoul-Henning Kamp 	}
2097e8444a7eSPoul-Henning Kamp 
2098e8444a7eSPoul-Henning Kamp 	/* we don't calibrate fixed rate cputicks */
2099e8444a7eSPoul-Henning Kamp 	if (!cpu_tick_variable)
2100e8444a7eSPoul-Henning Kamp 		return;
2101e8444a7eSPoul-Henning Kamp 
2102e8444a7eSPoul-Henning Kamp 	getbinuptime(&t_this);
2103e8444a7eSPoul-Henning Kamp 	c_this = cpu_ticks();
2104e8444a7eSPoul-Henning Kamp 	if (t_last.sec != 0) {
2105e8444a7eSPoul-Henning Kamp 		c_delta = c_this - c_last;
2106e8444a7eSPoul-Henning Kamp 		t_delta = t_this;
2107e8444a7eSPoul-Henning Kamp 		bintime_sub(&t_delta, &t_last);
2108e8444a7eSPoul-Henning Kamp 		/*
2109301af28aSPoul-Henning Kamp 		 * Headroom:
2110301af28aSPoul-Henning Kamp 		 * 	2^(64-20) / 16[s] =
2111301af28aSPoul-Henning Kamp 		 * 	2^(44) / 16[s] =
2112301af28aSPoul-Henning Kamp 		 * 	17.592.186.044.416 / 16 =
2113301af28aSPoul-Henning Kamp 		 * 	1.099.511.627.776 [Hz]
2114301af28aSPoul-Henning Kamp 		 */
2115301af28aSPoul-Henning Kamp 		divi = t_delta.sec << 20;
2116301af28aSPoul-Henning Kamp 		divi |= t_delta.frac >> (64 - 20);
2117301af28aSPoul-Henning Kamp 		c_delta <<= 20;
2118301af28aSPoul-Henning Kamp 		c_delta /= divi;
2119e8444a7eSPoul-Henning Kamp 		if (c_delta > cpu_tick_frequency) {
212059048707SPoul-Henning Kamp 			if (0 && bootverbose)
2121fef527eeSPoul-Henning Kamp 				printf("cpu_tick increased to %ju Hz\n",
21226cda760fSPoul-Henning Kamp 				    c_delta);
2123e8444a7eSPoul-Henning Kamp 			cpu_tick_frequency = c_delta;
2124e8444a7eSPoul-Henning Kamp 		}
2125e8444a7eSPoul-Henning Kamp 	}
2126e8444a7eSPoul-Henning Kamp 	c_last = c_this;
2127e8444a7eSPoul-Henning Kamp 	t_last = t_this;
2128e8444a7eSPoul-Henning Kamp }
2129e8444a7eSPoul-Henning Kamp 
2130e8444a7eSPoul-Henning Kamp void
set_cputicker(cpu_tick_f * func,uint64_t freq,bool isvariable)21318701571dSMitchell Horne set_cputicker(cpu_tick_f *func, uint64_t freq, bool isvariable)
2132e8444a7eSPoul-Henning Kamp {
2133e8444a7eSPoul-Henning Kamp 
2134e8444a7eSPoul-Henning Kamp 	if (func == NULL) {
2135e8444a7eSPoul-Henning Kamp 		cpu_ticks = tc_cpu_ticks;
2136e8444a7eSPoul-Henning Kamp 	} else {
2137e8444a7eSPoul-Henning Kamp 		cpu_tick_frequency = freq;
21388701571dSMitchell Horne 		cpu_tick_variable = isvariable;
2139e8444a7eSPoul-Henning Kamp 		cpu_ticks = func;
2140e8444a7eSPoul-Henning Kamp 	}
2141e8444a7eSPoul-Henning Kamp }
2142e8444a7eSPoul-Henning Kamp 
2143e8444a7eSPoul-Henning Kamp uint64_t
cpu_tickrate(void)2144e8444a7eSPoul-Henning Kamp cpu_tickrate(void)
2145e8444a7eSPoul-Henning Kamp {
2146e8444a7eSPoul-Henning Kamp 
2147e8444a7eSPoul-Henning Kamp 	if (cpu_ticks == tc_cpu_ticks)
2148e8444a7eSPoul-Henning Kamp 		return (tc_getfrequency());
2149e8444a7eSPoul-Henning Kamp 	return (cpu_tick_frequency);
2150e8444a7eSPoul-Henning Kamp }
2151e8444a7eSPoul-Henning Kamp 
2152e8444a7eSPoul-Henning Kamp /*
2153e8444a7eSPoul-Henning Kamp  * We need to be slightly careful converting cputicks to microseconds.
2154e8444a7eSPoul-Henning Kamp  * There is plenty of margin in 64 bits of microseconds (half a million
2155e8444a7eSPoul-Henning Kamp  * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
2156e8444a7eSPoul-Henning Kamp  * before divide conversion (to retain precision) we find that the
2157e8444a7eSPoul-Henning Kamp  * margin shrinks to 1.5 hours (one millionth of 146y).
2158e8444a7eSPoul-Henning Kamp  */
2159e8444a7eSPoul-Henning Kamp 
2160e8444a7eSPoul-Henning Kamp uint64_t
cputick2usec(uint64_t tick)2161e8444a7eSPoul-Henning Kamp cputick2usec(uint64_t tick)
2162e8444a7eSPoul-Henning Kamp {
2163bb53dd56Sfirk 	uint64_t tr;
2164bb53dd56Sfirk 	tr = cpu_tickrate();
2165bb53dd56Sfirk 	return ((tick / tr) * 1000000ULL) + ((tick % tr) * 1000000ULL) / tr;
2166e8444a7eSPoul-Henning Kamp }
2167e8444a7eSPoul-Henning Kamp 
2168e8444a7eSPoul-Henning Kamp cpu_tick_f	*cpu_ticks = tc_cpu_ticks;
2169aea81038SKonstantin Belousov 
2170aea81038SKonstantin Belousov static int vdso_th_enable = 1;
2171aea81038SKonstantin Belousov static int
sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)2172aea81038SKonstantin Belousov sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
2173aea81038SKonstantin Belousov {
2174aea81038SKonstantin Belousov 	int old_vdso_th_enable, error;
2175aea81038SKonstantin Belousov 
2176aea81038SKonstantin Belousov 	old_vdso_th_enable = vdso_th_enable;
2177aea81038SKonstantin Belousov 	error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
2178aea81038SKonstantin Belousov 	if (error != 0)
2179aea81038SKonstantin Belousov 		return (error);
2180aea81038SKonstantin Belousov 	vdso_th_enable = old_vdso_th_enable;
2181aea81038SKonstantin Belousov 	return (0);
2182aea81038SKonstantin Belousov }
2183aea81038SKonstantin Belousov SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
2184aea81038SKonstantin Belousov     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2185aea81038SKonstantin Belousov     NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
2186aea81038SKonstantin Belousov 
2187aea81038SKonstantin Belousov uint32_t
tc_fill_vdso_timehands(struct vdso_timehands * vdso_th)2188aea81038SKonstantin Belousov tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
2189aea81038SKonstantin Belousov {
2190aea81038SKonstantin Belousov 	struct timehands *th;
2191aea81038SKonstantin Belousov 	uint32_t enabled;
2192aea81038SKonstantin Belousov 
2193aea81038SKonstantin Belousov 	th = timehands;
2194aea81038SKonstantin Belousov 	vdso_th->th_scale = th->th_scale;
2195aea81038SKonstantin Belousov 	vdso_th->th_offset_count = th->th_offset_count;
2196aea81038SKonstantin Belousov 	vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
2197aea81038SKonstantin Belousov 	vdso_th->th_offset = th->th_offset;
21985760b029SKonstantin Belousov 	vdso_th->th_boottime = th->th_boottime;
219916808549SKonstantin Belousov 	if (th->th_counter->tc_fill_vdso_timehands != NULL) {
220016808549SKonstantin Belousov 		enabled = th->th_counter->tc_fill_vdso_timehands(vdso_th,
220116808549SKonstantin Belousov 		    th->th_counter);
220216808549SKonstantin Belousov 	} else
220316808549SKonstantin Belousov 		enabled = 0;
2204aea81038SKonstantin Belousov 	if (!vdso_th_enable)
2205aea81038SKonstantin Belousov 		enabled = 0;
2206aea81038SKonstantin Belousov 	return (enabled);
2207aea81038SKonstantin Belousov }
2208aea81038SKonstantin Belousov 
2209aea81038SKonstantin Belousov #ifdef COMPAT_FREEBSD32
2210aea81038SKonstantin Belousov uint32_t
tc_fill_vdso_timehands32(struct vdso_timehands32 * vdso_th32)2211aea81038SKonstantin Belousov tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
2212aea81038SKonstantin Belousov {
2213aea81038SKonstantin Belousov 	struct timehands *th;
2214aea81038SKonstantin Belousov 	uint32_t enabled;
2215aea81038SKonstantin Belousov 
2216aea81038SKonstantin Belousov 	th = timehands;
2217aea81038SKonstantin Belousov 	*(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
2218aea81038SKonstantin Belousov 	vdso_th32->th_offset_count = th->th_offset_count;
2219aea81038SKonstantin Belousov 	vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
2220aea81038SKonstantin Belousov 	vdso_th32->th_offset.sec = th->th_offset.sec;
2221aea81038SKonstantin Belousov 	*(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
22225760b029SKonstantin Belousov 	vdso_th32->th_boottime.sec = th->th_boottime.sec;
22235760b029SKonstantin Belousov 	*(uint64_t *)&vdso_th32->th_boottime.frac[0] = th->th_boottime.frac;
222416808549SKonstantin Belousov 	if (th->th_counter->tc_fill_vdso_timehands32 != NULL) {
222516808549SKonstantin Belousov 		enabled = th->th_counter->tc_fill_vdso_timehands32(vdso_th32,
222616808549SKonstantin Belousov 		    th->th_counter);
222716808549SKonstantin Belousov 	} else
222816808549SKonstantin Belousov 		enabled = 0;
2229aea81038SKonstantin Belousov 	if (!vdso_th_enable)
2230aea81038SKonstantin Belousov 		enabled = 0;
2231aea81038SKonstantin Belousov 	return (enabled);
2232aea81038SKonstantin Belousov }
2233aea81038SKonstantin Belousov #endif
223436bcc44eSKonstantin Belousov 
223536bcc44eSKonstantin Belousov #include "opt_ddb.h"
223636bcc44eSKonstantin Belousov #ifdef DDB
223736bcc44eSKonstantin Belousov #include <ddb/ddb.h>
223836bcc44eSKonstantin Belousov 
DB_SHOW_COMMAND(timecounter,db_show_timecounter)223936bcc44eSKonstantin Belousov DB_SHOW_COMMAND(timecounter, db_show_timecounter)
224036bcc44eSKonstantin Belousov {
224136bcc44eSKonstantin Belousov 	struct timehands *th;
224236bcc44eSKonstantin Belousov 	struct timecounter *tc;
224336bcc44eSKonstantin Belousov 	u_int val1, val2;
224436bcc44eSKonstantin Belousov 
224536bcc44eSKonstantin Belousov 	th = timehands;
224636bcc44eSKonstantin Belousov 	tc = th->th_counter;
224736bcc44eSKonstantin Belousov 	val1 = tc->tc_get_timecount(tc);
224836bcc44eSKonstantin Belousov 	__compiler_membar();
224936bcc44eSKonstantin Belousov 	val2 = tc->tc_get_timecount(tc);
225036bcc44eSKonstantin Belousov 
225136bcc44eSKonstantin Belousov 	db_printf("timecounter %p %s\n", tc, tc->tc_name);
225236bcc44eSKonstantin Belousov 	db_printf("  mask %#x freq %ju qual %d flags %#x priv %p\n",
225336bcc44eSKonstantin Belousov 	    tc->tc_counter_mask, (uintmax_t)tc->tc_frequency, tc->tc_quality,
225436bcc44eSKonstantin Belousov 	    tc->tc_flags, tc->tc_priv);
225536bcc44eSKonstantin Belousov 	db_printf("  val %#x %#x\n", val1, val2);
225636bcc44eSKonstantin Belousov 	db_printf("timehands adj %#jx scale %#jx ldelta %d off_cnt %d gen %d\n",
225736bcc44eSKonstantin Belousov 	    (uintmax_t)th->th_adjustment, (uintmax_t)th->th_scale,
225836bcc44eSKonstantin Belousov 	    th->th_large_delta, th->th_offset_count, th->th_generation);
225936bcc44eSKonstantin Belousov 	db_printf("  offset %jd %jd boottime %jd %jd\n",
226036bcc44eSKonstantin Belousov 	    (intmax_t)th->th_offset.sec, (uintmax_t)th->th_offset.frac,
226136bcc44eSKonstantin Belousov 	    (intmax_t)th->th_boottime.sec, (uintmax_t)th->th_boottime.frac);
226236bcc44eSKonstantin Belousov }
226336bcc44eSKonstantin Belousov #endif
2264