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

4.115
Last change on this file since cc693845 was cc693845, checked in by Alexander Krutwig <alexander.krutwig@…>, on 05/12/15 at 10:12:18

timecounter: Honor FFCLOCK define

Update #2271.

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