2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_ffclock.h"
35 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
44 #include <sys/sysproto.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 #include <sys/timeffc.h>
51 FEATURE(ffclock, "Feed-forward clock support");
53 extern struct ffclock_estimate ffclock_estimate;
54 extern struct bintime ffclock_boottime;
55 extern int8_t ffclock_updated;
56 extern struct mtx ffclock_mtx;
59 * Feed-forward clock absolute time. This should be the preferred way to read
60 * the feed-forward clock for "wall-clock" type time. The flags allow to compose
61 * various flavours of absolute time (e.g. with or without leap seconds taken
62 * into account). If valid pointers are provided, the ffcounter value and an
63 * upper bound on clock error associated with the bintime are provided.
64 * NOTE: use ffclock_convert_abs() to differ the conversion of a ffcounter value
68 ffclock_abstime(ffcounter *ffcount, struct bintime *bt,
69 struct bintime *error_bound, uint32_t flags)
71 struct ffclock_estimate cest;
73 ffcounter update_ffcount;
74 ffcounter ffdelta_error;
76 /* Get counter and corresponding time. */
77 if ((flags & FFCLOCK_FAST) == FFCLOCK_FAST)
78 ffclock_last_tick(&ffc, bt, flags);
80 ffclock_read_counter(&ffc);
81 ffclock_convert_abs(ffc, bt, flags);
84 /* Current ffclock estimate, use update_ffcount as generation number. */
86 update_ffcount = ffclock_estimate.update_ffcount;
87 bcopy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
88 } while (update_ffcount != ffclock_estimate.update_ffcount);
91 * Leap second adjustment. Total as seen by synchronisation algorithm
92 * since it started. cest.leapsec_next is the ffcounter prediction of
93 * when the next leapsecond occurs.
95 if ((flags & FFCLOCK_LEAPSEC) == FFCLOCK_LEAPSEC) {
96 bt->sec -= cest.leapsec_total;
97 if (ffc > cest.leapsec_next)
98 bt->sec -= cest.leapsec;
101 /* Boot time adjustment, for uptime/monotonic clocks. */
102 if ((flags & FFCLOCK_UPTIME) == FFCLOCK_UPTIME) {
103 bintime_sub(bt, &ffclock_boottime);
106 /* Compute error bound if a valid pointer has been passed. */
108 ffdelta_error = ffc - cest.update_ffcount;
109 ffclock_convert_diff(ffdelta_error, error_bound);
110 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
111 bintime_mul(error_bound, cest.errb_rate *
112 (uint64_t)18446744073709LL);
113 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns] */
114 bintime_addx(error_bound, cest.errb_abs *
115 (uint64_t)18446744073LL);
123 * Feed-forward difference clock. This should be the preferred way to convert a
124 * time interval in ffcounter values into a time interval in seconds. If a valid
125 * pointer is passed, an upper bound on the error in computing the time interval
126 * in seconds is provided.
129 ffclock_difftime(ffcounter ffdelta, struct bintime *bt,
130 struct bintime *error_bound)
132 ffcounter update_ffcount;
135 ffclock_convert_diff(ffdelta, bt);
139 update_ffcount = ffclock_estimate.update_ffcount;
140 err_rate = ffclock_estimate.errb_rate;
141 } while (update_ffcount != ffclock_estimate.update_ffcount);
143 ffclock_convert_diff(ffdelta, error_bound);
144 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
145 bintime_mul(error_bound, err_rate * (uint64_t)18446744073709LL);
150 * Create a new kern.sysclock sysctl node, which will be home to some generic
151 * sysclock configuration variables. Feed-forward clock specific variables will
152 * live under the ffclock subnode.
155 SYSCTL_NODE(_kern, OID_AUTO, sysclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
156 "System clock related configuration");
157 SYSCTL_NODE(_kern_sysclock, OID_AUTO, ffclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
158 "Feed-forward clock configuration");
160 static char *sysclocks[] = {"feedback", "feed-forward"};
161 #define MAX_SYSCLOCK_NAME_LEN 16
162 #define NUM_SYSCLOCKS nitems(sysclocks)
164 static int ffclock_version = 2;
165 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, version, CTLFLAG_RD,
166 &ffclock_version, 0, "Feed-forward clock kernel version");
168 /* List available sysclocks. */
170 sysctl_kern_sysclock_available(SYSCTL_HANDLER_ARGS)
175 s = sbuf_new_for_sysctl(NULL, NULL,
176 MAX_SYSCLOCK_NAME_LEN * NUM_SYSCLOCKS, req);
180 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
181 sbuf_cat(s, sysclocks[clk]);
182 if (clk + 1 < NUM_SYSCLOCKS)
185 error = sbuf_finish(s);
191 SYSCTL_PROC(_kern_sysclock, OID_AUTO, available,
192 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
193 sysctl_kern_sysclock_available, "A",
194 "List of available system clocks");
197 * Return the name of the active system clock if read, or attempt to change
198 * the active system clock to the user specified one if written to. The active
199 * system clock is read when calling any of the [get]{bin,nano,micro}[up]time()
203 sysctl_kern_sysclock_active(SYSCTL_HANDLER_ARGS)
205 char newclock[MAX_SYSCLOCK_NAME_LEN];
209 /* Return the name of the current active sysclock. */
210 strlcpy(newclock, sysclocks[sysclock_active], sizeof(newclock));
211 error = sysctl_handle_string(oidp, newclock, sizeof(newclock), req);
213 /* Check for error or no change */
214 if (error != 0 || req->newptr == NULL)
217 /* Change the active sysclock to the user specified one: */
219 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
220 if (strncmp(newclock, sysclocks[clk],
221 MAX_SYSCLOCK_NAME_LEN - 1)) {
224 sysclock_active = clk;
232 SYSCTL_PROC(_kern_sysclock, OID_AUTO, active,
233 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
234 sysctl_kern_sysclock_active, "A",
235 "Name of the active system clock which is currently serving time");
237 static int sysctl_kern_ffclock_ffcounter_bypass = 0;
238 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, ffcounter_bypass, CTLFLAG_RW,
239 &sysctl_kern_ffclock_ffcounter_bypass, 0,
240 "Use reliable hardware timecounter as the feed-forward counter");
243 * High level functions to access the Feed-Forward Clock.
246 ffclock_bintime(struct bintime *bt)
249 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
253 ffclock_nanotime(struct timespec *tsp)
257 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
258 bintime2timespec(&bt, tsp);
262 ffclock_microtime(struct timeval *tvp)
266 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
267 bintime2timeval(&bt, tvp);
271 ffclock_getbintime(struct bintime *bt)
274 ffclock_abstime(NULL, bt, NULL,
275 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
279 ffclock_getnanotime(struct timespec *tsp)
283 ffclock_abstime(NULL, &bt, NULL,
284 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
285 bintime2timespec(&bt, tsp);
289 ffclock_getmicrotime(struct timeval *tvp)
293 ffclock_abstime(NULL, &bt, NULL,
294 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
295 bintime2timeval(&bt, tvp);
299 ffclock_binuptime(struct bintime *bt)
302 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
306 ffclock_nanouptime(struct timespec *tsp)
310 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
311 bintime2timespec(&bt, tsp);
315 ffclock_microuptime(struct timeval *tvp)
319 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
320 bintime2timeval(&bt, tvp);
324 ffclock_getbinuptime(struct bintime *bt)
327 ffclock_abstime(NULL, bt, NULL,
328 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
332 ffclock_getnanouptime(struct timespec *tsp)
336 ffclock_abstime(NULL, &bt, NULL,
337 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
338 bintime2timespec(&bt, tsp);
342 ffclock_getmicrouptime(struct timeval *tvp)
346 ffclock_abstime(NULL, &bt, NULL,
347 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
348 bintime2timeval(&bt, tvp);
352 ffclock_bindifftime(ffcounter ffdelta, struct bintime *bt)
355 ffclock_difftime(ffdelta, bt, NULL);
359 ffclock_nanodifftime(ffcounter ffdelta, struct timespec *tsp)
363 ffclock_difftime(ffdelta, &bt, NULL);
364 bintime2timespec(&bt, tsp);
368 ffclock_microdifftime(ffcounter ffdelta, struct timeval *tvp)
372 ffclock_difftime(ffdelta, &bt, NULL);
373 bintime2timeval(&bt, tvp);
377 * System call allowing userland applications to retrieve the current value of
378 * the Feed-Forward Clock counter.
380 #ifndef _SYS_SYSPROTO_H_
381 struct ffclock_getcounter_args {
387 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
393 ffclock_read_counter(&ffcount);
396 error = copyout(&ffcount, uap->ffcount, sizeof(ffcounter));
402 * System call allowing the synchronisation daemon to push new feed-forward clock
403 * estimates to the kernel. Acquire ffclock_mtx to prevent concurrent updates
404 * and ensure data consistency.
405 * NOTE: ffclock_updated signals the fftimehands that new estimates are
406 * available. The updated estimates are picked up by the fftimehands on next
407 * tick, which could take as long as 1/hz seconds (if ticks are not missed).
409 #ifndef _SYS_SYSPROTO_H_
410 struct ffclock_setestimate_args {
411 struct ffclock_estimate *cest;
416 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
418 struct ffclock_estimate cest;
421 /* Reuse of PRIV_CLOCK_SETTIME. */
422 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
425 if ((error = copyin(uap->cest, &cest, sizeof(struct ffclock_estimate)))
429 mtx_lock(&ffclock_mtx);
430 memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
432 mtx_unlock(&ffclock_mtx);
437 * System call allowing userland applications to retrieve the clock estimates
438 * stored within the kernel. It is useful to kickstart the synchronisation
439 * daemon with the kernel's knowledge of hardware timecounter.
441 #ifndef _SYS_SYSPROTO_H_
442 struct ffclock_getestimate_args {
443 struct ffclock_estimate *cest;
448 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)
450 struct ffclock_estimate cest;
453 mtx_lock(&ffclock_mtx);
454 memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
455 mtx_unlock(&ffclock_mtx);
456 error = copyout(&cest, uap->cest, sizeof(struct ffclock_estimate));
463 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
470 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
477 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)