2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 The University of Melbourne
7 * This software was developed by Julien Ridoux at the University of Melbourne
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_ffclock.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/timeffc.h>
54 FEATURE(ffclock, "Feed-forward clock support");
56 extern struct ffclock_estimate ffclock_estimate;
57 extern struct bintime ffclock_boottime;
58 extern int8_t ffclock_updated;
59 extern struct mtx ffclock_mtx;
62 * Feed-forward clock absolute time. This should be the preferred way to read
63 * the feed-forward clock for "wall-clock" type time. The flags allow to compose
64 * various flavours of absolute time (e.g. with or without leap seconds taken
65 * into account). If valid pointers are provided, the ffcounter value and an
66 * upper bound on clock error associated with the bintime are provided.
67 * NOTE: use ffclock_convert_abs() to differ the conversion of a ffcounter value
71 ffclock_abstime(ffcounter *ffcount, struct bintime *bt,
72 struct bintime *error_bound, uint32_t flags)
74 struct ffclock_estimate cest;
76 ffcounter update_ffcount;
77 ffcounter ffdelta_error;
79 /* Get counter and corresponding time. */
80 if ((flags & FFCLOCK_FAST) == FFCLOCK_FAST)
81 ffclock_last_tick(&ffc, bt, flags);
83 ffclock_read_counter(&ffc);
84 ffclock_convert_abs(ffc, bt, flags);
87 /* Current ffclock estimate, use update_ffcount as generation number. */
89 update_ffcount = ffclock_estimate.update_ffcount;
90 bcopy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
91 } while (update_ffcount != ffclock_estimate.update_ffcount);
94 * Leap second adjustment. Total as seen by synchronisation algorithm
95 * since it started. cest.leapsec_next is the ffcounter prediction of
96 * when the next leapsecond occurs.
98 if ((flags & FFCLOCK_LEAPSEC) == FFCLOCK_LEAPSEC) {
99 bt->sec -= cest.leapsec_total;
100 if (ffc > cest.leapsec_next)
101 bt->sec -= cest.leapsec;
104 /* Boot time adjustment, for uptime/monotonic clocks. */
105 if ((flags & FFCLOCK_UPTIME) == FFCLOCK_UPTIME) {
106 bintime_sub(bt, &ffclock_boottime);
109 /* Compute error bound if a valid pointer has been passed. */
111 ffdelta_error = ffc - cest.update_ffcount;
112 ffclock_convert_diff(ffdelta_error, error_bound);
113 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
114 bintime_mul(error_bound, cest.errb_rate *
115 (uint64_t)18446744073709LL);
116 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns] */
117 bintime_addx(error_bound, cest.errb_abs *
118 (uint64_t)18446744073LL);
126 * Feed-forward difference clock. This should be the preferred way to convert a
127 * time interval in ffcounter values into a time interval in seconds. If a valid
128 * pointer is passed, an upper bound on the error in computing the time interval
129 * in seconds is provided.
132 ffclock_difftime(ffcounter ffdelta, struct bintime *bt,
133 struct bintime *error_bound)
135 ffcounter update_ffcount;
138 ffclock_convert_diff(ffdelta, bt);
142 update_ffcount = ffclock_estimate.update_ffcount;
143 err_rate = ffclock_estimate.errb_rate;
144 } while (update_ffcount != ffclock_estimate.update_ffcount);
146 ffclock_convert_diff(ffdelta, error_bound);
147 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
148 bintime_mul(error_bound, err_rate * (uint64_t)18446744073709LL);
153 * Create a new kern.sysclock sysctl node, which will be home to some generic
154 * sysclock configuration variables. Feed-forward clock specific variables will
155 * live under the ffclock subnode.
158 SYSCTL_NODE(_kern, OID_AUTO, sysclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
159 "System clock related configuration");
160 SYSCTL_NODE(_kern_sysclock, OID_AUTO, ffclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
161 "Feed-forward clock configuration");
163 static char *sysclocks[] = {"feedback", "feed-forward"};
164 #define MAX_SYSCLOCK_NAME_LEN 16
165 #define NUM_SYSCLOCKS nitems(sysclocks)
167 static int ffclock_version = 2;
168 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, version, CTLFLAG_RD,
169 &ffclock_version, 0, "Feed-forward clock kernel version");
171 /* List available sysclocks. */
173 sysctl_kern_sysclock_available(SYSCTL_HANDLER_ARGS)
178 s = sbuf_new_for_sysctl(NULL, NULL,
179 MAX_SYSCLOCK_NAME_LEN * NUM_SYSCLOCKS, req);
183 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
184 sbuf_cat(s, sysclocks[clk]);
185 if (clk + 1 < NUM_SYSCLOCKS)
188 error = sbuf_finish(s);
194 SYSCTL_PROC(_kern_sysclock, OID_AUTO, available,
195 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
196 sysctl_kern_sysclock_available, "A",
197 "List of available system clocks");
200 * Return the name of the active system clock if read, or attempt to change
201 * the active system clock to the user specified one if written to. The active
202 * system clock is read when calling any of the [get]{bin,nano,micro}[up]time()
206 sysctl_kern_sysclock_active(SYSCTL_HANDLER_ARGS)
208 char newclock[MAX_SYSCLOCK_NAME_LEN];
212 /* Return the name of the current active sysclock. */
213 strlcpy(newclock, sysclocks[sysclock_active], sizeof(newclock));
214 error = sysctl_handle_string(oidp, newclock, sizeof(newclock), req);
216 /* Check for error or no change */
217 if (error != 0 || req->newptr == NULL)
220 /* Change the active sysclock to the user specified one: */
222 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
223 if (strncmp(newclock, sysclocks[clk],
224 MAX_SYSCLOCK_NAME_LEN - 1)) {
227 sysclock_active = clk;
235 SYSCTL_PROC(_kern_sysclock, OID_AUTO, active,
236 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
237 sysctl_kern_sysclock_active, "A",
238 "Name of the active system clock which is currently serving time");
240 static int sysctl_kern_ffclock_ffcounter_bypass = 0;
241 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, ffcounter_bypass, CTLFLAG_RW,
242 &sysctl_kern_ffclock_ffcounter_bypass, 0,
243 "Use reliable hardware timecounter as the feed-forward counter");
246 * High level functions to access the Feed-Forward Clock.
249 ffclock_bintime(struct bintime *bt)
252 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
256 ffclock_nanotime(struct timespec *tsp)
260 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
261 bintime2timespec(&bt, tsp);
265 ffclock_microtime(struct timeval *tvp)
269 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
270 bintime2timeval(&bt, tvp);
274 ffclock_getbintime(struct bintime *bt)
277 ffclock_abstime(NULL, bt, NULL,
278 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
282 ffclock_getnanotime(struct timespec *tsp)
286 ffclock_abstime(NULL, &bt, NULL,
287 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
288 bintime2timespec(&bt, tsp);
292 ffclock_getmicrotime(struct timeval *tvp)
296 ffclock_abstime(NULL, &bt, NULL,
297 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
298 bintime2timeval(&bt, tvp);
302 ffclock_binuptime(struct bintime *bt)
305 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
309 ffclock_nanouptime(struct timespec *tsp)
313 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
314 bintime2timespec(&bt, tsp);
318 ffclock_microuptime(struct timeval *tvp)
322 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
323 bintime2timeval(&bt, tvp);
327 ffclock_getbinuptime(struct bintime *bt)
330 ffclock_abstime(NULL, bt, NULL,
331 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
335 ffclock_getnanouptime(struct timespec *tsp)
339 ffclock_abstime(NULL, &bt, NULL,
340 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
341 bintime2timespec(&bt, tsp);
345 ffclock_getmicrouptime(struct timeval *tvp)
349 ffclock_abstime(NULL, &bt, NULL,
350 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
351 bintime2timeval(&bt, tvp);
355 ffclock_bindifftime(ffcounter ffdelta, struct bintime *bt)
358 ffclock_difftime(ffdelta, bt, NULL);
362 ffclock_nanodifftime(ffcounter ffdelta, struct timespec *tsp)
366 ffclock_difftime(ffdelta, &bt, NULL);
367 bintime2timespec(&bt, tsp);
371 ffclock_microdifftime(ffcounter ffdelta, struct timeval *tvp)
375 ffclock_difftime(ffdelta, &bt, NULL);
376 bintime2timeval(&bt, tvp);
380 * System call allowing userland applications to retrieve the current value of
381 * the Feed-Forward Clock counter.
383 #ifndef _SYS_SYSPROTO_H_
384 struct ffclock_getcounter_args {
390 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
396 ffclock_read_counter(&ffcount);
399 error = copyout(&ffcount, uap->ffcount, sizeof(ffcounter));
405 * System call allowing the synchronisation daemon to push new feed-foward clock
406 * estimates to the kernel. Acquire ffclock_mtx to prevent concurrent updates
407 * and ensure data consistency.
408 * NOTE: ffclock_updated signals the fftimehands that new estimates are
409 * available. The updated estimates are picked up by the fftimehands on next
410 * tick, which could take as long as 1/hz seconds (if ticks are not missed).
412 #ifndef _SYS_SYSPROTO_H_
413 struct ffclock_setestimate_args {
414 struct ffclock_estimate *cest;
419 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
421 struct ffclock_estimate cest;
424 /* Reuse of PRIV_CLOCK_SETTIME. */
425 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
428 if ((error = copyin(uap->cest, &cest, sizeof(struct ffclock_estimate)))
432 mtx_lock(&ffclock_mtx);
433 memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
435 mtx_unlock(&ffclock_mtx);
440 * System call allowing userland applications to retrieve the clock estimates
441 * stored within the kernel. It is useful to kickstart the synchronisation
442 * daemon with the kernel's knowledge of hardware timecounter.
444 #ifndef _SYS_SYSPROTO_H_
445 struct ffclock_getestimate_args {
446 struct ffclock_estimate *cest;
451 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)
453 struct ffclock_estimate cest;
456 mtx_lock(&ffclock_mtx);
457 memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
458 mtx_unlock(&ffclock_mtx);
459 error = copyout(&cest, uap->cest, sizeof(struct ffclock_estimate));
466 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
473 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
480 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)