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/sysproto.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/timeffc.h>
53 FEATURE(ffclock, "Feed-forward clock support");
55 extern struct ffclock_estimate ffclock_estimate;
56 extern struct bintime ffclock_boottime;
57 extern int8_t ffclock_updated;
58 extern struct mtx ffclock_mtx;
61 * Feed-forward clock absolute time. This should be the preferred way to read
62 * the feed-forward clock for "wall-clock" type time. The flags allow to compose
63 * various flavours of absolute time (e.g. with or without leap seconds taken
64 * into account). If valid pointers are provided, the ffcounter value and an
65 * upper bound on clock error associated with the bintime are provided.
66 * NOTE: use ffclock_convert_abs() to differ the conversion of a ffcounter value
70 ffclock_abstime(ffcounter *ffcount, struct bintime *bt,
71 struct bintime *error_bound, uint32_t flags)
73 struct ffclock_estimate cest;
75 ffcounter update_ffcount;
76 ffcounter ffdelta_error;
78 /* Get counter and corresponding time. */
79 if ((flags & FFCLOCK_FAST) == FFCLOCK_FAST)
80 ffclock_last_tick(&ffc, bt, flags);
82 ffclock_read_counter(&ffc);
83 ffclock_convert_abs(ffc, bt, flags);
86 /* Current ffclock estimate, use update_ffcount as generation number. */
88 update_ffcount = ffclock_estimate.update_ffcount;
89 bcopy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
90 } while (update_ffcount != ffclock_estimate.update_ffcount);
93 * Leap second adjustment. Total as seen by synchronisation algorithm
94 * since it started. cest.leapsec_next is the ffcounter prediction of
95 * when the next leapsecond occurs.
97 if ((flags & FFCLOCK_LEAPSEC) == FFCLOCK_LEAPSEC) {
98 bt->sec -= cest.leapsec_total;
99 if (ffc > cest.leapsec_next)
100 bt->sec -= cest.leapsec;
103 /* Boot time adjustment, for uptime/monotonic clocks. */
104 if ((flags & FFCLOCK_UPTIME) == FFCLOCK_UPTIME) {
105 bintime_sub(bt, &ffclock_boottime);
108 /* Compute error bound if a valid pointer has been passed. */
110 ffdelta_error = ffc - cest.update_ffcount;
111 ffclock_convert_diff(ffdelta_error, error_bound);
112 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
113 bintime_mul(error_bound, cest.errb_rate *
114 (uint64_t)18446744073709LL);
115 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns] */
116 bintime_addx(error_bound, cest.errb_abs *
117 (uint64_t)18446744073LL);
125 * Feed-forward difference clock. This should be the preferred way to convert a
126 * time interval in ffcounter values into a time interval in seconds. If a valid
127 * pointer is passed, an upper bound on the error in computing the time interval
128 * in seconds is provided.
131 ffclock_difftime(ffcounter ffdelta, struct bintime *bt,
132 struct bintime *error_bound)
134 ffcounter update_ffcount;
137 ffclock_convert_diff(ffdelta, bt);
141 update_ffcount = ffclock_estimate.update_ffcount;
142 err_rate = ffclock_estimate.errb_rate;
143 } while (update_ffcount != ffclock_estimate.update_ffcount);
145 ffclock_convert_diff(ffdelta, error_bound);
146 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
147 bintime_mul(error_bound, err_rate * (uint64_t)18446744073709LL);
152 * Create a new kern.sysclock sysctl node, which will be home to some generic
153 * sysclock configuration variables. Feed-forward clock specific variables will
154 * live under the ffclock subnode.
157 SYSCTL_NODE(_kern, OID_AUTO, sysclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
158 "System clock related configuration");
159 SYSCTL_NODE(_kern_sysclock, OID_AUTO, ffclock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
160 "Feed-forward clock configuration");
162 static char *sysclocks[] = {"feedback", "feed-forward"};
163 #define MAX_SYSCLOCK_NAME_LEN 16
164 #define NUM_SYSCLOCKS nitems(sysclocks)
166 static int ffclock_version = 2;
167 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, version, CTLFLAG_RD,
168 &ffclock_version, 0, "Feed-forward clock kernel version");
170 /* List available sysclocks. */
172 sysctl_kern_sysclock_available(SYSCTL_HANDLER_ARGS)
177 s = sbuf_new_for_sysctl(NULL, NULL,
178 MAX_SYSCLOCK_NAME_LEN * NUM_SYSCLOCKS, req);
182 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
183 sbuf_cat(s, sysclocks[clk]);
184 if (clk + 1 < NUM_SYSCLOCKS)
187 error = sbuf_finish(s);
193 SYSCTL_PROC(_kern_sysclock, OID_AUTO, available,
194 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 0, 0,
195 sysctl_kern_sysclock_available, "A",
196 "List of available system clocks");
199 * Return the name of the active system clock if read, or attempt to change
200 * the active system clock to the user specified one if written to. The active
201 * system clock is read when calling any of the [get]{bin,nano,micro}[up]time()
205 sysctl_kern_sysclock_active(SYSCTL_HANDLER_ARGS)
207 char newclock[MAX_SYSCLOCK_NAME_LEN];
211 /* Return the name of the current active sysclock. */
212 strlcpy(newclock, sysclocks[sysclock_active], sizeof(newclock));
213 error = sysctl_handle_string(oidp, newclock, sizeof(newclock), req);
215 /* Check for error or no change */
216 if (error != 0 || req->newptr == NULL)
219 /* Change the active sysclock to the user specified one: */
221 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
222 if (strncmp(newclock, sysclocks[clk],
223 MAX_SYSCLOCK_NAME_LEN - 1)) {
226 sysclock_active = clk;
234 SYSCTL_PROC(_kern_sysclock, OID_AUTO, active,
235 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
236 sysctl_kern_sysclock_active, "A",
237 "Name of the active system clock which is currently serving time");
239 static int sysctl_kern_ffclock_ffcounter_bypass = 0;
240 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, ffcounter_bypass, CTLFLAG_RW,
241 &sysctl_kern_ffclock_ffcounter_bypass, 0,
242 "Use reliable hardware timecounter as the feed-forward counter");
245 * High level functions to access the Feed-Forward Clock.
248 ffclock_bintime(struct bintime *bt)
251 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
255 ffclock_nanotime(struct timespec *tsp)
259 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
260 bintime2timespec(&bt, tsp);
264 ffclock_microtime(struct timeval *tvp)
268 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
269 bintime2timeval(&bt, tvp);
273 ffclock_getbintime(struct bintime *bt)
276 ffclock_abstime(NULL, bt, NULL,
277 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
281 ffclock_getnanotime(struct timespec *tsp)
285 ffclock_abstime(NULL, &bt, NULL,
286 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
287 bintime2timespec(&bt, tsp);
291 ffclock_getmicrotime(struct timeval *tvp)
295 ffclock_abstime(NULL, &bt, NULL,
296 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
297 bintime2timeval(&bt, tvp);
301 ffclock_binuptime(struct bintime *bt)
304 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
308 ffclock_nanouptime(struct timespec *tsp)
312 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
313 bintime2timespec(&bt, tsp);
317 ffclock_microuptime(struct timeval *tvp)
321 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
322 bintime2timeval(&bt, tvp);
326 ffclock_getbinuptime(struct bintime *bt)
329 ffclock_abstime(NULL, bt, NULL,
330 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
334 ffclock_getnanouptime(struct timespec *tsp)
338 ffclock_abstime(NULL, &bt, NULL,
339 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
340 bintime2timespec(&bt, tsp);
344 ffclock_getmicrouptime(struct timeval *tvp)
348 ffclock_abstime(NULL, &bt, NULL,
349 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
350 bintime2timeval(&bt, tvp);
354 ffclock_bindifftime(ffcounter ffdelta, struct bintime *bt)
357 ffclock_difftime(ffdelta, bt, NULL);
361 ffclock_nanodifftime(ffcounter ffdelta, struct timespec *tsp)
365 ffclock_difftime(ffdelta, &bt, NULL);
366 bintime2timespec(&bt, tsp);
370 ffclock_microdifftime(ffcounter ffdelta, struct timeval *tvp)
374 ffclock_difftime(ffdelta, &bt, NULL);
375 bintime2timeval(&bt, tvp);
379 * System call allowing userland applications to retrieve the current value of
380 * the Feed-Forward Clock counter.
382 #ifndef _SYS_SYSPROTO_H_
383 struct ffclock_getcounter_args {
389 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
395 ffclock_read_counter(&ffcount);
398 error = copyout(&ffcount, uap->ffcount, sizeof(ffcounter));
404 * System call allowing the synchronisation daemon to push new feed-forward clock
405 * estimates to the kernel. Acquire ffclock_mtx to prevent concurrent updates
406 * and ensure data consistency.
407 * NOTE: ffclock_updated signals the fftimehands that new estimates are
408 * available. The updated estimates are picked up by the fftimehands on next
409 * tick, which could take as long as 1/hz seconds (if ticks are not missed).
411 #ifndef _SYS_SYSPROTO_H_
412 struct ffclock_setestimate_args {
413 struct ffclock_estimate *cest;
418 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
420 struct ffclock_estimate cest;
423 /* Reuse of PRIV_CLOCK_SETTIME. */
424 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
427 if ((error = copyin(uap->cest, &cest, sizeof(struct ffclock_estimate)))
431 mtx_lock(&ffclock_mtx);
432 memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
434 mtx_unlock(&ffclock_mtx);
439 * System call allowing userland applications to retrieve the clock estimates
440 * stored within the kernel. It is useful to kickstart the synchronisation
441 * daemon with the kernel's knowledge of hardware timecounter.
443 #ifndef _SYS_SYSPROTO_H_
444 struct ffclock_getestimate_args {
445 struct ffclock_estimate *cest;
450 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)
452 struct ffclock_estimate cest;
455 mtx_lock(&ffclock_mtx);
456 memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
457 mtx_unlock(&ffclock_mtx);
458 error = copyout(&cest, uap->cest, sizeof(struct ffclock_estimate));
465 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
472 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
479 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)