24#include "opt_ffclock.h"
27#include <sys/kernel.h>
28#include <sys/limits.h>
33#include <sys/sleepqueue.h>
34#include <sys/sysctl.h>
35#include <sys/syslog.h>
37#include <sys/timeffc.h>
38#include <sys/timepps.h>
39#include <sys/timetc.h>
89 .th_scale = (uint64_t)-1 / 1000000,
116 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
118 "Estimated system boottime");
123 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
127SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RWTUN,
132 CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
145 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
147 "Allowed time interval deviation in percents");
163 struct timeval boottime;
172 if (req->flags & SCTL_MASK32) {
173 tv[0] = boottime.tv_sec;
174 tv[1] = boottime.tv_usec;
175 return (SYSCTL_OUT(req, tv,
sizeof(tv)));
179 return (SYSCTL_OUT(req, &boottime,
sizeof(boottime)));
188 ncount = tc->tc_get_timecount(tc);
198 freq = tc->tc_frequency;
213 tc->tc_counter_mask);
218 uint64_t large_delta, uint64_t delta)
222 if (__predict_false(delta >= large_delta)) {
224 x = (scale >> 32) * delta;
226 bintime_addx(
bt, x << 32);
227 bintime_addx(
bt, (scale & 0xffffffff) * delta);
229 bintime_addx(
bt, scale * delta);
245 u_int delta, gen, large_delta;
250 btp = (
struct bintime *)((vm_offset_t)th + off);
255 atomic_thread_fence_acq();
260#define GETTHBINTIME(dst, member) \
262 _Static_assert(_Generic(((struct timehands *)NULL)->member, \
263 struct bintime: 1, default: 0) == 1, \
264 "struct timehands member is not of struct bintime type"); \
265 bintime_off(dst, __offsetof(struct timehands, member)); \
277 memcpy(out, (
char *)th + off, out_size);
278 atomic_thread_fence_acq();
281#define GETTHMEMBER(dst, member) \
283 _Static_assert(_Generic(*dst, \
284 __typeof(((struct timehands *)NULL)->member): 1, \
286 "*dst and struct timehands member have different types"); \
287 getthmember(dst, sizeof(*dst), __offsetof(struct timehands, \
300fbclock_nanouptime(
struct timespec *tsp)
304 fbclock_binuptime(&
bt);
305 bintime2timespec(&
bt, tsp);
309fbclock_microuptime(
struct timeval *tvp)
313 fbclock_binuptime(&
bt);
314 bintime2timeval(&
bt, tvp);
325fbclock_nanotime(
struct timespec *tsp)
329 fbclock_bintime(&
bt);
330 bintime2timespec(&
bt, tsp);
334fbclock_microtime(
struct timeval *tvp)
338 fbclock_bintime(&
bt);
339 bintime2timeval(&
bt, tvp);
350fbclock_getnanouptime(
struct timespec *tsp)
355 bintime2timespec(&
bt, tsp);
359fbclock_getmicrouptime(
struct timeval *tvp)
364 bintime2timeval(&
bt, tvp);
375fbclock_getnanotime(
struct timespec *tsp)
382fbclock_getmicrotime(
struct timeval *tvp)
402 bintime2timespec(&
bt, tsp);
411 bintime2timeval(&
bt, tvp);
427 bintime2timespec(&
bt, tsp);
436 bintime2timeval(&
bt, tvp);
452 bintime2timespec(&
bt, tsp);
461 bintime2timeval(&
bt, tvp);
492 bintime2timeval(&boottimebin, boottime);
511struct ffclock_estimate ffclock_estimate;
512struct bintime ffclock_boottime;
513uint32_t ffclock_status;
514int8_t ffclock_updated;
515struct mtx ffclock_mtx;
518 struct ffclock_estimate cest;
521 ffcounter tick_ffcount;
522 uint64_t period_lerp;
523 volatile uint8_t gen;
524 struct fftimehands *next;
527#define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
529static struct fftimehands ffth[10];
530static struct fftimehands *
volatile fftimehands = ffth;
535 struct fftimehands *cur;
536 struct fftimehands *last;
538 memset(ffth, 0,
sizeof(ffth));
540 last = ffth + NUM_ELEMENTS(ffth) - 1;
541 for (cur = ffth; cur < last; cur++)
546 ffclock_status = FFCLOCK_STA_UNSYNC;
547 mtx_init(&ffclock_mtx,
"ffclock lock", NULL, MTX_DEF);
558ffclock_reset_clock(
struct timespec *
ts)
561 struct ffclock_estimate cest;
564 memset(&cest, 0,
sizeof(
struct ffclock_estimate));
566 timespec2bintime(
ts, &ffclock_boottime);
567 timespec2bintime(
ts, &(cest.update_time));
568 ffclock_read_counter(&cest.update_ffcount);
569 cest.leapsec_next = 0;
570 cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
573 cest.status = FFCLOCK_STA_UNSYNC;
574 cest.leapsec_total = 0;
577 mtx_lock(&ffclock_mtx);
578 bcopy(&cest, &ffclock_estimate,
sizeof(
struct ffclock_estimate));
579 ffclock_updated = INT8_MAX;
580 mtx_unlock(&ffclock_mtx);
582 printf(
"ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
583 (
unsigned long long)tc->tc_frequency, (
long)
ts->tv_sec,
584 (
unsigned long)
ts->tv_nsec);
595ffclock_convert_delta(ffcounter ffdelta, uint64_t period,
struct bintime *
bt)
598 ffcounter delta, delta_max;
600 delta_max = (1ULL << (8 *
sizeof(
unsigned int))) - 1;
603 if (ffdelta > delta_max)
609 bintime_mul(&bt2, (
unsigned int)delta);
610 bintime_add(
bt, &bt2);
612 }
while (ffdelta > 0);
624ffclock_windup(
unsigned int delta)
626 struct ffclock_estimate *cest;
627 struct fftimehands *ffth;
631 unsigned int polling;
632 uint8_t forward_jump, ogen;
639 ffth = fftimehands->next;
643 bcopy(&fftimehands->cest, cest,
sizeof(
struct ffclock_estimate));
644 ffdelta = (ffcounter)delta;
645 ffth->period_lerp = fftimehands->period_lerp;
647 ffth->tick_time = fftimehands->tick_time;
648 ffclock_convert_delta(ffdelta, cest->period, &
bt);
649 bintime_add(&ffth->tick_time, &
bt);
651 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
652 ffclock_convert_delta(ffdelta, ffth->period_lerp, &
bt);
653 bintime_add(&ffth->tick_time_lerp, &
bt);
655 ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
662 if (ffclock_updated == 0) {
663 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
664 ffclock_convert_delta(ffdelta, cest->period, &
bt);
665 if (
bt.sec > 2 * FFCLOCK_SKM_SCALE)
666 ffclock_status |= FFCLOCK_STA_UNSYNC;
678 if (ffclock_updated > 0) {
679 bcopy(&ffclock_estimate, cest,
sizeof(
struct ffclock_estimate));
680 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
681 ffth->tick_time = cest->update_time;
682 ffclock_convert_delta(ffdelta, cest->period, &
bt);
683 bintime_add(&ffth->tick_time, &
bt);
686 if (ffclock_updated == INT8_MAX)
687 ffth->tick_time_lerp = ffth->tick_time;
689 if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
694 bintime_clear(&gap_lerp);
696 gap_lerp = ffth->tick_time;
697 bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
699 gap_lerp = ffth->tick_time_lerp;
700 bintime_sub(&gap_lerp, &ffth->tick_time);
713 if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
714 ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
715 ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
717 bintime_add(&ffclock_boottime, &gap_lerp);
719 bintime_sub(&ffclock_boottime, &gap_lerp);
720 ffth->tick_time_lerp = ffth->tick_time;
721 bintime_clear(&gap_lerp);
724 ffclock_status = cest->status;
725 ffth->period_lerp = cest->period;
732 if (bintime_isset(&gap_lerp)) {
733 ffdelta = cest->update_ffcount;
734 ffdelta -= fftimehands->cest.update_ffcount;
735 ffclock_convert_delta(ffdelta, cest->period, &
bt);
738 bt.frac = 5000000 * (uint64_t)18446744073LL;
739 bintime_mul(&
bt, polling);
740 if (bintime_cmp(&gap_lerp, &
bt, >))
745 if (gap_lerp.sec > 0) {
747 frac /= ffdelta / gap_lerp.sec;
749 frac += gap_lerp.frac / ffdelta;
752 ffth->period_lerp += frac;
754 ffth->period_lerp -= frac;
778 struct fftimehands *ffth;
779 struct ffclock_estimate *cest;
784 ffth = fftimehands->next;
789 bcopy(&(fftimehands->cest), cest,
sizeof(
struct ffclock_estimate));
790 cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
793 cest->status |= FFCLOCK_STA_UNSYNC;
795 ffth->tick_ffcount = fftimehands->tick_ffcount;
796 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
797 ffth->tick_time = fftimehands->tick_time;
798 ffth->period_lerp = cest->period;
813ffclock_last_tick(ffcounter *ffcount,
struct bintime *
bt, uint32_t
flags)
815 struct fftimehands *ffth;
825 if ((
flags & FFCLOCK_LERP) == FFCLOCK_LERP)
826 *
bt = ffth->tick_time_lerp;
828 *
bt = ffth->tick_time;
829 *ffcount = ffth->tick_ffcount;
830 }
while (gen == 0 || gen != ffth->gen);
841ffclock_convert_abs(ffcounter ffcount,
struct bintime *
bt, uint32_t
flags)
843 struct fftimehands *ffth;
855 if (ffcount > ffth->tick_ffcount)
856 ffdelta = ffcount - ffth->tick_ffcount;
858 ffdelta = ffth->tick_ffcount - ffcount;
860 if ((
flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
861 *
bt = ffth->tick_time_lerp;
862 ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
864 *
bt = ffth->tick_time;
865 ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
868 if (ffcount > ffth->tick_ffcount)
869 bintime_add(
bt, &bt2);
871 bintime_sub(
bt, &bt2);
872 }
while (gen == 0 || gen != ffth->gen);
882ffclock_convert_diff(ffcounter ffdelta,
struct bintime *
bt)
884 struct fftimehands *ffth;
891 ffclock_convert_delta(ffdelta, ffth->cest.period,
bt);
892 }
while (gen == 0 || gen != ffth->gen);
899ffclock_read_counter(ffcounter *ffcount)
902 struct fftimehands *ffth;
903 unsigned int gen, delta;
914 *ffcount = ffth->tick_ffcount;
915 atomic_thread_fence_acq();
1030 bintime2timespec(&
bt, tsp);
1050 struct fbclock_info *fbi;
1053 unsigned int delta, gen;
1056 struct fftimehands *ffth;
1057 struct ffclock_info *ffi;
1058 struct ffclock_estimate cest;
1060 ffi = &clock_snap->ff_info;
1063 fbi = &clock_snap->fb_info;
1073 ffi->tick_time = ffth->tick_time_lerp;
1074 ffi->tick_time_lerp = ffth->tick_time_lerp;
1075 ffi->period = ffth->cest.period;
1076 ffi->period_lerp = ffth->period_lerp;
1077 clock_snap->ffcount = ffth->tick_ffcount;
1082 atomic_thread_fence_acq();
1085 clock_snap->delta = delta;
1093 (uint64_t)18446744073709ULL;
1094 clock_snap->fb_info.error =
bt;
1098 clock_snap->ffcount += delta;
1101 ffi->leapsec_adjustment = cest.leapsec_total;
1102 if (clock_snap->ffcount > cest.leapsec_next)
1103 ffi->leapsec_adjustment -= cest.leapsec;
1106 clock_snap->ff_info.status = cest.status;
1107 ffcount = clock_snap->ffcount - cest.update_ffcount;
1108 ffclock_convert_delta(ffcount, cest.period, &
bt);
1110 bintime_mul(&
bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1112 bintime_addx(&
bt, cest.errb_abs * (uint64_t)18446744073ULL);
1113 clock_snap->ff_info.error =
bt;
1123 int whichclock, uint32_t
flags)
1131 switch (whichclock) {
1133 *
bt = cs->fb_info.tick_time;
1137 bintime_addx(
bt, cs->fb_info.th_scale * cs->delta);
1139 if ((
flags & FBCLOCK_UPTIME) == 0) {
1141 bintime_add(
bt, &boottimebin);
1146 if (
flags & FFCLOCK_LERP) {
1147 *
bt = cs->ff_info.tick_time_lerp;
1148 period = cs->ff_info.period_lerp;
1150 *
bt = cs->ff_info.tick_time;
1151 period = cs->ff_info.period;
1155 if (cs->delta > 0) {
1156 ffclock_convert_delta(cs->delta, period, &bt2);
1157 bintime_add(
bt, &bt2);
1161 if (
flags & FFCLOCK_LEAPSEC)
1162 bt->sec -= cs->ff_info.leapsec_adjustment;
1165 if (
flags & FFCLOCK_UPTIME)
1166 bintime_sub(
bt, &ffclock_boottime);
1184 struct sysctl_oid *tc_root;
1186 u = tc->tc_frequency / tc->tc_counter_mask;
1190 if (u >
hz && tc->tc_quality >= 0) {
1191 tc->tc_quality = -2000;
1193 printf(
"Timecounter \"%s\" frequency %ju Hz",
1194 tc->tc_name, (uintmax_t)tc->tc_frequency);
1195 printf(
" -- Insufficient hz, needs at least %u\n", u);
1198 printf(
"Timecounter \"%s\" frequency %ju Hz quality %d\n",
1199 tc->tc_name, (uintmax_t)tc->tc_frequency,
1206 tc_root = SYSCTL_ADD_NODE_WITH_LABEL(NULL,
1207 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1208 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1209 "timecounter description",
"timecounter");
1210 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1211 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1212 "mask for implemented bits");
1213 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1214 "counter", CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, tc,
1216 "current timecounter value");
1217 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1218 "frequency", CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, tc,
1220 "timecounter frequency");
1221 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1222 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1223 "goodness of time counter");
1237 if (tc->tc_quality < 0)
1250 (void)tc->tc_get_timecount(tc);
1294 struct timespec tbef, taft;
1297 timespec2bintime(
ts, &
bt);
1302 bintime_sub(&
bt, &bt2);
1314 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1315 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1316 (intmax_t)taft.tv_sec, taft.tv_nsec,
1317 (intmax_t)
ts->tv_sec,
ts->tv_nsec);
1348 scale = (uint64_t)1 << 63;
1366 u_int delta, ncount, ogen;
1383 atomic_thread_fence_rel();
1385 if (new_boottimebin != NULL)
1396 ncount = tc->tc_get_timecount(tc);
1400 ffclock_windup(delta);
1453 if ((tc->tc_flags & TC_FLAGS_C2STOP) != 0)
1455 if ((th->
th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
1461 (((uint64_t)tc->tc_counter_mask + 1) / 3));
1464 ffclock_change_tc(th);
1487 time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1506 strlcpy(newname, tc->tc_name,
sizeof(newname));
1510 if (error != 0 || req->newptr == NULL)
1516 if (strcmp(newname, tc->tc_name) == 0) {
1520 for (newtc =
timecounters; newtc != NULL; newtc = newtc->tc_next) {
1521 if (strcmp(newname, newtc->tc_name) != 0)
1525 (void)newtc->tc_get_timecount(newtc);
1540 return (newtc != NULL ? 0 : EINVAL);
1543 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, 0, 0,
1545 "Timecounter hardware selected");
1560 for (tc =
timecounters; tc != NULL; tc = tc->tc_next) {
1563 sbuf_printf(&sb,
"%s(%d)", tc->tc_name, tc->tc_quality);
1572 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
1574 "Timecounter hardware detected");
1588 return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers);
1592pps_fetch(
struct pps_fetch_args *fapi,
struct pps_state *pps)
1595 pps_seq_t aseq, cseq;
1598 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1608 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1609 if (fapi->timeout.tv_sec == -1)
1612 tv.tv_sec = fapi->timeout.tv_sec;
1613 tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1616 aseq = atomic_load_int(&pps->ppsinfo.assert_sequence);
1617 cseq = atomic_load_int(&pps->ppsinfo.clear_sequence);
1618 while (aseq == atomic_load_int(&pps->ppsinfo.assert_sequence) &&
1619 cseq == atomic_load_int(&pps->ppsinfo.clear_sequence)) {
1620 if (
abi_aware(pps, 1) && pps->driver_mtx != NULL) {
1621 if (pps->flags & PPSFLAG_MTX_SPIN) {
1622 err = msleep_spin(pps, pps->driver_mtx,
1625 err = msleep(pps, pps->driver_mtx, PCATCH,
1629 err = tsleep(pps, PCATCH,
"ppsfch", timo);
1631 if (err == EWOULDBLOCK) {
1632 if (fapi->timeout.tv_sec == -1) {
1637 }
else if (err != 0) {
1643 pps->ppsinfo.current_mode = pps->ppsparam.mode;
1644 fapi->pps_info_buf = pps->ppsinfo;
1653 struct pps_fetch_args *fapi;
1655 struct pps_fetch_ffc_args *fapi_ffc;
1658 struct pps_kcbind_args *kapi;
1661 KASSERT(pps != NULL, (
"NULL pps pointer in pps_ioctl"));
1663 case PPS_IOC_CREATE:
1665 case PPS_IOC_DESTROY:
1667 case PPS_IOC_SETPARAMS:
1668 app = (pps_params_t *)
data;
1669 if (app->mode & ~pps->ppscap)
1673 if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1676 pps->ppsparam = *app;
1678 case PPS_IOC_GETPARAMS:
1679 app = (pps_params_t *)
data;
1680 *app = pps->ppsparam;
1681 app->api_version = PPS_API_VERS_1;
1683 case PPS_IOC_GETCAP:
1684 *(
int*)
data = pps->ppscap;
1687 fapi = (
struct pps_fetch_args *)
data;
1690 case PPS_IOC_FETCH_FFCOUNTER:
1691 fapi_ffc = (
struct pps_fetch_ffc_args *)
data;
1692 if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1695 if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1696 return (EOPNOTSUPP);
1697 pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1698 fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1700 switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1701 case PPS_TSCLK_FBCK:
1702 fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1703 pps->ppsinfo.assert_timestamp;
1704 fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1705 pps->ppsinfo.clear_timestamp;
1707 case PPS_TSCLK_FFWD:
1714 case PPS_IOC_KCBIND:
1716 kapi = (
struct pps_kcbind_args *)
data;
1718 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1720 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1722 if (kapi->edge & ~pps->ppscap)
1724 pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) |
1725 (pps->kcmode & KCMODE_ABIFLAG);
1728 return (EOPNOTSUPP);
1738 pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1739 if (pps->ppscap & PPS_CAPTUREASSERT)
1740 pps->ppscap |= PPS_OFFSETASSERT;
1741 if (pps->ppscap & PPS_CAPTURECLEAR)
1742 pps->ppscap |= PPS_OFFSETCLEAR;
1744 pps->ppscap |= PPS_TSCLK_MASK;
1746 pps->kcmode &= ~KCMODE_ABIFLAG;
1754 if (pps->driver_abi > 0) {
1755 pps->kcmode |= KCMODE_ABIFLAG;
1756 pps->kernel_abi = PPS_ABI_VERSION;
1765 KASSERT(pps != NULL, (
"NULL pps pointer in pps_capture"));
1770 pps->capffth = fftimehands;
1773 atomic_thread_fence_acq();
1782 struct timespec
ts, *tsp, *osp;
1783 u_int tcount, *pcount;
1787 struct timespec *tsp_ffc;
1788 pps_seq_t *pseq_ffc;
1795 KASSERT(pps != NULL, (
"NULL pps pointer in pps_event"));
1797 if ((event & pps->ppsparam.mode) == 0)
1800 if (pps->capgen == 0 || pps->capgen !=
1801 atomic_load_acq_int(&pps->capth->th_generation))
1805 if (event == PPS_CAPTUREASSERT) {
1806 tsp = &pps->ppsinfo.assert_timestamp;
1807 osp = &pps->ppsparam.assert_offset;
1808 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1810 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1812 pcount = &pps->ppscount[0];
1813 pseq = &pps->ppsinfo.assert_sequence;
1815 ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1816 tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1817 pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1820 tsp = &pps->ppsinfo.clear_timestamp;
1821 osp = &pps->ppsparam.clear_offset;
1822 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1824 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1826 pcount = &pps->ppscount[1];
1827 pseq = &pps->ppsinfo.clear_sequence;
1829 ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1830 tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1831 pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1839 if (pps->ppstc != pps->capth->th_counter) {
1840 pps->ppstc = pps->capth->th_counter;
1841 *pcount = pps->capcount;
1842 pps->ppscount[2] = pps->capcount;
1847 tcount = pps->capcount - pps->capth->th_offset_count;
1848 tcount &= pps->capth->th_counter->tc_counter_mask;
1849 bt = pps->capth->th_bintime;
1850 bintime_addx(&
bt, pps->capth->th_scale * tcount);
1851 bintime2timespec(&
bt, &
ts);
1854 atomic_thread_fence_acq();
1855 if (pps->capgen != pps->capth->th_generation)
1858 *pcount = pps->capcount;
1863 timespecadd(tsp, osp, tsp);
1864 if (tsp->tv_nsec < 0) {
1865 tsp->tv_nsec += 1000000000;
1871 *ffcount = pps->capffth->tick_ffcount + tcount;
1872 bt = pps->capffth->tick_time;
1873 ffclock_convert_delta(tcount, pps->capffth->cest.period, &
bt);
1874 bintime_add(&
bt, &pps->capffth->tick_time);
1875 bintime2timespec(&
bt, &
ts);
1889 tcount = pps->capcount - pps->ppscount[2];
1890 pps->ppscount[2] = pps->capcount;
1891 tcount &= pps->capth->th_counter->tc_counter_mask;
1892 scale = (uint64_t)1 << 63;
1893 scale /= pps->capth->th_counter->tc_frequency;
1897 bintime_addx(&
bt, scale * tcount);
1898 bintime2timespec(&
bt, &
ts);
1899 hardpps(tsp,
ts.tv_nsec + 1000000000 *
ts.tv_sec);
1916 "Approximate number of hardclock ticks in a millisecond");
1962 if (error != 0 || req->newptr == NULL)
1979 TUNABLE_INT_FETCH(
"kern.timecounter.timehands_count",
1992 mtx_init(&
tc_lock,
"tc", NULL, MTX_DEF);
2021 printf(
"Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
2048 uint64_t
res, *base;
2052 base = DPCPU_PTR(tc_cpu_ticks_base);
2053 last = DPCPU_PTR(tc_cpu_ticks_last);
2055 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
2057 *base += (uint64_t)tc->tc_counter_mask + 1;
2067 static time_t last_calib;
2086 static uint64_t c_last;
2087 uint64_t c_this, c_delta;
2089 struct bintime t_this, t_delta;
2104 if (t_last.sec != 0) {
2105 c_delta = c_this - c_last;
2107 bintime_sub(&t_delta, &t_last);
2115 divi = t_delta.sec << 20;
2116 divi |= t_delta.frac >> (64 - 20);
2121 printf(
"cpu_tick increased to %ju Hz\n",
2166 if (
tick > 18446744073709551LL)
2168 else if (
tick > 18446744073709LL)
2180 int old_vdso_th_enable, error;
2190 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2202 vdso_th->th_counter_mask = th->
th_counter->tc_counter_mask;
2205 if (th->
th_counter->tc_fill_vdso_timehands != NULL) {
2206 enabled = th->
th_counter->tc_fill_vdso_timehands(vdso_th,
2215#ifdef COMPAT_FREEBSD32
2217tc_fill_vdso_timehands32(
struct vdso_timehands32 *vdso_th32)
2225 vdso_th32->th_counter_mask = th->
th_counter->tc_counter_mask;
2226 vdso_th32->th_offset.sec = th->
th_offset.sec;
2227 *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->
th_offset.frac;
2229 *(uint64_t *)&vdso_th32->th_boottime.frac[0] = th->
th_boottime.frac;
2230 if (th->
th_counter->tc_fill_vdso_timehands32 != NULL) {
2231 enabled = th->
th_counter->tc_fill_vdso_timehands32(vdso_th32,
2253 val1 = tc->tc_get_timecount(tc);
2254 __compiler_membar();
2255 val2 = tc->tc_get_timecount(tc);
2257 db_printf(
"timecounter %p %s\n", tc, tc->tc_name);
2258 db_printf(
" mask %#x freq %ju qual %d flags %#x priv %p\n",
2259 tc->tc_counter_mask, (uintmax_t)tc->tc_frequency, tc->tc_quality,
2260 tc->tc_flags, tc->tc_priv);
2261 db_printf(
" val %#x %#x\n", val1, val2);
2262 db_printf(
"timehands adj %#jx scale %#jx ldelta %d off_cnt %d gen %d\n",
2265 db_printf(
" offset %jd %jd boottime %jd %jd\n",
static struct bt_table bt
int tvtohz(struct timeval *tv)
void ntp_update_second(int64_t *adjustment, time_t *newsec)
void timekeep_push_vdso(void)
void wakeup(const void *ident)
int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
int sysctl_handle_64(SYSCTL_HANDLER_ARGS)
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
int sysctl_handle_string(SYSCTL_HANDLER_ARGS)
struct sbuf * sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, struct sysctl_req *req)
void tc_ticktock(int cnt)
static struct timecounter dummy_timecounter
static __inline void bintime_off(struct bintime *bt, u_int off)
static int cpu_tick_variable
static void inittimehands(void *dummy)
void pps_event(struct pps_state *pps, int event)
static int pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
static void tc_windup(struct bintime *new_boottimebin)
void set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
uint32_t tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
volatile int rtc_generation
int sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt, int whichclock, uint32_t flags)
static uint64_t cpu_tick_frequency
void getbinuptime(struct bintime *bt)
MTX_SYSINIT(tc_setclock_init, &tc_setclock_mtx, "tcsetc", MTX_SPIN)
DPCPU_DEFINE_STATIC(uint64_t, tc_cpu_ticks_base)
static __inline void bintime_add_tc_delta(struct bintime *bt, uint64_t scale, uint64_t large_delta, uint64_t delta)
SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "")
static int sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
static void __inline tc_adjprecision(void)
struct timecounter * timecounter
void dtrace_getnanotime(struct timespec *tsp)
void nanouptime(struct timespec *tsp)
static int timestepwarnings
void getboottimebin(struct bintime *boottimebin)
SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, 0, sysctl_kern_boottime, "S,timeval", "Estimated system boottime")
static __inline void getthmember(void *out, size_t out_size, u_int off)
#define GETTHMEMBER(dst, member)
static void cpu_tick_calibrate(int)
uint64_t tc_getfrequency(void)
static int sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RWTUN, ×tepwarnings, 0, "Log time steps")
static struct mtx tc_setclock_mtx
void sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
void dtrace_getnanouptime(struct timespec *tsp)
SYSINIT(timehands, SI_SUB_TUNABLES, SI_ORDER_ANY, inittimehands, NULL)
volatile time_t time_uptime
struct bintime bt_tickthreshold
sbintime_t sbt_timethreshold
void getnanouptime(struct timespec *tsp)
static struct mtx tc_lock
void getboottime(struct timeval *boottime)
int pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
struct bintime tc_tick_bt
#define GETTHBINTIME(dst, member)
void tc_setclock(struct timespec *ts)
static struct timecounter * timecounters
static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
static bool sleeping_on_old_rtc(struct thread *td)
void binuptime(struct bintime *bt)
static char tc_from_tunable[16]
static struct timehands *volatile timehands
static struct timehands ths[16]
void cpu_tick_calibration(void)
void tc_init(struct timecounter *tc)
static __inline u_int tc_delta(struct timehands *th)
sbintime_t sbt_tickthreshold
volatile time_t time_second
static u_int dummy_get_timecount(struct timecounter *tc)
void pps_capture(struct pps_state *pps)
void bintime(struct bintime *bt)
static int abi_aware(struct pps_state *pps, int vers)
static int sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
void getmicrouptime(struct timeval *tvp)
void getbintime(struct bintime *bt)
static int sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
void getnanotime(struct timespec *tsp)
void microtime(struct timeval *tvp)
void microuptime(struct timeval *tvp)
static uint64_t tc_cpu_ticks(void)
static int sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
static int vdso_th_enable
static int timehands_count
struct bintime bt_timethreshold
uint64_t cpu_tickrate(void)
void pps_init(struct pps_state *pps)
void nanotime(struct timespec *tsp)
static void recalculate_scaling_factor_and_large_delta(struct timehands *th)
uint64_t cputick2usec(uint64_t tick)
void getmicrotime(struct timeval *tvp)
static void inittimecounter(void *dummy)
void pps_init_abi(struct pps_state *pps)
struct timespec th_nanotime
struct bintime th_bintime
struct timecounter * th_counter
struct timeval th_microtime
struct timehands * th_next
struct bintime th_boottime
int printf(const char *fmt,...)
void log(int level, const char *fmt,...)
int sbuf_finish(struct sbuf *s)
int sbuf_putc(struct sbuf *s, int c)
void sbuf_delete(struct sbuf *s)
int sbuf_printf(struct sbuf *s, const char *fmt,...)
void sleepq_chains_remove_matching(bool(*matches)(struct thread *))