source: rtems/cpukit/score/src/kern_tc.c @ 51304dde

5
Last change on this file since 51304dde was 51304dde, checked in by Ian Lepore <ian@…>, on 05/04/15 at 17:59:39

timecounter: Merge FreeBSD change r282424

Implement a mechanism for making changes in the kernel<->driver PPS interface without breaking ABI or API compatibility with existing drivers.

The existing data structures used to communicate between the kernel and
driver portions of PPS processing contain no spare/padding fields and no
flags field or other straightforward mechanism for communicating changes
in the structures or behaviors of the code. This makes it difficult to
MFC new features added to the PPS facility. ABI compatibility is
important; out-of-tree drivers in module form are known to exist. (Note
that the existing api_version field in the pps_params structure must
contain the value mandated by RFC 2783 and any RFCs that come along after.)

These changes introduce a pair of abi-version fields which are filled in
by the driver and the kernel respectively to indicate the interface
version. The driver sets its version field before calling the new
pps_init_abi() function. That lets the kernel know how much of the
pps_state structure is understood by the driver and it can avoid using
newer fields at the end of the structure that it knows about if the driver
is a lower version. The kernel fills in its version field during the init
call, letting the driver know what features and data the kernel supports.

To implement the new version information in a way that is backwards
compatible with code from before these changes, the high bit of the
lightly-used 'kcmode' field is repurposed as a flag bit that indicates the
driver is aware of the abi versioning scheme. Basically if this bit is
clear that indicates a "version 0" driver and if it is set the driver_abi
field indicates the version.

These changes also move the recently-added 'mtx' field of pps_state from
the middle to the end of the structure, and make the kernel code that uses
this field conditional on the driver being abi version 1 or higher. It
changes the only driver currently supplying the mtx field, usb_serial, to
use pps_init_abi().

Reviewed by: hselasky@

Update #3175.

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