source: rtems/cpukit/score/src/kern_tc.c @ a9219e7

5
Last change on this file since a9219e7 was a9219e7, checked in by Ed Schouten <ed@…>, on 12/14/16 at 12:56:58

timecounter: Merge FreeBSD change r310053

Add labels to sysctls related to clocks.

Sysctls like kern.eventtimer.et.*.quality currently embed the name of
the clock device. This is problematic for the Prometheus metrics
exporter for two reasons:

  • Some of those clocks have dashes in their names, which Prometheus doesn't allow to be used in metric names.
  • It doesn't allow for extracting the same property of all clocks on the system from within a single query.

Attach these nodes to have a label, so that the Prometheus metrics
exporter gives these metric a uniform name with the name of the clock
attached as a label.

Reviewed by: cem
Differential Revision: https://reviews.freebsd.org/D8775

Update #3175.

  • Property mode set to 100644
File size: 60.7 KB
Line 
1/*-
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * Copyright (c) 2011, 2015, 2016 The FreeBSD Foundation
10 * All rights reserved.
11 *
12 * Portions of this software were developed by Julien Ridoux at the University
13 * of Melbourne under sponsorship from the FreeBSD Foundation.
14 *
15 * Portions of this software were developed by Konstantin Belousov
16 * under sponsorship from the FreeBSD Foundation.
17 */
18
19#ifdef __rtems__
20#include <sys/lock.h>
21#define _KERNEL
22#define binuptime(_bt) _Timecounter_Binuptime(_bt)
23#define nanouptime(_tsp) _Timecounter_Nanouptime(_tsp)
24#define microuptime(_tvp) _Timecounter_Microuptime(_tvp)
25#define bintime(_bt) _Timecounter_Bintime(_bt)
26#define nanotime(_tsp) _Timecounter_Nanotime(_tsp)
27#define microtime(_tvp) _Timecounter_Microtime(_tvp)
28#define getbinuptime(_bt) _Timecounter_Getbinuptime(_bt)
29#define getnanouptime(_tsp) _Timecounter_Getnanouptime(_tsp)
30#define getmicrouptime(_tvp) _Timecounter_Getmicrouptime(_tvp)
31#define getbintime(_bt) _Timecounter_Getbintime(_bt)
32#define getnanotime(_tsp) _Timecounter_Getnanotime(_tsp)
33#define getmicrotime(_tvp) _Timecounter_Getmicrotime(_tvp)
34#define getboottime(_tvp) _Timecounter_Getboottime(_tvp)
35#define getboottimebin(_bt) _Timecounter_Getboottimebin(_bt)
36#define tc_init _Timecounter_Install
37#define timecounter _Timecounter
38#define time_second _Timecounter_Time_second
39#define time_uptime _Timecounter_Time_uptime
40#include <rtems/score/timecounterimpl.h>
41#include <rtems/score/atomic.h>
42#include <rtems/score/smp.h>
43#include <rtems/score/todimpl.h>
44#include <rtems/score/watchdogimpl.h>
45#endif /* __rtems__ */
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD r284178 2015-06-09T11:49:56Z$");
48
49#include "opt_compat.h"
50#include "opt_ntp.h"
51#include "opt_ffclock.h"
52
53#include <sys/param.h>
54#ifndef __rtems__
55#include <sys/kernel.h>
56#include <sys/limits.h>
57#include <sys/lock.h>
58#include <sys/mutex.h>
59#include <sys/sbuf.h>
60#include <sys/sysctl.h>
61#include <sys/syslog.h>
62#include <sys/systm.h>
63#endif /* __rtems__ */
64#include <sys/timeffc.h>
65#include <sys/timepps.h>
66#include <sys/timetc.h>
67#include <sys/timex.h>
68#ifndef __rtems__
69#include <sys/vdso.h>
70#endif /* __rtems__ */
71#ifdef __rtems__
72#include <limits.h>
73#include <string.h>
74#include <rtems.h>
75ISR_LOCK_DEFINE(, _Timecounter_Lock, "Timecounter")
76#define _Timecounter_Release(lock_context) \
77  _ISR_lock_Release_and_ISR_enable(&_Timecounter_Lock, lock_context)
78#define hz rtems_clock_get_ticks_per_second()
79#define printf(...)
80#define bcopy(x, y, z) memcpy(y, x, z);
81#define log(...)
82static inline int
83builtin_fls(int x)
84{
85        return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
86}
87#define fls(x) builtin_fls(x)
88/* FIXME: https://devel.rtems.org/ticket/2348 */
89#define ntp_update_second(a, b) do { (void) a; (void) b; } while (0)
90
91static inline void
92atomic_thread_fence_acq(void)
93{
94
95        _Atomic_Fence(ATOMIC_ORDER_ACQUIRE);
96}
97
98static inline void
99atomic_thread_fence_rel(void)
100{
101
102        _Atomic_Fence(ATOMIC_ORDER_RELEASE);
103}
104
105static inline u_int
106atomic_load_acq_int(Atomic_Uint *i)
107{
108
109        return (_Atomic_Load_uint(i, ATOMIC_ORDER_ACQUIRE));
110}
111
112static inline void
113atomic_store_rel_int(Atomic_Uint *i, u_int val)
114{
115
116        _Atomic_Store_uint(i, val, ATOMIC_ORDER_RELEASE);
117}
118#endif /* __rtems__ */
119
120/*
121 * A large step happens on boot.  This constant detects such steps.
122 * It is relatively small so that ntp_update_second gets called enough
123 * in the typical 'missed a couple of seconds' case, but doesn't loop
124 * forever when the time step is large.
125 */
126#define LARGE_STEP      200
127
128/*
129 * Implement a dummy timecounter which we can use until we get a real one
130 * in the air.  This allows the console and other early stuff to use
131 * time services.
132 */
133
134static uint32_t
135dummy_get_timecount(struct timecounter *tc)
136{
137#ifndef __rtems__
138        static uint32_t now;
139
140        return (++now);
141#else /* __rtems__ */
142        return 0;
143#endif /* __rtems__ */
144}
145
146static struct timecounter dummy_timecounter = {
147        dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
148};
149
150struct timehands {
151        /* These fields must be initialized by the driver. */
152        struct timecounter      *th_counter;
153        int64_t                 th_adjustment;
154        uint64_t                th_scale;
155        uint32_t                th_offset_count;
156        struct bintime          th_offset;
157        struct bintime          th_bintime;
158        struct timeval          th_microtime;
159        struct timespec         th_nanotime;
160        struct bintime          th_boottime;
161        /* Fields not to be copied in tc_windup start with th_generation. */
162#ifndef __rtems__
163        u_int                   th_generation;
164#else /* __rtems__ */
165        Atomic_Uint             th_generation;
166#endif /* __rtems__ */
167        struct timehands        *th_next;
168};
169
170#if defined(RTEMS_SMP)
171static struct timehands th0;
172static struct timehands th1 = {
173        .th_next = &th0
174};
175#endif
176static struct timehands th0 = {
177        .th_counter = &dummy_timecounter,
178        .th_scale = (uint64_t)-1 / 1000000,
179        .th_offset = { .sec = 1 },
180        .th_generation = 1,
181#ifdef __rtems__
182        .th_bintime = { .sec = TOD_SECONDS_1970_THROUGH_1988 },
183        .th_microtime = { TOD_SECONDS_1970_THROUGH_1988, 0 },
184        .th_nanotime = { TOD_SECONDS_1970_THROUGH_1988, 0 },
185        .th_boottime = { .sec = TOD_SECONDS_1970_THROUGH_1988 - 1 },
186#endif /* __rtems__ */
187#if defined(RTEMS_SMP)
188        .th_next = &th1
189#else
190        .th_next = &th0
191#endif
192};
193
194static struct timehands *volatile timehands = &th0;
195struct timecounter *timecounter = &dummy_timecounter;
196static struct timecounter *timecounters = &dummy_timecounter;
197
198#ifndef __rtems__
199int tc_min_ticktock_freq = 1;
200#endif /* __rtems__ */
201
202#ifndef __rtems__
203volatile time_t time_second = 1;
204#else /* __rtems__ */
205volatile time_t time_second = TOD_SECONDS_1970_THROUGH_1988;
206#endif /* __rtems__ */
207volatile time_t time_uptime = 1;
208
209#ifndef __rtems__
210static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
211SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
212    NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
213
214SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
215static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
216
217static int timestepwarnings;
218SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
219    &timestepwarnings, 0, "Log time steps");
220
221struct bintime bt_timethreshold;
222struct bintime bt_tickthreshold;
223sbintime_t sbt_timethreshold;
224sbintime_t sbt_tickthreshold;
225struct bintime tc_tick_bt;
226sbintime_t tc_tick_sbt;
227int tc_precexp;
228int tc_timepercentage = TC_DEFAULTPERC;
229static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS);
230SYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation,
231    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
232    sysctl_kern_timecounter_adjprecision, "I",
233    "Allowed time interval deviation in percents");
234
235static int tc_chosen;   /* Non-zero if a specific tc was chosen via sysctl. */
236#endif /* __rtems__ */
237
238static void tc_windup(struct bintime *new_boottimebin);
239#ifndef __rtems__
240static void cpu_tick_calibrate(int);
241#else /* __rtems__ */
242static void _Timecounter_Windup(struct bintime *new_boottimebin,
243    ISR_lock_Context *lock_context);
244#endif /* __rtems__ */
245
246void dtrace_getnanotime(struct timespec *tsp);
247
248#ifndef __rtems__
249static int
250sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
251{
252        struct timeval boottime;
253
254        getboottime(&boottime);
255
256#ifndef __mips__
257#ifdef SCTL_MASK32
258        int tv[2];
259
260        if (req->flags & SCTL_MASK32) {
261                tv[0] = boottime.tv_sec;
262                tv[1] = boottime.tv_usec;
263                return (SYSCTL_OUT(req, tv, sizeof(tv)));
264        }
265#endif
266#endif
267        return (SYSCTL_OUT(req, &boottime, sizeof(boottime)));
268}
269
270static int
271sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
272{
273        uint32_t ncount;
274        struct timecounter *tc = arg1;
275
276        ncount = tc->tc_get_timecount(tc);
277        return (sysctl_handle_int(oidp, &ncount, 0, req));
278}
279
280static int
281sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
282{
283        uint64_t freq;
284        struct timecounter *tc = arg1;
285
286        freq = tc->tc_frequency;
287        return (sysctl_handle_64(oidp, &freq, 0, req));
288}
289#endif /* __rtems__ */
290
291/*
292 * Return the difference between the timehands' counter value now and what
293 * was when we copied it to the timehands' offset_count.
294 */
295static __inline uint32_t
296tc_delta(struct timehands *th)
297{
298        struct timecounter *tc;
299
300        tc = th->th_counter;
301        return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
302            tc->tc_counter_mask);
303}
304
305/*
306 * Functions for reading the time.  We have to loop until we are sure that
307 * the timehands that we operated on was not updated under our feet.  See
308 * the comment in <sys/time.h> for a description of these 12 functions.
309 */
310
311#ifdef FFCLOCK
312void
313fbclock_binuptime(struct bintime *bt)
314{
315        struct timehands *th;
316        unsigned int gen;
317
318        do {
319                th = timehands;
320                gen = atomic_load_acq_int(&th->th_generation);
321                *bt = th->th_offset;
322                bintime_addx(bt, th->th_scale * tc_delta(th));
323                atomic_thread_fence_acq();
324        } while (gen == 0 || gen != th->th_generation);
325}
326
327void
328fbclock_nanouptime(struct timespec *tsp)
329{
330        struct bintime bt;
331
332        fbclock_binuptime(&bt);
333        bintime2timespec(&bt, tsp);
334}
335
336void
337fbclock_microuptime(struct timeval *tvp)
338{
339        struct bintime bt;
340
341        fbclock_binuptime(&bt);
342        bintime2timeval(&bt, tvp);
343}
344
345void
346fbclock_bintime(struct bintime *bt)
347{
348        struct timehands *th;
349        unsigned int gen;
350
351        do {
352                th = timehands;
353                gen = atomic_load_acq_int(&th->th_generation);
354                *bt = th->th_bintime;
355                bintime_addx(bt, th->th_scale * tc_delta(th));
356                atomic_thread_fence_acq();
357        } while (gen == 0 || gen != th->th_generation);
358}
359
360void
361fbclock_nanotime(struct timespec *tsp)
362{
363        struct bintime bt;
364
365        fbclock_bintime(&bt);
366        bintime2timespec(&bt, tsp);
367}
368
369void
370fbclock_microtime(struct timeval *tvp)
371{
372        struct bintime bt;
373
374        fbclock_bintime(&bt);
375        bintime2timeval(&bt, tvp);
376}
377
378void
379fbclock_getbinuptime(struct bintime *bt)
380{
381        struct timehands *th;
382        unsigned int gen;
383
384        do {
385                th = timehands;
386                gen = atomic_load_acq_int(&th->th_generation);
387                *bt = th->th_offset;
388                atomic_thread_fence_acq();
389        } while (gen == 0 || gen != th->th_generation);
390}
391
392void
393fbclock_getnanouptime(struct timespec *tsp)
394{
395        struct timehands *th;
396        unsigned int gen;
397
398        do {
399                th = timehands;
400                gen = atomic_load_acq_int(&th->th_generation);
401                bintime2timespec(&th->th_offset, tsp);
402                atomic_thread_fence_acq();
403        } while (gen == 0 || gen != th->th_generation);
404}
405
406void
407fbclock_getmicrouptime(struct timeval *tvp)
408{
409        struct timehands *th;
410        unsigned int gen;
411
412        do {
413                th = timehands;
414                gen = atomic_load_acq_int(&th->th_generation);
415                bintime2timeval(&th->th_offset, tvp);
416                atomic_thread_fence_acq();
417        } while (gen == 0 || gen != th->th_generation);
418}
419
420void
421fbclock_getbintime(struct bintime *bt)
422{
423        struct timehands *th;
424        unsigned int gen;
425
426        do {
427                th = timehands;
428                gen = atomic_load_acq_int(&th->th_generation);
429                *bt = th->th_bintime;
430                atomic_thread_fence_acq();
431        } while (gen == 0 || gen != th->th_generation);
432}
433
434void
435fbclock_getnanotime(struct timespec *tsp)
436{
437        struct timehands *th;
438        unsigned int gen;
439
440        do {
441                th = timehands;
442                gen = atomic_load_acq_int(&th->th_generation);
443                *tsp = th->th_nanotime;
444                atomic_thread_fence_acq();
445        } while (gen == 0 || gen != th->th_generation);
446}
447
448void
449fbclock_getmicrotime(struct timeval *tvp)
450{
451        struct timehands *th;
452        unsigned int gen;
453
454        do {
455                th = timehands;
456                gen = atomic_load_acq_int(&th->th_generation);
457                *tvp = th->th_microtime;
458                atomic_thread_fence_acq();
459        } while (gen == 0 || gen != th->th_generation);
460}
461#else /* !FFCLOCK */
462void
463binuptime(struct bintime *bt)
464{
465        struct timehands *th;
466        uint32_t gen;
467
468        do {
469                th = timehands;
470                gen = atomic_load_acq_int(&th->th_generation);
471                *bt = th->th_offset;
472                bintime_addx(bt, th->th_scale * tc_delta(th));
473                atomic_thread_fence_acq();
474        } while (gen == 0 || gen != th->th_generation);
475}
476#ifdef __rtems__
477sbintime_t
478_Timecounter_Sbinuptime(void)
479{
480        struct timehands *th;
481        uint32_t gen;
482        sbintime_t sbt;
483
484        do {
485                th = timehands;
486                gen = atomic_load_acq_int(&th->th_generation);
487                sbt = bttosbt(th->th_offset);
488                sbt += (th->th_scale * tc_delta(th)) >> 32;
489                atomic_thread_fence_acq();
490        } while (gen == 0 || gen != th->th_generation);
491
492        return (sbt);
493}
494#endif /* __rtems__ */
495
496void
497nanouptime(struct timespec *tsp)
498{
499        struct bintime bt;
500
501        binuptime(&bt);
502        bintime2timespec(&bt, tsp);
503}
504
505void
506microuptime(struct timeval *tvp)
507{
508        struct bintime bt;
509
510        binuptime(&bt);
511        bintime2timeval(&bt, tvp);
512}
513
514void
515bintime(struct bintime *bt)
516{
517        struct timehands *th;
518        u_int gen;
519
520        do {
521                th = timehands;
522                gen = atomic_load_acq_int(&th->th_generation);
523                *bt = th->th_bintime;
524                bintime_addx(bt, th->th_scale * tc_delta(th));
525                atomic_thread_fence_acq();
526        } while (gen == 0 || gen != th->th_generation);
527}
528
529void
530nanotime(struct timespec *tsp)
531{
532        struct bintime bt;
533
534        bintime(&bt);
535        bintime2timespec(&bt, tsp);
536}
537
538void
539microtime(struct timeval *tvp)
540{
541        struct bintime bt;
542
543        bintime(&bt);
544        bintime2timeval(&bt, tvp);
545}
546
547void
548getbinuptime(struct bintime *bt)
549{
550        struct timehands *th;
551        uint32_t gen;
552
553        do {
554                th = timehands;
555                gen = atomic_load_acq_int(&th->th_generation);
556                *bt = th->th_offset;
557                atomic_thread_fence_acq();
558        } while (gen == 0 || gen != th->th_generation);
559}
560
561void
562getnanouptime(struct timespec *tsp)
563{
564        struct timehands *th;
565        uint32_t gen;
566
567        do {
568                th = timehands;
569                gen = atomic_load_acq_int(&th->th_generation);
570                bintime2timespec(&th->th_offset, tsp);
571                atomic_thread_fence_acq();
572        } while (gen == 0 || gen != th->th_generation);
573}
574
575void
576getmicrouptime(struct timeval *tvp)
577{
578        struct timehands *th;
579        uint32_t gen;
580
581        do {
582                th = timehands;
583                gen = atomic_load_acq_int(&th->th_generation);
584                bintime2timeval(&th->th_offset, tvp);
585                atomic_thread_fence_acq();
586        } while (gen == 0 || gen != th->th_generation);
587}
588
589void
590getbintime(struct bintime *bt)
591{
592        struct timehands *th;
593        uint32_t gen;
594
595        do {
596                th = timehands;
597                gen = atomic_load_acq_int(&th->th_generation);
598                *bt = th->th_bintime;
599                atomic_thread_fence_acq();
600        } while (gen == 0 || gen != th->th_generation);
601}
602
603void
604getnanotime(struct timespec *tsp)
605{
606        struct timehands *th;
607        uint32_t gen;
608
609        do {
610                th = timehands;
611                gen = atomic_load_acq_int(&th->th_generation);
612                *tsp = th->th_nanotime;
613                atomic_thread_fence_acq();
614        } while (gen == 0 || gen != th->th_generation);
615}
616
617void
618getmicrotime(struct timeval *tvp)
619{
620        struct timehands *th;
621        uint32_t gen;
622
623        do {
624                th = timehands;
625                gen = atomic_load_acq_int(&th->th_generation);
626                *tvp = th->th_microtime;
627                atomic_thread_fence_acq();
628        } while (gen == 0 || gen != th->th_generation);
629}
630#endif /* FFCLOCK */
631
632void
633getboottime(struct timeval *boottime)
634{
635        struct bintime boottimebin;
636
637        getboottimebin(&boottimebin);
638        bintime2timeval(&boottimebin, boottime);
639}
640
641void
642getboottimebin(struct bintime *boottimebin)
643{
644        struct timehands *th;
645        u_int gen;
646
647        do {
648                th = timehands;
649                gen = atomic_load_acq_int(&th->th_generation);
650                *boottimebin = th->th_boottime;
651                atomic_thread_fence_acq();
652        } while (gen == 0 || gen != th->th_generation);
653}
654
655#ifdef FFCLOCK
656/*
657 * Support for feed-forward synchronization algorithms. This is heavily inspired
658 * by the timehands mechanism but kept independent from it. *_windup() functions
659 * have some connection to avoid accessing the timecounter hardware more than
660 * necessary.
661 */
662
663/* Feed-forward clock estimates kept updated by the synchronization daemon. */
664struct ffclock_estimate ffclock_estimate;
665struct bintime ffclock_boottime;        /* Feed-forward boot time estimate. */
666uint32_t ffclock_status;                /* Feed-forward clock status. */
667int8_t ffclock_updated;                 /* New estimates are available. */
668struct mtx ffclock_mtx;                 /* Mutex on ffclock_estimate. */
669
670struct fftimehands {
671        struct ffclock_estimate cest;
672        struct bintime          tick_time;
673        struct bintime          tick_time_lerp;
674        ffcounter               tick_ffcount;
675        uint64_t                period_lerp;
676        volatile uint8_t        gen;
677        struct fftimehands      *next;
678};
679
680#define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
681
682static struct fftimehands ffth[10];
683static struct fftimehands *volatile fftimehands = ffth;
684
685static void
686ffclock_init(void)
687{
688        struct fftimehands *cur;
689        struct fftimehands *last;
690
691        memset(ffth, 0, sizeof(ffth));
692
693        last = ffth + NUM_ELEMENTS(ffth) - 1;
694        for (cur = ffth; cur < last; cur++)
695                cur->next = cur + 1;
696        last->next = ffth;
697
698        ffclock_updated = 0;
699        ffclock_status = FFCLOCK_STA_UNSYNC;
700        mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
701}
702
703/*
704 * Reset the feed-forward clock estimates. Called from inittodr() to get things
705 * kick started and uses the timecounter nominal frequency as a first period
706 * estimate. Note: this function may be called several time just after boot.
707 * Note: this is the only function that sets the value of boot time for the
708 * monotonic (i.e. uptime) version of the feed-forward clock.
709 */
710void
711ffclock_reset_clock(struct timespec *ts)
712{
713        struct timecounter *tc;
714        struct ffclock_estimate cest;
715
716        tc = timehands->th_counter;
717        memset(&cest, 0, sizeof(struct ffclock_estimate));
718
719        timespec2bintime(ts, &ffclock_boottime);
720        timespec2bintime(ts, &(cest.update_time));
721        ffclock_read_counter(&cest.update_ffcount);
722        cest.leapsec_next = 0;
723        cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
724        cest.errb_abs = 0;
725        cest.errb_rate = 0;
726        cest.status = FFCLOCK_STA_UNSYNC;
727        cest.leapsec_total = 0;
728        cest.leapsec = 0;
729
730        mtx_lock(&ffclock_mtx);
731        bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
732        ffclock_updated = INT8_MAX;
733        mtx_unlock(&ffclock_mtx);
734
735        printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
736            (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
737            (unsigned long)ts->tv_nsec);
738}
739
740/*
741 * Sub-routine to convert a time interval measured in RAW counter units to time
742 * in seconds stored in bintime format.
743 * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
744 * larger than the max value of u_int (on 32 bit architecture). Loop to consume
745 * extra cycles.
746 */
747static void
748ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
749{
750        struct bintime bt2;
751        ffcounter delta, delta_max;
752
753        delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
754        bintime_clear(bt);
755        do {
756                if (ffdelta > delta_max)
757                        delta = delta_max;
758                else
759                        delta = ffdelta;
760                bt2.sec = 0;
761                bt2.frac = period;
762                bintime_mul(&bt2, (unsigned int)delta);
763                bintime_add(bt, &bt2);
764                ffdelta -= delta;
765        } while (ffdelta > 0);
766}
767
768/*
769 * Update the fftimehands.
770 * Push the tick ffcount and time(s) forward based on current clock estimate.
771 * The conversion from ffcounter to bintime relies on the difference clock
772 * principle, whose accuracy relies on computing small time intervals. If a new
773 * clock estimate has been passed by the synchronisation daemon, make it
774 * current, and compute the linear interpolation for monotonic time if needed.
775 */
776static void
777ffclock_windup(unsigned int delta)
778{
779        struct ffclock_estimate *cest;
780        struct fftimehands *ffth;
781        struct bintime bt, gap_lerp;
782        ffcounter ffdelta;
783        uint64_t frac;
784        unsigned int polling;
785        uint8_t forward_jump, ogen;
786
787        /*
788         * Pick the next timehand, copy current ffclock estimates and move tick
789         * times and counter forward.
790         */
791        forward_jump = 0;
792        ffth = fftimehands->next;
793        ogen = ffth->gen;
794        ffth->gen = 0;
795        cest = &ffth->cest;
796        bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
797        ffdelta = (ffcounter)delta;
798        ffth->period_lerp = fftimehands->period_lerp;
799
800        ffth->tick_time = fftimehands->tick_time;
801        ffclock_convert_delta(ffdelta, cest->period, &bt);
802        bintime_add(&ffth->tick_time, &bt);
803
804        ffth->tick_time_lerp = fftimehands->tick_time_lerp;
805        ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
806        bintime_add(&ffth->tick_time_lerp, &bt);
807
808        ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
809
810        /*
811         * Assess the status of the clock, if the last update is too old, it is
812         * likely the synchronisation daemon is dead and the clock is free
813         * running.
814         */
815        if (ffclock_updated == 0) {
816                ffdelta = ffth->tick_ffcount - cest->update_ffcount;
817                ffclock_convert_delta(ffdelta, cest->period, &bt);
818                if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
819                        ffclock_status |= FFCLOCK_STA_UNSYNC;
820        }
821
822        /*
823         * If available, grab updated clock estimates and make them current.
824         * Recompute time at this tick using the updated estimates. The clock
825         * estimates passed the feed-forward synchronisation daemon may result
826         * in time conversion that is not monotonically increasing (just after
827         * the update). time_lerp is a particular linear interpolation over the
828         * synchronisation algo polling period that ensures monotonicity for the
829         * clock ids requesting it.
830         */
831        if (ffclock_updated > 0) {
832                bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
833                ffdelta = ffth->tick_ffcount - cest->update_ffcount;
834                ffth->tick_time = cest->update_time;
835                ffclock_convert_delta(ffdelta, cest->period, &bt);
836                bintime_add(&ffth->tick_time, &bt);
837
838                /* ffclock_reset sets ffclock_updated to INT8_MAX */
839                if (ffclock_updated == INT8_MAX)
840                        ffth->tick_time_lerp = ffth->tick_time;
841
842                if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
843                        forward_jump = 1;
844                else
845                        forward_jump = 0;
846
847                bintime_clear(&gap_lerp);
848                if (forward_jump) {
849                        gap_lerp = ffth->tick_time;
850                        bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
851                } else {
852                        gap_lerp = ffth->tick_time_lerp;
853                        bintime_sub(&gap_lerp, &ffth->tick_time);
854                }
855
856                /*
857                 * The reset from the RTC clock may be far from accurate, and
858                 * reducing the gap between real time and interpolated time
859                 * could take a very long time if the interpolated clock insists
860                 * on strict monotonicity. The clock is reset under very strict
861                 * conditions (kernel time is known to be wrong and
862                 * synchronization daemon has been restarted recently.
863                 * ffclock_boottime absorbs the jump to ensure boot time is
864                 * correct and uptime functions stay consistent.
865                 */
866                if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
867                    ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
868                    ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
869                        if (forward_jump)
870                                bintime_add(&ffclock_boottime, &gap_lerp);
871                        else
872                                bintime_sub(&ffclock_boottime, &gap_lerp);
873                        ffth->tick_time_lerp = ffth->tick_time;
874                        bintime_clear(&gap_lerp);
875                }
876
877                ffclock_status = cest->status;
878                ffth->period_lerp = cest->period;
879
880                /*
881                 * Compute corrected period used for the linear interpolation of
882                 * time. The rate of linear interpolation is capped to 5000PPM
883                 * (5ms/s).
884                 */
885                if (bintime_isset(&gap_lerp)) {
886                        ffdelta = cest->update_ffcount;
887                        ffdelta -= fftimehands->cest.update_ffcount;
888                        ffclock_convert_delta(ffdelta, cest->period, &bt);
889                        polling = bt.sec;
890                        bt.sec = 0;
891                        bt.frac = 5000000 * (uint64_t)18446744073LL;
892                        bintime_mul(&bt, polling);
893                        if (bintime_cmp(&gap_lerp, &bt, >))
894                                gap_lerp = bt;
895
896                        /* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
897                        frac = 0;
898                        if (gap_lerp.sec > 0) {
899                                frac -= 1;
900                                frac /= ffdelta / gap_lerp.sec;
901                        }
902                        frac += gap_lerp.frac / ffdelta;
903
904                        if (forward_jump)
905                                ffth->period_lerp += frac;
906                        else
907                                ffth->period_lerp -= frac;
908                }
909
910                ffclock_updated = 0;
911        }
912        if (++ogen == 0)
913                ogen = 1;
914        ffth->gen = ogen;
915        fftimehands = ffth;
916}
917
918/*
919 * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
920 * the old and new hardware counter cannot be read simultaneously. tc_windup()
921 * does read the two counters 'back to back', but a few cycles are effectively
922 * lost, and not accumulated in tick_ffcount. This is a fairly radical
923 * operation for a feed-forward synchronization daemon, and it is its job to not
924 * pushing irrelevant data to the kernel. Because there is no locking here,
925 * simply force to ignore pending or next update to give daemon a chance to
926 * realize the counter has changed.
927 */
928static void
929ffclock_change_tc(struct timehands *th)
930{
931        struct fftimehands *ffth;
932        struct ffclock_estimate *cest;
933        struct timecounter *tc;
934        uint8_t ogen;
935
936        tc = th->th_counter;
937        ffth = fftimehands->next;
938        ogen = ffth->gen;
939        ffth->gen = 0;
940
941        cest = &ffth->cest;
942        bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
943        cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
944        cest->errb_abs = 0;
945        cest->errb_rate = 0;
946        cest->status |= FFCLOCK_STA_UNSYNC;
947
948        ffth->tick_ffcount = fftimehands->tick_ffcount;
949        ffth->tick_time_lerp = fftimehands->tick_time_lerp;
950        ffth->tick_time = fftimehands->tick_time;
951        ffth->period_lerp = cest->period;
952
953        /* Do not lock but ignore next update from synchronization daemon. */
954        ffclock_updated--;
955
956        if (++ogen == 0)
957                ogen = 1;
958        ffth->gen = ogen;
959        fftimehands = ffth;
960}
961
962/*
963 * Retrieve feed-forward counter and time of last kernel tick.
964 */
965void
966ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
967{
968        struct fftimehands *ffth;
969        uint8_t gen;
970
971        /*
972         * No locking but check generation has not changed. Also need to make
973         * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
974         */
975        do {
976                ffth = fftimehands;
977                gen = ffth->gen;
978                if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
979                        *bt = ffth->tick_time_lerp;
980                else
981                        *bt = ffth->tick_time;
982                *ffcount = ffth->tick_ffcount;
983        } while (gen == 0 || gen != ffth->gen);
984}
985
986/*
987 * Absolute clock conversion. Low level function to convert ffcounter to
988 * bintime. The ffcounter is converted using the current ffclock period estimate
989 * or the "interpolated period" to ensure monotonicity.
990 * NOTE: this conversion may have been deferred, and the clock updated since the
991 * hardware counter has been read.
992 */
993void
994ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
995{
996        struct fftimehands *ffth;
997        struct bintime bt2;
998        ffcounter ffdelta;
999        uint8_t gen;
1000
1001        /*
1002         * No locking but check generation has not changed. Also need to make
1003         * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
1004         */
1005        do {
1006                ffth = fftimehands;
1007                gen = ffth->gen;
1008                if (ffcount > ffth->tick_ffcount)
1009                        ffdelta = ffcount - ffth->tick_ffcount;
1010                else
1011                        ffdelta = ffth->tick_ffcount - ffcount;
1012
1013                if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
1014                        *bt = ffth->tick_time_lerp;
1015                        ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
1016                } else {
1017                        *bt = ffth->tick_time;
1018                        ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
1019                }
1020
1021                if (ffcount > ffth->tick_ffcount)
1022                        bintime_add(bt, &bt2);
1023                else
1024                        bintime_sub(bt, &bt2);
1025        } while (gen == 0 || gen != ffth->gen);
1026}
1027
1028/*
1029 * Difference clock conversion.
1030 * Low level function to Convert a time interval measured in RAW counter units
1031 * into bintime. The difference clock allows measuring small intervals much more
1032 * reliably than the absolute clock.
1033 */
1034void
1035ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
1036{
1037        struct fftimehands *ffth;
1038        uint8_t gen;
1039
1040        /* No locking but check generation has not changed. */
1041        do {
1042                ffth = fftimehands;
1043                gen = ffth->gen;
1044                ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
1045        } while (gen == 0 || gen != ffth->gen);
1046}
1047
1048/*
1049 * Access to current ffcounter value.
1050 */
1051void
1052ffclock_read_counter(ffcounter *ffcount)
1053{
1054        struct timehands *th;
1055        struct fftimehands *ffth;
1056        unsigned int gen, delta;
1057
1058        /*
1059         * ffclock_windup() called from tc_windup(), safe to rely on
1060         * th->th_generation only, for correct delta and ffcounter.
1061         */
1062        do {
1063                th = timehands;
1064                gen = atomic_load_acq_int(&th->th_generation);
1065                ffth = fftimehands;
1066                delta = tc_delta(th);
1067                *ffcount = ffth->tick_ffcount;
1068                atomic_thread_fence_acq();
1069        } while (gen == 0 || gen != th->th_generation);
1070
1071        *ffcount += delta;
1072}
1073
1074void
1075binuptime(struct bintime *bt)
1076{
1077
1078        binuptime_fromclock(bt, sysclock_active);
1079}
1080
1081void
1082nanouptime(struct timespec *tsp)
1083{
1084
1085        nanouptime_fromclock(tsp, sysclock_active);
1086}
1087
1088void
1089microuptime(struct timeval *tvp)
1090{
1091
1092        microuptime_fromclock(tvp, sysclock_active);
1093}
1094
1095void
1096bintime(struct bintime *bt)
1097{
1098
1099        bintime_fromclock(bt, sysclock_active);
1100}
1101
1102void
1103nanotime(struct timespec *tsp)
1104{
1105
1106        nanotime_fromclock(tsp, sysclock_active);
1107}
1108
1109void
1110microtime(struct timeval *tvp)
1111{
1112
1113        microtime_fromclock(tvp, sysclock_active);
1114}
1115
1116void
1117getbinuptime(struct bintime *bt)
1118{
1119
1120        getbinuptime_fromclock(bt, sysclock_active);
1121}
1122
1123void
1124getnanouptime(struct timespec *tsp)
1125{
1126
1127        getnanouptime_fromclock(tsp, sysclock_active);
1128}
1129
1130void
1131getmicrouptime(struct timeval *tvp)
1132{
1133
1134        getmicrouptime_fromclock(tvp, sysclock_active);
1135}
1136
1137void
1138getbintime(struct bintime *bt)
1139{
1140
1141        getbintime_fromclock(bt, sysclock_active);
1142}
1143
1144void
1145getnanotime(struct timespec *tsp)
1146{
1147
1148        getnanotime_fromclock(tsp, sysclock_active);
1149}
1150
1151void
1152getmicrotime(struct timeval *tvp)
1153{
1154
1155        getmicrouptime_fromclock(tvp, sysclock_active);
1156}
1157
1158#endif /* FFCLOCK */
1159
1160#ifndef __rtems__
1161/*
1162 * This is a clone of getnanotime and used for walltimestamps.
1163 * The dtrace_ prefix prevents fbt from creating probes for
1164 * it so walltimestamp can be safely used in all fbt probes.
1165 */
1166void
1167dtrace_getnanotime(struct timespec *tsp)
1168{
1169        struct timehands *th;
1170        uint32_t gen;
1171
1172        do {
1173                th = timehands;
1174                gen = atomic_load_acq_int(&th->th_generation);
1175                *tsp = th->th_nanotime;
1176                atomic_thread_fence_acq();
1177        } while (gen == 0 || gen != th->th_generation);
1178}
1179#endif /* __rtems__ */
1180
1181#ifdef FFCLOCK
1182/*
1183 * System clock currently providing time to the system. Modifiable via sysctl
1184 * when the FFCLOCK option is defined.
1185 */
1186int sysclock_active = SYSCLOCK_FBCK;
1187#endif
1188
1189/* Internal NTP status and error estimates. */
1190extern int time_status;
1191extern long time_esterror;
1192
1193#ifndef __rtems__
1194/*
1195 * Take a snapshot of sysclock data which can be used to compare system clocks
1196 * and generate timestamps after the fact.
1197 */
1198void
1199sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
1200{
1201        struct fbclock_info *fbi;
1202        struct timehands *th;
1203        struct bintime bt;
1204        unsigned int delta, gen;
1205#ifdef FFCLOCK
1206        ffcounter ffcount;
1207        struct fftimehands *ffth;
1208        struct ffclock_info *ffi;
1209        struct ffclock_estimate cest;
1210
1211        ffi = &clock_snap->ff_info;
1212#endif
1213
1214        fbi = &clock_snap->fb_info;
1215        delta = 0;
1216
1217        do {
1218                th = timehands;
1219                gen = atomic_load_acq_int(&th->th_generation);
1220                fbi->th_scale = th->th_scale;
1221                fbi->tick_time = th->th_offset;
1222#ifdef FFCLOCK
1223                ffth = fftimehands;
1224                ffi->tick_time = ffth->tick_time_lerp;
1225                ffi->tick_time_lerp = ffth->tick_time_lerp;
1226                ffi->period = ffth->cest.period;
1227                ffi->period_lerp = ffth->period_lerp;
1228                clock_snap->ffcount = ffth->tick_ffcount;
1229                cest = ffth->cest;
1230#endif
1231                if (!fast)
1232                        delta = tc_delta(th);
1233                atomic_thread_fence_acq();
1234        } while (gen == 0 || gen != th->th_generation);
1235
1236        clock_snap->delta = delta;
1237#ifdef FFCLOCK
1238        clock_snap->sysclock_active = sysclock_active;
1239#endif
1240
1241        /* Record feedback clock status and error. */
1242        clock_snap->fb_info.status = time_status;
1243        /* XXX: Very crude estimate of feedback clock error. */
1244        bt.sec = time_esterror / 1000000;
1245        bt.frac = ((time_esterror - bt.sec) * 1000000) *
1246            (uint64_t)18446744073709ULL;
1247        clock_snap->fb_info.error = bt;
1248
1249#ifdef FFCLOCK
1250        if (!fast)
1251                clock_snap->ffcount += delta;
1252
1253        /* Record feed-forward clock leap second adjustment. */
1254        ffi->leapsec_adjustment = cest.leapsec_total;
1255        if (clock_snap->ffcount > cest.leapsec_next)
1256                ffi->leapsec_adjustment -= cest.leapsec;
1257
1258        /* Record feed-forward clock status and error. */
1259        clock_snap->ff_info.status = cest.status;
1260        ffcount = clock_snap->ffcount - cest.update_ffcount;
1261        ffclock_convert_delta(ffcount, cest.period, &bt);
1262        /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1263        bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1264        /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1265        bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1266        clock_snap->ff_info.error = bt;
1267#endif
1268}
1269
1270/*
1271 * Convert a sysclock snapshot into a struct bintime based on the specified
1272 * clock source and flags.
1273 */
1274int
1275sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1276    int whichclock, uint32_t flags)
1277{
1278        struct bintime boottimebin;
1279#ifdef FFCLOCK
1280        struct bintime bt2;
1281        uint64_t period;
1282#endif
1283
1284        switch (whichclock) {
1285        case SYSCLOCK_FBCK:
1286                *bt = cs->fb_info.tick_time;
1287
1288                /* If snapshot was created with !fast, delta will be >0. */
1289                if (cs->delta > 0)
1290                        bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1291
1292                if ((flags & FBCLOCK_UPTIME) == 0) {
1293                        getboottimebin(&boottimebin);
1294                        bintime_add(bt, &boottimebin);
1295                }
1296                break;
1297#ifdef FFCLOCK
1298        case SYSCLOCK_FFWD:
1299                if (flags & FFCLOCK_LERP) {
1300                        *bt = cs->ff_info.tick_time_lerp;
1301                        period = cs->ff_info.period_lerp;
1302                } else {
1303                        *bt = cs->ff_info.tick_time;
1304                        period = cs->ff_info.period;
1305                }
1306
1307                /* If snapshot was created with !fast, delta will be >0. */
1308                if (cs->delta > 0) {
1309                        ffclock_convert_delta(cs->delta, period, &bt2);
1310                        bintime_add(bt, &bt2);
1311                }
1312
1313                /* Leap second adjustment. */
1314                if (flags & FFCLOCK_LEAPSEC)
1315                        bt->sec -= cs->ff_info.leapsec_adjustment;
1316
1317                /* Boot time adjustment, for uptime/monotonic clocks. */
1318                if (flags & FFCLOCK_UPTIME)
1319                        bintime_sub(bt, &ffclock_boottime);
1320                break;
1321#endif
1322        default:
1323                return (EINVAL);
1324                break;
1325        }
1326
1327        return (0);
1328}
1329#endif /* __rtems__ */
1330
1331/*
1332 * Initialize a new timecounter and possibly use it.
1333 */
1334void
1335tc_init(struct timecounter *tc)
1336{
1337#ifndef __rtems__
1338        uint32_t u;
1339        struct sysctl_oid *tc_root;
1340
1341        u = tc->tc_frequency / tc->tc_counter_mask;
1342        /* XXX: We need some margin here, 10% is a guess */
1343        u *= 11;
1344        u /= 10;
1345        if (u > hz && tc->tc_quality >= 0) {
1346                tc->tc_quality = -2000;
1347                if (bootverbose) {
1348                        printf("Timecounter \"%s\" frequency %ju Hz",
1349                            tc->tc_name, (uintmax_t)tc->tc_frequency);
1350                        printf(" -- Insufficient hz, needs at least %u\n", u);
1351                }
1352        } else if (tc->tc_quality >= 0 || bootverbose) {
1353                printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1354                    tc->tc_name, (uintmax_t)tc->tc_frequency,
1355                    tc->tc_quality);
1356        }
1357#endif /* __rtems__ */
1358
1359        tc->tc_next = timecounters;
1360        timecounters = tc;
1361#ifndef __rtems__
1362        /*
1363         * Set up sysctl tree for this counter.
1364         */
1365        tc_root = SYSCTL_ADD_NODE_WITH_LABEL(NULL,
1366            SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1367            CTLFLAG_RW, 0, "timecounter description", "timecounter");
1368        SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1369            "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1370            "mask for implemented bits");
1371        SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1372            "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1373            sysctl_kern_timecounter_get, "IU", "current timecounter value");
1374        SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1375            "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1376             sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1377        SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1378            "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1379            "goodness of time counter");
1380        /*
1381         * Do not automatically switch if the current tc was specifically
1382         * chosen.  Never automatically use a timecounter with negative quality.
1383         * Even though we run on the dummy counter, switching here may be
1384         * worse since this timecounter may not be monotonic.
1385         */
1386        if (tc_chosen)
1387                return;
1388        if (tc->tc_quality < 0)
1389                return;
1390        if (tc->tc_quality < timecounter->tc_quality)
1391                return;
1392        if (tc->tc_quality == timecounter->tc_quality &&
1393            tc->tc_frequency < timecounter->tc_frequency)
1394                return;
1395#endif /* __rtems__ */
1396        (void)tc->tc_get_timecount(tc);
1397        (void)tc->tc_get_timecount(tc);
1398        timecounter = tc;
1399#ifdef __rtems__
1400        tc_windup(NULL);
1401#endif /* __rtems__ */
1402}
1403
1404#ifndef __rtems__
1405/* Report the frequency of the current timecounter. */
1406uint64_t
1407tc_getfrequency(void)
1408{
1409
1410        return (timehands->th_counter->tc_frequency);
1411}
1412
1413static struct mtx tc_setclock_mtx;
1414MTX_SYSINIT(tc_setclock_init, &tc_setclock_mtx, "tcsetc", MTX_SPIN);
1415#endif /* __rtems__ */
1416
1417/*
1418 * Step our concept of UTC.  This is done by modifying our estimate of
1419 * when we booted.
1420 */
1421void
1422#ifndef __rtems__
1423tc_setclock(struct timespec *ts)
1424#else /* __rtems__ */
1425_Timecounter_Set_clock(const struct bintime *_bt,
1426    ISR_lock_Context *lock_context)
1427#endif /* __rtems__ */
1428{
1429#ifndef __rtems__
1430        struct timespec tbef, taft;
1431#endif /* __rtems__ */
1432        struct bintime bt, bt2;
1433
1434#ifndef __rtems__
1435        timespec2bintime(ts, &bt);
1436        nanotime(&tbef);
1437        mtx_lock_spin(&tc_setclock_mtx);
1438        cpu_tick_calibrate(1);
1439#else /* __rtems__ */
1440        bt = *_bt;
1441#endif /* __rtems__ */
1442        binuptime(&bt2);
1443        bintime_sub(&bt, &bt2);
1444
1445        /* XXX fiddle all the little crinkly bits around the fiords... */
1446#ifndef __rtems__
1447        tc_windup(&bt);
1448        mtx_unlock_spin(&tc_setclock_mtx);
1449        if (timestepwarnings) {
1450                nanotime(&taft);
1451                log(LOG_INFO,
1452                    "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1453                    (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1454                    (intmax_t)taft.tv_sec, taft.tv_nsec,
1455                    (intmax_t)ts->tv_sec, ts->tv_nsec);
1456        }
1457#else /* __rtems__ */
1458        _Timecounter_Windup(&bt, lock_context);
1459#endif /* __rtems__ */
1460}
1461
1462/*
1463 * Initialize the next struct timehands in the ring and make
1464 * it the active timehands.  Along the way we might switch to a different
1465 * timecounter and/or do seconds processing in NTP.  Slightly magic.
1466 */
1467static void
1468tc_windup(struct bintime *new_boottimebin)
1469#ifdef __rtems__
1470{
1471        ISR_lock_Context lock_context;
1472
1473        _Timecounter_Acquire(&lock_context);
1474        _Timecounter_Windup(new_boottimebin, &lock_context);
1475}
1476
1477static void
1478_Timecounter_Windup(struct bintime *new_boottimebin,
1479    ISR_lock_Context *lock_context)
1480#endif /* __rtems__ */
1481{
1482        struct bintime bt;
1483        struct timehands *th, *tho;
1484        uint64_t scale;
1485        uint32_t delta, ncount, ogen;
1486        int i;
1487        time_t t;
1488
1489        /*
1490         * Make the next timehands a copy of the current one, but do
1491         * not overwrite the generation or next pointer.  While we
1492         * update the contents, the generation must be zero.  We need
1493         * to ensure that the zero generation is visible before the
1494         * data updates become visible, which requires release fence.
1495         * For similar reasons, re-reading of the generation after the
1496         * data is read should use acquire fence.
1497         */
1498        tho = timehands;
1499#if defined(RTEMS_SMP)
1500        th = tho->th_next;
1501#else
1502        th = tho;
1503#endif
1504        ogen = th->th_generation;
1505        th->th_generation = 0;
1506        atomic_thread_fence_rel();
1507#if defined(RTEMS_SMP)
1508        bcopy(tho, th, offsetof(struct timehands, th_generation));
1509#endif
1510        if (new_boottimebin != NULL)
1511                th->th_boottime = *new_boottimebin;
1512
1513        /*
1514         * Capture a timecounter delta on the current timecounter and if
1515         * changing timecounters, a counter value from the new timecounter.
1516         * Update the offset fields accordingly.
1517         */
1518        delta = tc_delta(th);
1519        if (th->th_counter != timecounter)
1520                ncount = timecounter->tc_get_timecount(timecounter);
1521        else
1522                ncount = 0;
1523#ifdef FFCLOCK
1524        ffclock_windup(delta);
1525#endif
1526        th->th_offset_count += delta;
1527        th->th_offset_count &= th->th_counter->tc_counter_mask;
1528        while (delta > th->th_counter->tc_frequency) {
1529                /* Eat complete unadjusted seconds. */
1530                delta -= th->th_counter->tc_frequency;
1531                th->th_offset.sec++;
1532        }
1533        if ((delta > th->th_counter->tc_frequency / 2) &&
1534            (th->th_scale * delta < ((uint64_t)1 << 63))) {
1535                /* The product th_scale * delta just barely overflows. */
1536                th->th_offset.sec++;
1537        }
1538        bintime_addx(&th->th_offset, th->th_scale * delta);
1539
1540        /*
1541         * Hardware latching timecounters may not generate interrupts on
1542         * PPS events, so instead we poll them.  There is a finite risk that
1543         * the hardware might capture a count which is later than the one we
1544         * got above, and therefore possibly in the next NTP second which might
1545         * have a different rate than the current NTP second.  It doesn't
1546         * matter in practice.
1547         */
1548        if (tho->th_counter->tc_poll_pps)
1549                tho->th_counter->tc_poll_pps(tho->th_counter);
1550
1551        /*
1552         * Deal with NTP second processing.  The for loop normally
1553         * iterates at most once, but in extreme situations it might
1554         * keep NTP sane if timeouts are not run for several seconds.
1555         * At boot, the time step can be large when the TOD hardware
1556         * has been read, so on really large steps, we call
1557         * ntp_update_second only twice.  We need to call it twice in
1558         * case we missed a leap second.
1559         */
1560        bt = th->th_offset;
1561        bintime_add(&bt, &th->th_boottime);
1562        i = bt.sec - tho->th_microtime.tv_sec;
1563        if (i > LARGE_STEP)
1564                i = 2;
1565        for (; i > 0; i--) {
1566                t = bt.sec;
1567                ntp_update_second(&th->th_adjustment, &bt.sec);
1568                if (bt.sec != t)
1569                        th->th_boottime.sec += bt.sec - t;
1570        }
1571        th->th_bintime = th->th_offset;
1572        bintime_add(&th->th_bintime, &th->th_boottime);
1573        /* Update the UTC timestamps used by the get*() functions. */
1574        /* XXX shouldn't do this here.  Should force non-`get' versions. */
1575        bintime2timeval(&bt, &th->th_microtime);
1576        bintime2timespec(&bt, &th->th_nanotime);
1577
1578        /* Now is a good time to change timecounters. */
1579        if (th->th_counter != timecounter) {
1580#ifndef __rtems__
1581#ifndef __arm__
1582                if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0)
1583                        cpu_disable_c2_sleep++;
1584                if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
1585                        cpu_disable_c2_sleep--;
1586#endif
1587#endif /* __rtems__ */
1588                th->th_counter = timecounter;
1589                th->th_offset_count = ncount;
1590#ifndef __rtems__
1591                tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1592                    (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1593#endif /* __rtems__ */
1594#ifdef FFCLOCK
1595                ffclock_change_tc(th);
1596#endif
1597        }
1598
1599        /*-
1600         * Recalculate the scaling factor.  We want the number of 1/2^64
1601         * fractions of a second per period of the hardware counter, taking
1602         * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1603         * processing provides us with.
1604         *
1605         * The th_adjustment is nanoseconds per second with 32 bit binary
1606         * fraction and we want 64 bit binary fraction of second:
1607         *
1608         *       x = a * 2^32 / 10^9 = a * 4.294967296
1609         *
1610         * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1611         * we can only multiply by about 850 without overflowing, that
1612         * leaves no suitably precise fractions for multiply before divide.
1613         *
1614         * Divide before multiply with a fraction of 2199/512 results in a
1615         * systematic undercompensation of 10PPM of th_adjustment.  On a
1616         * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
1617         *
1618         * We happily sacrifice the lowest of the 64 bits of our result
1619         * to the goddess of code clarity.
1620         *
1621         */
1622        scale = (uint64_t)1 << 63;
1623        scale += (th->th_adjustment / 1024) * 2199;
1624        scale /= th->th_counter->tc_frequency;
1625        th->th_scale = scale * 2;
1626
1627        /*
1628         * Now that the struct timehands is again consistent, set the new
1629         * generation number, making sure to not make it zero.
1630         */
1631        if (++ogen == 0)
1632                ogen = 1;
1633        atomic_store_rel_int(&th->th_generation, ogen);
1634
1635        /* Go live with the new struct timehands. */
1636#ifdef FFCLOCK
1637        switch (sysclock_active) {
1638        case SYSCLOCK_FBCK:
1639#endif
1640                time_second = th->th_microtime.tv_sec;
1641                time_uptime = th->th_offset.sec;
1642#ifdef FFCLOCK
1643                break;
1644        case SYSCLOCK_FFWD:
1645                time_second = fftimehands->tick_time_lerp.sec;
1646                time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1647                break;
1648        }
1649#endif
1650
1651#if defined(RTEMS_SMP)
1652        timehands = th;
1653#endif
1654#ifndef __rtems__
1655        timekeep_push_vdso();
1656#endif /* __rtems__ */
1657#ifdef __rtems__
1658        _Timecounter_Release(lock_context);
1659#endif /* __rtems__ */
1660}
1661
1662#ifndef __rtems__
1663/* Report or change the active timecounter hardware. */
1664static int
1665sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1666{
1667        char newname[32];
1668        struct timecounter *newtc, *tc;
1669        int error;
1670
1671        tc = timecounter;
1672        strlcpy(newname, tc->tc_name, sizeof(newname));
1673
1674        error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1675        if (error != 0 || req->newptr == NULL)
1676                return (error);
1677        /* Record that the tc in use now was specifically chosen. */
1678        tc_chosen = 1;
1679        if (strcmp(newname, tc->tc_name) == 0)
1680                return (0);
1681        for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1682                if (strcmp(newname, newtc->tc_name) != 0)
1683                        continue;
1684
1685                /* Warm up new timecounter. */
1686                (void)newtc->tc_get_timecount(newtc);
1687                (void)newtc->tc_get_timecount(newtc);
1688
1689                timecounter = newtc;
1690
1691                /*
1692                 * The vdso timehands update is deferred until the next
1693                 * 'tc_windup()'.
1694                 *
1695                 * This is prudent given that 'timekeep_push_vdso()' does not
1696                 * use any locking and that it can be called in hard interrupt
1697                 * context via 'tc_windup()'.
1698                 */
1699                return (0);
1700        }
1701        return (EINVAL);
1702}
1703
1704SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1705    0, 0, sysctl_kern_timecounter_hardware, "A",
1706    "Timecounter hardware selected");
1707
1708
1709/* Report the available timecounter hardware. */
1710static int
1711sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1712{
1713        struct sbuf sb;
1714        struct timecounter *tc;
1715        int error;
1716
1717        sbuf_new_for_sysctl(&sb, NULL, 0, req);
1718        for (tc = timecounters; tc != NULL; tc = tc->tc_next) {
1719                if (tc != timecounters)
1720                        sbuf_putc(&sb, ' ');
1721                sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality);
1722        }
1723        error = sbuf_finish(&sb);
1724        sbuf_delete(&sb);
1725        return (error);
1726}
1727
1728SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1729    0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1730#endif /* __rtems__ */
1731
1732#ifndef __rtems__
1733/*
1734 * RFC 2783 PPS-API implementation.
1735 */
1736
1737/*
1738 *  Return true if the driver is aware of the abi version extensions in the
1739 *  pps_state structure, and it supports at least the given abi version number.
1740 */
1741static inline int
1742abi_aware(struct pps_state *pps, int vers)
1743{
1744
1745        return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers);
1746}
1747
1748static int
1749pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1750{
1751        int err, timo;
1752        pps_seq_t aseq, cseq;
1753        struct timeval tv;
1754
1755        if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1756                return (EINVAL);
1757
1758        /*
1759         * If no timeout is requested, immediately return whatever values were
1760         * most recently captured.  If timeout seconds is -1, that's a request
1761         * to block without a timeout.  WITNESS won't let us sleep forever
1762         * without a lock (we really don't need a lock), so just repeatedly
1763         * sleep a long time.
1764         */
1765        if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1766                if (fapi->timeout.tv_sec == -1)
1767                        timo = 0x7fffffff;
1768                else {
1769                        tv.tv_sec = fapi->timeout.tv_sec;
1770                        tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1771                        timo = tvtohz(&tv);
1772                }
1773                aseq = pps->ppsinfo.assert_sequence;
1774                cseq = pps->ppsinfo.clear_sequence;
1775                while (aseq == pps->ppsinfo.assert_sequence &&
1776                    cseq == pps->ppsinfo.clear_sequence) {
1777                        if (abi_aware(pps, 1) && pps->driver_mtx != NULL) {
1778                                if (pps->flags & PPSFLAG_MTX_SPIN) {
1779                                        err = msleep_spin(pps, pps->driver_mtx,
1780                                            "ppsfch", timo);
1781                                } else {
1782                                        err = msleep(pps, pps->driver_mtx, PCATCH,
1783                                            "ppsfch", timo);
1784                                }
1785                        } else {
1786                                err = tsleep(pps, PCATCH, "ppsfch", timo);
1787                        }
1788                        if (err == EWOULDBLOCK) {
1789                                if (fapi->timeout.tv_sec == -1) {
1790                                        continue;
1791                                } else {
1792                                        return (ETIMEDOUT);
1793                                }
1794                        } else if (err != 0) {
1795                                return (err);
1796                        }
1797                }
1798        }
1799
1800        pps->ppsinfo.current_mode = pps->ppsparam.mode;
1801        fapi->pps_info_buf = pps->ppsinfo;
1802
1803        return (0);
1804}
1805
1806int
1807pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1808{
1809        pps_params_t *app;
1810        struct pps_fetch_args *fapi;
1811#ifdef FFCLOCK
1812        struct pps_fetch_ffc_args *fapi_ffc;
1813#endif
1814#ifdef PPS_SYNC
1815        struct pps_kcbind_args *kapi;
1816#endif
1817
1818        KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1819        switch (cmd) {
1820        case PPS_IOC_CREATE:
1821                return (0);
1822        case PPS_IOC_DESTROY:
1823                return (0);
1824        case PPS_IOC_SETPARAMS:
1825                app = (pps_params_t *)data;
1826                if (app->mode & ~pps->ppscap)
1827                        return (EINVAL);
1828#ifdef FFCLOCK
1829                /* Ensure only a single clock is selected for ffc timestamp. */
1830                if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1831                        return (EINVAL);
1832#endif
1833                pps->ppsparam = *app;
1834                return (0);
1835        case PPS_IOC_GETPARAMS:
1836                app = (pps_params_t *)data;
1837                *app = pps->ppsparam;
1838                app->api_version = PPS_API_VERS_1;
1839                return (0);
1840        case PPS_IOC_GETCAP:
1841                *(int*)data = pps->ppscap;
1842                return (0);
1843        case PPS_IOC_FETCH:
1844                fapi = (struct pps_fetch_args *)data;
1845                return (pps_fetch(fapi, pps));
1846#ifdef FFCLOCK
1847        case PPS_IOC_FETCH_FFCOUNTER:
1848                fapi_ffc = (struct pps_fetch_ffc_args *)data;
1849                if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1850                    PPS_TSFMT_TSPEC)
1851                        return (EINVAL);
1852                if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1853                        return (EOPNOTSUPP);
1854                pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1855                fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1856                /* Overwrite timestamps if feedback clock selected. */
1857                switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1858                case PPS_TSCLK_FBCK:
1859                        fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1860                            pps->ppsinfo.assert_timestamp;
1861                        fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1862                            pps->ppsinfo.clear_timestamp;
1863                        break;
1864                case PPS_TSCLK_FFWD:
1865                        break;
1866                default:
1867                        break;
1868                }
1869                return (0);
1870#endif /* FFCLOCK */
1871        case PPS_IOC_KCBIND:
1872#ifdef PPS_SYNC
1873                kapi = (struct pps_kcbind_args *)data;
1874                /* XXX Only root should be able to do this */
1875                if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1876                        return (EINVAL);
1877                if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1878                        return (EINVAL);
1879                if (kapi->edge & ~pps->ppscap)
1880                        return (EINVAL);
1881                pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) |
1882                    (pps->kcmode & KCMODE_ABIFLAG);
1883                return (0);
1884#else
1885                return (EOPNOTSUPP);
1886#endif
1887        default:
1888                return (ENOIOCTL);
1889        }
1890}
1891
1892void
1893pps_init(struct pps_state *pps)
1894{
1895        pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1896        if (pps->ppscap & PPS_CAPTUREASSERT)
1897                pps->ppscap |= PPS_OFFSETASSERT;
1898        if (pps->ppscap & PPS_CAPTURECLEAR)
1899                pps->ppscap |= PPS_OFFSETCLEAR;
1900#ifdef FFCLOCK
1901        pps->ppscap |= PPS_TSCLK_MASK;
1902#endif
1903        pps->kcmode &= ~KCMODE_ABIFLAG;
1904}
1905
1906void
1907pps_init_abi(struct pps_state *pps)
1908{
1909
1910        pps_init(pps);
1911        if (pps->driver_abi > 0) {
1912                pps->kcmode |= KCMODE_ABIFLAG;
1913                pps->kernel_abi = PPS_ABI_VERSION;
1914        }
1915}
1916
1917void
1918pps_capture(struct pps_state *pps)
1919{
1920        struct timehands *th;
1921
1922        KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1923        th = timehands;
1924        pps->capgen = atomic_load_acq_int(&th->th_generation);
1925        pps->capth = th;
1926#ifdef FFCLOCK
1927        pps->capffth = fftimehands;
1928#endif
1929        pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1930        atomic_thread_fence_acq();
1931        if (pps->capgen != th->th_generation)
1932                pps->capgen = 0;
1933}
1934
1935void
1936pps_event(struct pps_state *pps, int event)
1937{
1938        struct bintime bt;
1939        struct timespec ts, *tsp, *osp;
1940        uint32_t tcount, *pcount;
1941        int foff;
1942        pps_seq_t *pseq;
1943#ifdef FFCLOCK
1944        struct timespec *tsp_ffc;
1945        pps_seq_t *pseq_ffc;
1946        ffcounter *ffcount;
1947#endif
1948#ifdef PPS_SYNC
1949        int fhard;
1950#endif
1951
1952        KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1953        /* Nothing to do if not currently set to capture this event type. */
1954        if ((event & pps->ppsparam.mode) == 0)
1955                return;
1956        /* If the timecounter was wound up underneath us, bail out. */
1957        if (pps->capgen == 0 || pps->capgen !=
1958            atomic_load_acq_int(&pps->capth->th_generation))
1959                return;
1960
1961        /* Things would be easier with arrays. */
1962        if (event == PPS_CAPTUREASSERT) {
1963                tsp = &pps->ppsinfo.assert_timestamp;
1964                osp = &pps->ppsparam.assert_offset;
1965                foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1966#ifdef PPS_SYNC
1967                fhard = pps->kcmode & PPS_CAPTUREASSERT;
1968#endif
1969                pcount = &pps->ppscount[0];
1970                pseq = &pps->ppsinfo.assert_sequence;
1971#ifdef FFCLOCK
1972                ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1973                tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1974                pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1975#endif
1976        } else {
1977                tsp = &pps->ppsinfo.clear_timestamp;
1978                osp = &pps->ppsparam.clear_offset;
1979                foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1980#ifdef PPS_SYNC
1981                fhard = pps->kcmode & PPS_CAPTURECLEAR;
1982#endif
1983                pcount = &pps->ppscount[1];
1984                pseq = &pps->ppsinfo.clear_sequence;
1985#ifdef FFCLOCK
1986                ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1987                tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1988                pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1989#endif
1990        }
1991
1992        /*
1993         * If the timecounter changed, we cannot compare the count values, so
1994         * we have to drop the rest of the PPS-stuff until the next event.
1995         */
1996        if (pps->ppstc != pps->capth->th_counter) {
1997                pps->ppstc = pps->capth->th_counter;
1998                *pcount = pps->capcount;
1999                pps->ppscount[2] = pps->capcount;
2000                return;
2001        }
2002
2003        /* Convert the count to a timespec. */
2004        tcount = pps->capcount - pps->capth->th_offset_count;
2005        tcount &= pps->capth->th_counter->tc_counter_mask;
2006        bt = pps->capth->th_bintime;
2007        bintime_addx(&bt, pps->capth->th_scale * tcount);
2008        bintime2timespec(&bt, &ts);
2009
2010        /* If the timecounter was wound up underneath us, bail out. */
2011        atomic_thread_fence_acq();
2012        if (pps->capgen != pps->capth->th_generation)
2013                return;
2014
2015        *pcount = pps->capcount;
2016        (*pseq)++;
2017        *tsp = ts;
2018
2019        if (foff) {
2020                timespecadd(tsp, osp);
2021                if (tsp->tv_nsec < 0) {
2022                        tsp->tv_nsec += 1000000000;
2023                        tsp->tv_sec -= 1;
2024                }
2025        }
2026
2027#ifdef FFCLOCK
2028        *ffcount = pps->capffth->tick_ffcount + tcount;
2029        bt = pps->capffth->tick_time;
2030        ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
2031        bintime_add(&bt, &pps->capffth->tick_time);
2032        bintime2timespec(&bt, &ts);
2033        (*pseq_ffc)++;
2034        *tsp_ffc = ts;
2035#endif
2036
2037#ifdef PPS_SYNC
2038        if (fhard) {
2039                uint64_t scale;
2040
2041                /*
2042                 * Feed the NTP PLL/FLL.
2043                 * The FLL wants to know how many (hardware) nanoseconds
2044                 * elapsed since the previous event.
2045                 */
2046                tcount = pps->capcount - pps->ppscount[2];
2047                pps->ppscount[2] = pps->capcount;
2048                tcount &= pps->capth->th_counter->tc_counter_mask;
2049                scale = (uint64_t)1 << 63;
2050                scale /= pps->capth->th_counter->tc_frequency;
2051                scale *= 2;
2052                bt.sec = 0;
2053                bt.frac = 0;
2054                bintime_addx(&bt, scale * tcount);
2055                bintime2timespec(&bt, &ts);
2056                hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
2057        }
2058#endif
2059
2060        /* Wakeup anyone sleeping in pps_fetch().  */
2061        wakeup(pps);
2062}
2063#else /* __rtems__ */
2064/* FIXME: https://devel.rtems.org/ticket/2349 */
2065#endif /* __rtems__ */
2066
2067/*
2068 * Timecounters need to be updated every so often to prevent the hardware
2069 * counter from overflowing.  Updating also recalculates the cached values
2070 * used by the get*() family of functions, so their precision depends on
2071 * the update frequency.
2072 */
2073
2074#ifndef __rtems__
2075static int tc_tick;
2076SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
2077    "Approximate number of hardclock ticks in a millisecond");
2078#endif /* __rtems__ */
2079
2080#ifndef __rtems__
2081void
2082tc_ticktock(int cnt)
2083{
2084        static int count;
2085
2086        if (mtx_trylock_spin(&tc_setclock_mtx)) {
2087                count += cnt;
2088                if (count >= tc_tick) {
2089                        count = 0;
2090                        tc_windup(NULL);
2091                }
2092                mtx_unlock_spin(&tc_setclock_mtx);
2093        }
2094}
2095#else /* __rtems__ */
2096void
2097_Timecounter_Tick(void)
2098{
2099        Per_CPU_Control *cpu_self = _Per_CPU_Get();
2100
2101        if (_Per_CPU_Is_boot_processor(cpu_self)) {
2102                tc_windup(NULL);
2103        }
2104
2105        _Watchdog_Tick(cpu_self);
2106}
2107
2108void
2109_Timecounter_Tick_simple(uint32_t delta, uint32_t offset,
2110    ISR_lock_Context *lock_context)
2111{
2112        struct bintime bt;
2113        struct timehands *th;
2114        uint32_t ogen;
2115
2116        th = timehands;
2117        ogen = th->th_generation;
2118        th->th_offset_count = offset;
2119        bintime_addx(&th->th_offset, th->th_scale * delta);
2120
2121        bt = th->th_offset;
2122        bintime_add(&bt, &th->th_boottime);
2123        /* Update the UTC timestamps used by the get*() functions. */
2124        th->th_bintime = bt;
2125        bintime2timeval(&bt, &th->th_microtime);
2126        bintime2timespec(&bt, &th->th_nanotime);
2127
2128        /*
2129         * Now that the struct timehands is again consistent, set the new
2130         * generation number, making sure to not make it zero.
2131         */
2132        if (++ogen == 0)
2133                ogen = 1;
2134        th->th_generation = ogen;
2135
2136        /* Go live with the new struct timehands. */
2137        time_second = th->th_microtime.tv_sec;
2138        time_uptime = th->th_offset.sec;
2139
2140        _Timecounter_Release(lock_context);
2141
2142        _Watchdog_Tick(_Per_CPU_Get_snapshot());
2143}
2144#endif /* __rtems__ */
2145
2146#ifndef __rtems__
2147static void __inline
2148tc_adjprecision(void)
2149{
2150        int t;
2151
2152        if (tc_timepercentage > 0) {
2153                t = (99 + tc_timepercentage) / tc_timepercentage;
2154                tc_precexp = fls(t + (t >> 1)) - 1;
2155                FREQ2BT(hz / tc_tick, &bt_timethreshold);
2156                FREQ2BT(hz, &bt_tickthreshold);
2157                bintime_shift(&bt_timethreshold, tc_precexp);
2158                bintime_shift(&bt_tickthreshold, tc_precexp);
2159        } else {
2160                tc_precexp = 31;
2161                bt_timethreshold.sec = INT_MAX;
2162                bt_timethreshold.frac = ~(uint64_t)0;
2163                bt_tickthreshold = bt_timethreshold;
2164        }
2165        sbt_timethreshold = bttosbt(bt_timethreshold);
2166        sbt_tickthreshold = bttosbt(bt_tickthreshold);
2167}
2168#endif /* __rtems__ */
2169
2170#ifndef __rtems__
2171static int
2172sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
2173{
2174        int error, val;
2175
2176        val = tc_timepercentage;
2177        error = sysctl_handle_int(oidp, &val, 0, req);
2178        if (error != 0 || req->newptr == NULL)
2179                return (error);
2180        tc_timepercentage = val;
2181        if (cold)
2182                goto done;
2183        tc_adjprecision();
2184done:
2185        return (0);
2186}
2187
2188static void
2189inittimecounter(void *dummy)
2190{
2191        u_int p;
2192        int tick_rate;
2193
2194        /*
2195         * Set the initial timeout to
2196         * max(1, <approx. number of hardclock ticks in a millisecond>).
2197         * People should probably not use the sysctl to set the timeout
2198         * to smaller than its initial value, since that value is the
2199         * smallest reasonable one.  If they want better timestamps they
2200         * should use the non-"get"* functions.
2201         */
2202        if (hz > 1000)
2203                tc_tick = (hz + 500) / 1000;
2204        else
2205                tc_tick = 1;
2206        tc_adjprecision();
2207        FREQ2BT(hz, &tick_bt);
2208        tick_sbt = bttosbt(tick_bt);
2209        tick_rate = hz / tc_tick;
2210        FREQ2BT(tick_rate, &tc_tick_bt);
2211        tc_tick_sbt = bttosbt(tc_tick_bt);
2212        p = (tc_tick * 1000000) / hz;
2213        printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
2214
2215#ifdef FFCLOCK
2216        ffclock_init();
2217#endif
2218        /* warm up new timecounter (again) and get rolling. */
2219        (void)timecounter->tc_get_timecount(timecounter);
2220        (void)timecounter->tc_get_timecount(timecounter);
2221        mtx_lock_spin(&tc_setclock_mtx);
2222        tc_windup(NULL);
2223        mtx_unlock_spin(&tc_setclock_mtx);
2224}
2225
2226SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
2227
2228/* Cpu tick handling -------------------------------------------------*/
2229
2230static int cpu_tick_variable;
2231static uint64_t cpu_tick_frequency;
2232
2233static DPCPU_DEFINE(uint64_t, tc_cpu_ticks_base);
2234static DPCPU_DEFINE(unsigned, tc_cpu_ticks_last);
2235
2236static uint64_t
2237tc_cpu_ticks(void)
2238{
2239        struct timecounter *tc;
2240        uint64_t res, *base;
2241        unsigned u, *last;
2242
2243        critical_enter();
2244        base = DPCPU_PTR(tc_cpu_ticks_base);
2245        last = DPCPU_PTR(tc_cpu_ticks_last);
2246        tc = timehands->th_counter;
2247        u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
2248        if (u < *last)
2249                *base += (uint64_t)tc->tc_counter_mask + 1;
2250        *last = u;
2251        res = u + *base;
2252        critical_exit();
2253        return (res);
2254}
2255
2256void
2257cpu_tick_calibration(void)
2258{
2259        static time_t last_calib;
2260
2261        if (time_uptime != last_calib && !(time_uptime & 0xf)) {
2262                cpu_tick_calibrate(0);
2263                last_calib = time_uptime;
2264        }
2265}
2266
2267/*
2268 * This function gets called every 16 seconds on only one designated
2269 * CPU in the system from hardclock() via cpu_tick_calibration()().
2270 *
2271 * Whenever the real time clock is stepped we get called with reset=1
2272 * to make sure we handle suspend/resume and similar events correctly.
2273 */
2274
2275static void
2276cpu_tick_calibrate(int reset)
2277{
2278        static uint64_t c_last;
2279        uint64_t c_this, c_delta;
2280        static struct bintime  t_last;
2281        struct bintime t_this, t_delta;
2282        uint32_t divi;
2283
2284        if (reset) {
2285                /* The clock was stepped, abort & reset */
2286                t_last.sec = 0;
2287                return;
2288        }
2289
2290        /* we don't calibrate fixed rate cputicks */
2291        if (!cpu_tick_variable)
2292                return;
2293
2294        getbinuptime(&t_this);
2295        c_this = cpu_ticks();
2296        if (t_last.sec != 0) {
2297                c_delta = c_this - c_last;
2298                t_delta = t_this;
2299                bintime_sub(&t_delta, &t_last);
2300                /*
2301                 * Headroom:
2302                 *      2^(64-20) / 16[s] =
2303                 *      2^(44) / 16[s] =
2304                 *      17.592.186.044.416 / 16 =
2305                 *      1.099.511.627.776 [Hz]
2306                 */
2307                divi = t_delta.sec << 20;
2308                divi |= t_delta.frac >> (64 - 20);
2309                c_delta <<= 20;
2310                c_delta /= divi;
2311                if (c_delta > cpu_tick_frequency) {
2312                        if (0 && bootverbose)
2313                                printf("cpu_tick increased to %ju Hz\n",
2314                                    c_delta);
2315                        cpu_tick_frequency = c_delta;
2316                }
2317        }
2318        c_last = c_this;
2319        t_last = t_this;
2320}
2321
2322void
2323set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
2324{
2325
2326        if (func == NULL) {
2327                cpu_ticks = tc_cpu_ticks;
2328        } else {
2329                cpu_tick_frequency = freq;
2330                cpu_tick_variable = var;
2331                cpu_ticks = func;
2332        }
2333}
2334
2335uint64_t
2336cpu_tickrate(void)
2337{
2338
2339        if (cpu_ticks == tc_cpu_ticks)
2340                return (tc_getfrequency());
2341        return (cpu_tick_frequency);
2342}
2343
2344/*
2345 * We need to be slightly careful converting cputicks to microseconds.
2346 * There is plenty of margin in 64 bits of microseconds (half a million
2347 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
2348 * before divide conversion (to retain precision) we find that the
2349 * margin shrinks to 1.5 hours (one millionth of 146y).
2350 * With a three prong approach we never lose significant bits, no
2351 * matter what the cputick rate and length of timeinterval is.
2352 */
2353
2354uint64_t
2355cputick2usec(uint64_t tick)
2356{
2357
2358        if (tick > 18446744073709551LL)         /* floor(2^64 / 1000) */
2359                return (tick / (cpu_tickrate() / 1000000LL));
2360        else if (tick > 18446744073709LL)       /* floor(2^64 / 1000000) */
2361                return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
2362        else
2363                return ((tick * 1000000LL) / cpu_tickrate());
2364}
2365
2366cpu_tick_f      *cpu_ticks = tc_cpu_ticks;
2367#endif /* __rtems__ */
2368
2369#ifndef __rtems__
2370static int vdso_th_enable = 1;
2371static int
2372sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
2373{
2374        int old_vdso_th_enable, error;
2375
2376        old_vdso_th_enable = vdso_th_enable;
2377        error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
2378        if (error != 0)
2379                return (error);
2380        vdso_th_enable = old_vdso_th_enable;
2381        return (0);
2382}
2383SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
2384    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2385    NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
2386
2387uint32_t
2388tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
2389{
2390        struct timehands *th;
2391        uint32_t enabled;
2392
2393        th = timehands;
2394        vdso_th->th_scale = th->th_scale;
2395        vdso_th->th_offset_count = th->th_offset_count;
2396        vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
2397        vdso_th->th_offset = th->th_offset;
2398        vdso_th->th_boottime = th->th_boottime;
2399        if (th->th_counter->tc_fill_vdso_timehands != NULL) {
2400                enabled = th->th_counter->tc_fill_vdso_timehands(vdso_th,
2401                    th->th_counter);
2402        } else
2403                enabled = 0;
2404        if (!vdso_th_enable)
2405                enabled = 0;
2406        return (enabled);
2407}
2408#endif /* __rtems__ */
2409
2410#ifdef COMPAT_FREEBSD32
2411uint32_t
2412tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
2413{
2414        struct timehands *th;
2415        uint32_t enabled;
2416
2417        th = timehands;
2418        *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
2419        vdso_th32->th_offset_count = th->th_offset_count;
2420        vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
2421        vdso_th32->th_offset.sec = th->th_offset.sec;
2422        *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
2423        vdso_th32->th_boottime.sec = th->th_boottime.sec;
2424        *(uint64_t *)&vdso_th32->th_boottime.frac[0] = th->th_boottime.frac;
2425        if (th->th_counter->tc_fill_vdso_timehands32 != NULL) {
2426                enabled = th->th_counter->tc_fill_vdso_timehands32(vdso_th32,
2427                    th->th_counter);
2428        } else
2429                enabled = 0;
2430        if (!vdso_th_enable)
2431                enabled = 0;
2432        return (enabled);
2433}
2434#endif
Note: See TracBrowser for help on using the repository browser.