2 * Copyright (c) 2011 The University of Melbourne
5 * This software was developed by Julien Ridoux at the University of Melbourne
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/timeffc.h>
52 FEATURE(ffclock, "Feed-forward clock support");
54 extern struct ffclock_estimate ffclock_estimate;
55 extern struct bintime ffclock_boottime;
56 extern int8_t ffclock_updated;
57 extern struct mtx ffclock_mtx;
60 * Feed-forward clock absolute time. This should be the preferred way to read
61 * the feed-forward clock for "wall-clock" type time. The flags allow to compose
62 * various flavours of absolute time (e.g. with or without leap seconds taken
63 * into account). If valid pointers are provided, the ffcounter value and an
64 * upper bound on clock error associated with the bintime are provided.
65 * NOTE: use ffclock_convert_abs() to differ the conversion of a ffcounter value
69 ffclock_abstime(ffcounter *ffcount, struct bintime *bt,
70 struct bintime *error_bound, uint32_t flags)
72 struct ffclock_estimate cest;
74 ffcounter update_ffcount;
75 ffcounter ffdelta_error;
77 /* Get counter and corresponding time. */
78 if ((flags & FFCLOCK_FAST) == FFCLOCK_FAST)
79 ffclock_last_tick(&ffc, bt, flags);
81 ffclock_read_counter(&ffc);
82 ffclock_convert_abs(ffc, bt, flags);
85 /* Current ffclock estimate, use update_ffcount as generation number. */
87 update_ffcount = ffclock_estimate.update_ffcount;
88 bcopy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
89 } while (update_ffcount != ffclock_estimate.update_ffcount);
92 * Leap second adjustment. Total as seen by synchronisation algorithm
93 * since it started. cest.leapsec_next is the ffcounter prediction of
94 * when the next leapsecond occurs.
96 if ((flags & FFCLOCK_LEAPSEC) == FFCLOCK_LEAPSEC) {
97 bt->sec -= cest.leapsec_total;
98 if (ffc > cest.leapsec_next)
99 bt->sec -= cest.leapsec;
102 /* Boot time adjustment, for uptime/monotonic clocks. */
103 if ((flags & FFCLOCK_UPTIME) == FFCLOCK_UPTIME) {
104 bintime_sub(bt, &ffclock_boottime);
107 /* Compute error bound if a valid pointer has been passed. */
109 ffdelta_error = ffc - cest.update_ffcount;
110 ffclock_convert_diff(ffdelta_error, error_bound);
111 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
112 bintime_mul(error_bound, cest.errb_rate *
113 (uint64_t)18446744073709LL);
114 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns] */
115 bintime_addx(error_bound, cest.errb_abs *
116 (uint64_t)18446744073LL);
124 * Feed-forward difference clock. This should be the preferred way to convert a
125 * time interval in ffcounter values into a time interval in seconds. If a valid
126 * pointer is passed, an upper bound on the error in computing the time interval
127 * in seconds is provided.
130 ffclock_difftime(ffcounter ffdelta, struct bintime *bt,
131 struct bintime *error_bound)
133 ffcounter update_ffcount;
136 ffclock_convert_diff(ffdelta, bt);
140 update_ffcount = ffclock_estimate.update_ffcount;
141 err_rate = ffclock_estimate.errb_rate;
142 } while (update_ffcount != ffclock_estimate.update_ffcount);
144 ffclock_convert_diff(ffdelta, error_bound);
145 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s] */
146 bintime_mul(error_bound, err_rate * (uint64_t)18446744073709LL);
151 * Create a new kern.sysclock sysctl node, which will be home to some generic
152 * sysclock configuration variables. Feed-forward clock specific variables will
153 * live under the ffclock subnode.
156 SYSCTL_NODE(_kern, OID_AUTO, sysclock, CTLFLAG_RW, 0,
157 "System clock related configuration");
158 SYSCTL_NODE(_kern_sysclock, OID_AUTO, ffclock, CTLFLAG_RW, 0,
159 "Feed-forward clock configuration");
161 static char *sysclocks[] = {"feedback", "feed-forward"};
162 #define MAX_SYSCLOCK_NAME_LEN 16
163 #define NUM_SYSCLOCKS (sizeof(sysclocks) / sizeof(*sysclocks))
165 static int ffclock_version = 2;
166 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, version, CTLFLAG_RD,
167 &ffclock_version, 0, "Feed-forward clock kernel version");
169 /* List available sysclocks. */
171 sysctl_kern_sysclock_available(SYSCTL_HANDLER_ARGS)
176 s = sbuf_new_for_sysctl(NULL, NULL,
177 MAX_SYSCLOCK_NAME_LEN * NUM_SYSCLOCKS, req);
181 for (clk = 0; clk < NUM_SYSCLOCKS; clk++) {
182 sbuf_cat(s, sysclocks[clk]);
183 if (clk + 1 < NUM_SYSCLOCKS)
186 error = sbuf_finish(s);
192 SYSCTL_PROC(_kern_sysclock, OID_AUTO, available, CTLTYPE_STRING | CTLFLAG_RD,
193 0, 0, 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, CTLTYPE_STRING | CTLFLAG_RW,
233 0, 0, sysctl_kern_sysclock_active, "A",
234 "Name of the active system clock which is currently serving time");
236 static int sysctl_kern_ffclock_ffcounter_bypass = 0;
237 SYSCTL_INT(_kern_sysclock_ffclock, OID_AUTO, ffcounter_bypass, CTLFLAG_RW,
238 &sysctl_kern_ffclock_ffcounter_bypass, 0,
239 "Use reliable hardware timecounter as the feed-forward counter");
242 * High level functions to access the Feed-Forward Clock.
245 ffclock_bintime(struct bintime *bt)
248 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
252 ffclock_nanotime(struct timespec *tsp)
256 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
257 bintime2timespec(&bt, tsp);
261 ffclock_microtime(struct timeval *tvp)
265 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_LEAPSEC);
266 bintime2timeval(&bt, tvp);
270 ffclock_getbintime(struct bintime *bt)
273 ffclock_abstime(NULL, bt, NULL,
274 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
278 ffclock_getnanotime(struct timespec *tsp)
282 ffclock_abstime(NULL, &bt, NULL,
283 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
284 bintime2timespec(&bt, tsp);
288 ffclock_getmicrotime(struct timeval *tvp)
292 ffclock_abstime(NULL, &bt, NULL,
293 FFCLOCK_LERP | FFCLOCK_LEAPSEC | FFCLOCK_FAST);
294 bintime2timeval(&bt, tvp);
298 ffclock_binuptime(struct bintime *bt)
301 ffclock_abstime(NULL, bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
305 ffclock_nanouptime(struct timespec *tsp)
309 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
310 bintime2timespec(&bt, tsp);
314 ffclock_microuptime(struct timeval *tvp)
318 ffclock_abstime(NULL, &bt, NULL, FFCLOCK_LERP | FFCLOCK_UPTIME);
319 bintime2timeval(&bt, tvp);
323 ffclock_getbinuptime(struct bintime *bt)
326 ffclock_abstime(NULL, bt, NULL,
327 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
331 ffclock_getnanouptime(struct timespec *tsp)
335 ffclock_abstime(NULL, &bt, NULL,
336 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
337 bintime2timespec(&bt, tsp);
341 ffclock_getmicrouptime(struct timeval *tvp)
345 ffclock_abstime(NULL, &bt, NULL,
346 FFCLOCK_LERP | FFCLOCK_UPTIME | FFCLOCK_FAST);
347 bintime2timeval(&bt, tvp);
351 ffclock_bindifftime(ffcounter ffdelta, struct bintime *bt)
354 ffclock_difftime(ffdelta, bt, NULL);
358 ffclock_nanodifftime(ffcounter ffdelta, struct timespec *tsp)
362 ffclock_difftime(ffdelta, &bt, NULL);
363 bintime2timespec(&bt, tsp);
367 ffclock_microdifftime(ffcounter ffdelta, struct timeval *tvp)
371 ffclock_difftime(ffdelta, &bt, NULL);
372 bintime2timeval(&bt, tvp);
376 * System call allowing userland applications to retrieve the current value of
377 * the Feed-Forward Clock counter.
379 #ifndef _SYS_SYSPROTO_H_
380 struct ffclock_getcounter_args {
386 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
392 ffclock_read_counter(&ffcount);
395 error = copyout(&ffcount, uap->ffcount, sizeof(ffcounter));
401 * System call allowing the synchronisation daemon to push new feed-foward clock
402 * estimates to the kernel. Acquire ffclock_mtx to prevent concurrent updates
403 * and ensure data consistency.
404 * NOTE: ffclock_updated signals the fftimehands that new estimates are
405 * available. The updated estimates are picked up by the fftimehands on next
406 * tick, which could take as long as 1/hz seconds (if ticks are not missed).
408 #ifndef _SYS_SYSPROTO_H_
409 struct ffclock_setestimate_args {
410 struct ffclock_estimate *cest;
415 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
417 struct ffclock_estimate cest;
420 /* Reuse of PRIV_CLOCK_SETTIME. */
421 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
424 if ((error = copyin(uap->cest, &cest, sizeof(struct ffclock_estimate)))
428 mtx_lock(&ffclock_mtx);
429 memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
431 mtx_unlock(&ffclock_mtx);
436 * System call allowing userland applications to retrieve the clock estimates
437 * stored within the kernel. It is useful to kickstart the synchronisation
438 * daemon with the kernel's knowledge of hardware timecounter.
440 #ifndef _SYS_SYSPROTO_H_
441 struct ffclock_getestimate_args {
442 struct ffclock_estimate *cest;
447 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)
449 struct ffclock_estimate cest;
452 mtx_lock(&ffclock_mtx);
453 memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
454 mtx_unlock(&ffclock_mtx);
455 error = copyout(&cest, uap->cest, sizeof(struct ffclock_estimate));
462 sys_ffclock_getcounter(struct thread *td, struct ffclock_getcounter_args *uap)
469 sys_ffclock_setestimate(struct thread *td, struct ffclock_setestimate_args *uap)
476 sys_ffclock_getestimate(struct thread *td, struct ffclock_getestimate_args *uap)