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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
13 #include "opt_compat.h"
16 #include <sys/param.h>
17 #include <sys/kernel.h>
18 #include <sys/sysctl.h>
19 #include <sys/syslog.h>
20 #include <sys/systm.h>
21 #include <sys/timepps.h>
22 #include <sys/timetc.h>
23 #include <sys/timex.h>
27 * A large step happens on boot. This constant detects such steps.
28 * It is relatively small so that ntp_update_second gets called enough
29 * in the typical 'missed a couple of seconds' case, but doesn't loop
30 * forever when the time step is large.
32 #define LARGE_STEP 200
35 * Implement a dummy timecounter which we can use until we get a real one
36 * in the air. This allows the console and other early stuff to use
41 dummy_get_timecount(struct timecounter *tc)
48 static struct timecounter dummy_timecounter = {
49 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
53 /* These fields must be initialized by the driver. */
54 struct timecounter *th_counter;
55 int64_t th_adjustment;
57 u_int th_offset_count;
58 struct bintime th_offset;
59 struct timeval th_microtime;
60 struct timespec th_nanotime;
61 /* Fields not to be copied in tc_windup start with th_generation. */
62 volatile u_int th_generation;
63 struct timehands *th_next;
66 static struct timehands th0;
67 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
68 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
69 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
70 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
71 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
72 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
73 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
74 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
75 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
76 static struct timehands th0 = {
79 (uint64_t)-1 / 1000000,
88 static struct timehands *volatile timehands = &th0;
89 struct timecounter *timecounter = &dummy_timecounter;
90 static struct timecounter *timecounters = &dummy_timecounter;
92 int tc_min_ticktock_freq = 1;
94 time_t time_second = 1;
95 time_t time_uptime = 1;
97 struct bintime boottimebin;
98 struct timeval boottime;
99 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
100 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
101 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
103 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
104 SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
106 static int timestepwarnings;
107 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
108 ×tepwarnings, 0, "Log time steps");
110 static void tc_windup(void);
111 static void cpu_tick_calibrate(int);
114 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
119 if (req->flags & SCTL_MASK32) {
120 tv[0] = boottime.tv_sec;
121 tv[1] = boottime.tv_usec;
122 return SYSCTL_OUT(req, tv, sizeof(tv));
125 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
129 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
132 struct timecounter *tc = arg1;
134 ncount = tc->tc_get_timecount(tc);
135 return sysctl_handle_int(oidp, &ncount, 0, req);
139 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
142 struct timecounter *tc = arg1;
144 freq = tc->tc_frequency;
145 return sysctl_handle_64(oidp, &freq, 0, req);
149 * Return the difference between the timehands' counter value now and what
150 * was when we copied it to the timehands' offset_count.
152 static __inline u_int
153 tc_delta(struct timehands *th)
155 struct timecounter *tc;
158 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
159 tc->tc_counter_mask);
163 * Functions for reading the time. We have to loop until we are sure that
164 * the timehands that we operated on was not updated under our feet. See
165 * the comment in <sys/time.h> for a description of these 12 functions.
169 binuptime(struct bintime *bt)
171 struct timehands *th;
176 gen = th->th_generation;
178 bintime_addx(bt, th->th_scale * tc_delta(th));
179 } while (gen == 0 || gen != th->th_generation);
183 nanouptime(struct timespec *tsp)
188 bintime2timespec(&bt, tsp);
192 microuptime(struct timeval *tvp)
197 bintime2timeval(&bt, tvp);
201 bintime(struct bintime *bt)
205 bintime_add(bt, &boottimebin);
209 nanotime(struct timespec *tsp)
214 bintime2timespec(&bt, tsp);
218 microtime(struct timeval *tvp)
223 bintime2timeval(&bt, tvp);
227 getbinuptime(struct bintime *bt)
229 struct timehands *th;
234 gen = th->th_generation;
236 } while (gen == 0 || gen != th->th_generation);
240 getnanouptime(struct timespec *tsp)
242 struct timehands *th;
247 gen = th->th_generation;
248 bintime2timespec(&th->th_offset, tsp);
249 } while (gen == 0 || gen != th->th_generation);
253 getmicrouptime(struct timeval *tvp)
255 struct timehands *th;
260 gen = th->th_generation;
261 bintime2timeval(&th->th_offset, tvp);
262 } while (gen == 0 || gen != th->th_generation);
266 getbintime(struct bintime *bt)
268 struct timehands *th;
273 gen = th->th_generation;
275 } while (gen == 0 || gen != th->th_generation);
276 bintime_add(bt, &boottimebin);
280 getnanotime(struct timespec *tsp)
282 struct timehands *th;
287 gen = th->th_generation;
288 *tsp = th->th_nanotime;
289 } while (gen == 0 || gen != th->th_generation);
293 getmicrotime(struct timeval *tvp)
295 struct timehands *th;
300 gen = th->th_generation;
301 *tvp = th->th_microtime;
302 } while (gen == 0 || gen != th->th_generation);
306 * Initialize a new timecounter and possibly use it.
309 tc_init(struct timecounter *tc)
312 struct sysctl_oid *tc_root;
314 u = tc->tc_frequency / tc->tc_counter_mask;
315 /* XXX: We need some margin here, 10% is a guess */
318 if (u > hz && tc->tc_quality >= 0) {
319 tc->tc_quality = -2000;
321 printf("Timecounter \"%s\" frequency %ju Hz",
322 tc->tc_name, (uintmax_t)tc->tc_frequency);
323 printf(" -- Insufficient hz, needs at least %u\n", u);
325 } else if (tc->tc_quality >= 0 || bootverbose) {
326 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
327 tc->tc_name, (uintmax_t)tc->tc_frequency,
331 tc->tc_next = timecounters;
334 * Set up sysctl tree for this counter.
336 tc_root = SYSCTL_ADD_NODE(NULL,
337 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
338 CTLFLAG_RW, 0, "timecounter description");
339 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
340 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
341 "mask for implemented bits");
342 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
343 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
344 sysctl_kern_timecounter_get, "IU", "current timecounter value");
345 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
346 "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
347 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
348 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
349 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
350 "goodness of time counter");
352 * Never automatically use a timecounter with negative quality.
353 * Even though we run on the dummy counter, switching here may be
354 * worse since this timecounter may not be monotonous.
356 if (tc->tc_quality < 0)
358 if (tc->tc_quality < timecounter->tc_quality)
360 if (tc->tc_quality == timecounter->tc_quality &&
361 tc->tc_frequency < timecounter->tc_frequency)
363 (void)tc->tc_get_timecount(tc);
364 (void)tc->tc_get_timecount(tc);
368 /* Report the frequency of the current timecounter. */
370 tc_getfrequency(void)
373 return (timehands->th_counter->tc_frequency);
377 * Step our concept of UTC. This is done by modifying our estimate of
382 tc_setclock(struct timespec *ts)
384 struct timespec tbef, taft;
385 struct bintime bt, bt2;
387 cpu_tick_calibrate(1);
389 timespec2bintime(ts, &bt);
391 bintime_sub(&bt, &bt2);
392 bintime_add(&bt2, &boottimebin);
394 bintime2timeval(&bt, &boottime);
396 /* XXX fiddle all the little crinkly bits around the fiords... */
399 if (timestepwarnings) {
401 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
402 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
403 (intmax_t)taft.tv_sec, taft.tv_nsec,
404 (intmax_t)ts->tv_sec, ts->tv_nsec);
406 cpu_tick_calibrate(1);
410 * Initialize the next struct timehands in the ring and make
411 * it the active timehands. Along the way we might switch to a different
412 * timecounter and/or do seconds processing in NTP. Slightly magic.
418 struct timehands *th, *tho;
420 u_int delta, ncount, ogen;
425 * Make the next timehands a copy of the current one, but do not
426 * overwrite the generation or next pointer. While we update
427 * the contents, the generation must be zero.
431 ogen = th->th_generation;
432 th->th_generation = 0;
433 bcopy(tho, th, offsetof(struct timehands, th_generation));
436 * Capture a timecounter delta on the current timecounter and if
437 * changing timecounters, a counter value from the new timecounter.
438 * Update the offset fields accordingly.
440 delta = tc_delta(th);
441 if (th->th_counter != timecounter)
442 ncount = timecounter->tc_get_timecount(timecounter);
445 th->th_offset_count += delta;
446 th->th_offset_count &= th->th_counter->tc_counter_mask;
447 while (delta > th->th_counter->tc_frequency) {
448 /* Eat complete unadjusted seconds. */
449 delta -= th->th_counter->tc_frequency;
452 if ((delta > th->th_counter->tc_frequency / 2) &&
453 (th->th_scale * delta < ((uint64_t)1 << 63))) {
454 /* The product th_scale * delta just barely overflows. */
457 bintime_addx(&th->th_offset, th->th_scale * delta);
460 * Hardware latching timecounters may not generate interrupts on
461 * PPS events, so instead we poll them. There is a finite risk that
462 * the hardware might capture a count which is later than the one we
463 * got above, and therefore possibly in the next NTP second which might
464 * have a different rate than the current NTP second. It doesn't
465 * matter in practice.
467 if (tho->th_counter->tc_poll_pps)
468 tho->th_counter->tc_poll_pps(tho->th_counter);
471 * Deal with NTP second processing. The for loop normally
472 * iterates at most once, but in extreme situations it might
473 * keep NTP sane if timeouts are not run for several seconds.
474 * At boot, the time step can be large when the TOD hardware
475 * has been read, so on really large steps, we call
476 * ntp_update_second only twice. We need to call it twice in
477 * case we missed a leap second.
480 bintime_add(&bt, &boottimebin);
481 i = bt.sec - tho->th_microtime.tv_sec;
486 ntp_update_second(&th->th_adjustment, &bt.sec);
488 boottimebin.sec += bt.sec - t;
490 /* Update the UTC timestamps used by the get*() functions. */
491 /* XXX shouldn't do this here. Should force non-`get' versions. */
492 bintime2timeval(&bt, &th->th_microtime);
493 bintime2timespec(&bt, &th->th_nanotime);
495 /* Now is a good time to change timecounters. */
496 if (th->th_counter != timecounter) {
498 if ((timecounter->tc_flags & TC_FLAGS_C3STOP) != 0)
499 cpu_disable_deep_sleep++;
500 if ((th->th_counter->tc_flags & TC_FLAGS_C3STOP) != 0)
501 cpu_disable_deep_sleep--;
503 th->th_counter = timecounter;
504 th->th_offset_count = ncount;
505 tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
506 (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
510 * Recalculate the scaling factor. We want the number of 1/2^64
511 * fractions of a second per period of the hardware counter, taking
512 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
513 * processing provides us with.
515 * The th_adjustment is nanoseconds per second with 32 bit binary
516 * fraction and we want 64 bit binary fraction of second:
518 * x = a * 2^32 / 10^9 = a * 4.294967296
520 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
521 * we can only multiply by about 850 without overflowing, that
522 * leaves no suitably precise fractions for multiply before divide.
524 * Divide before multiply with a fraction of 2199/512 results in a
525 * systematic undercompensation of 10PPM of th_adjustment. On a
526 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
528 * We happily sacrifice the lowest of the 64 bits of our result
529 * to the goddess of code clarity.
532 scale = (uint64_t)1 << 63;
533 scale += (th->th_adjustment / 1024) * 2199;
534 scale /= th->th_counter->tc_frequency;
535 th->th_scale = scale * 2;
538 * Now that the struct timehands is again consistent, set the new
539 * generation number, making sure to not make it zero.
543 th->th_generation = ogen;
545 /* Go live with the new struct timehands. */
546 time_second = th->th_microtime.tv_sec;
547 time_uptime = th->th_offset.sec;
549 timekeep_push_vdso();
552 /* Report or change the active timecounter hardware. */
554 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
557 struct timecounter *newtc, *tc;
561 strlcpy(newname, tc->tc_name, sizeof(newname));
563 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
564 if (error != 0 || req->newptr == NULL ||
565 strcmp(newname, tc->tc_name) == 0)
567 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
568 if (strcmp(newname, newtc->tc_name) != 0)
571 /* Warm up new timecounter. */
572 (void)newtc->tc_get_timecount(newtc);
573 (void)newtc->tc_get_timecount(newtc);
576 timekeep_push_vdso();
582 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
583 0, 0, sysctl_kern_timecounter_hardware, "A",
584 "Timecounter hardware selected");
587 /* Report or change the active timecounter hardware. */
589 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
592 struct timecounter *tc;
597 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
598 sprintf(buf, "%s%s(%d)",
599 spc, tc->tc_name, tc->tc_quality);
600 error = SYSCTL_OUT(req, buf, strlen(buf));
606 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
607 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
610 * RFC 2783 PPS-API implementation.
614 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
617 struct pps_fetch_args *fapi;
619 struct pps_kcbind_args *kapi;
622 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
626 case PPS_IOC_DESTROY:
628 case PPS_IOC_SETPARAMS:
629 app = (pps_params_t *)data;
630 if (app->mode & ~pps->ppscap)
632 pps->ppsparam = *app;
634 case PPS_IOC_GETPARAMS:
635 app = (pps_params_t *)data;
636 *app = pps->ppsparam;
637 app->api_version = PPS_API_VERS_1;
640 *(int*)data = pps->ppscap;
643 fapi = (struct pps_fetch_args *)data;
644 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
646 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
648 pps->ppsinfo.current_mode = pps->ppsparam.mode;
649 fapi->pps_info_buf = pps->ppsinfo;
653 kapi = (struct pps_kcbind_args *)data;
654 /* XXX Only root should be able to do this */
655 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
657 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
659 if (kapi->edge & ~pps->ppscap)
661 pps->kcmode = kapi->edge;
672 pps_init(struct pps_state *pps)
674 pps->ppscap |= PPS_TSFMT_TSPEC;
675 if (pps->ppscap & PPS_CAPTUREASSERT)
676 pps->ppscap |= PPS_OFFSETASSERT;
677 if (pps->ppscap & PPS_CAPTURECLEAR)
678 pps->ppscap |= PPS_OFFSETCLEAR;
682 pps_capture(struct pps_state *pps)
684 struct timehands *th;
686 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
688 pps->capgen = th->th_generation;
690 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
691 if (pps->capgen != th->th_generation)
696 pps_event(struct pps_state *pps, int event)
699 struct timespec ts, *tsp, *osp;
700 u_int tcount, *pcount;
704 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
705 /* If the timecounter was wound up underneath us, bail out. */
706 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
709 /* Things would be easier with arrays. */
710 if (event == PPS_CAPTUREASSERT) {
711 tsp = &pps->ppsinfo.assert_timestamp;
712 osp = &pps->ppsparam.assert_offset;
713 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
714 fhard = pps->kcmode & PPS_CAPTUREASSERT;
715 pcount = &pps->ppscount[0];
716 pseq = &pps->ppsinfo.assert_sequence;
718 tsp = &pps->ppsinfo.clear_timestamp;
719 osp = &pps->ppsparam.clear_offset;
720 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
721 fhard = pps->kcmode & PPS_CAPTURECLEAR;
722 pcount = &pps->ppscount[1];
723 pseq = &pps->ppsinfo.clear_sequence;
727 * If the timecounter changed, we cannot compare the count values, so
728 * we have to drop the rest of the PPS-stuff until the next event.
730 if (pps->ppstc != pps->capth->th_counter) {
731 pps->ppstc = pps->capth->th_counter;
732 *pcount = pps->capcount;
733 pps->ppscount[2] = pps->capcount;
737 /* Convert the count to a timespec. */
738 tcount = pps->capcount - pps->capth->th_offset_count;
739 tcount &= pps->capth->th_counter->tc_counter_mask;
740 bt = pps->capth->th_offset;
741 bintime_addx(&bt, pps->capth->th_scale * tcount);
742 bintime_add(&bt, &boottimebin);
743 bintime2timespec(&bt, &ts);
745 /* If the timecounter was wound up underneath us, bail out. */
746 if (pps->capgen != pps->capth->th_generation)
749 *pcount = pps->capcount;
754 timespecadd(tsp, osp);
755 if (tsp->tv_nsec < 0) {
756 tsp->tv_nsec += 1000000000;
765 * Feed the NTP PLL/FLL.
766 * The FLL wants to know how many (hardware) nanoseconds
767 * elapsed since the previous event.
769 tcount = pps->capcount - pps->ppscount[2];
770 pps->ppscount[2] = pps->capcount;
771 tcount &= pps->capth->th_counter->tc_counter_mask;
772 scale = (uint64_t)1 << 63;
773 scale /= pps->capth->th_counter->tc_frequency;
777 bintime_addx(&bt, scale * tcount);
778 bintime2timespec(&bt, &ts);
779 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
785 * Timecounters need to be updated every so often to prevent the hardware
786 * counter from overflowing. Updating also recalculates the cached values
787 * used by the get*() family of functions, so their precision depends on
788 * the update frequency.
792 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
793 "Approximate number of hardclock ticks in a millisecond");
808 inittimecounter(void *dummy)
813 * Set the initial timeout to
814 * max(1, <approx. number of hardclock ticks in a millisecond>).
815 * People should probably not use the sysctl to set the timeout
816 * to smaller than its inital value, since that value is the
817 * smallest reasonable one. If they want better timestamps they
818 * should use the non-"get"* functions.
821 tc_tick = (hz + 500) / 1000;
824 p = (tc_tick * 1000000) / hz;
825 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
827 /* warm up new timecounter (again) and get rolling. */
828 (void)timecounter->tc_get_timecount(timecounter);
829 (void)timecounter->tc_get_timecount(timecounter);
833 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
835 /* Cpu tick handling -------------------------------------------------*/
837 static int cpu_tick_variable;
838 static uint64_t cpu_tick_frequency;
843 static uint64_t base;
844 static unsigned last;
846 struct timecounter *tc;
848 tc = timehands->th_counter;
849 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
851 base += (uint64_t)tc->tc_counter_mask + 1;
857 cpu_tick_calibration(void)
859 static time_t last_calib;
861 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
862 cpu_tick_calibrate(0);
863 last_calib = time_uptime;
868 * This function gets called every 16 seconds on only one designated
869 * CPU in the system from hardclock() via cpu_tick_calibration()().
871 * Whenever the real time clock is stepped we get called with reset=1
872 * to make sure we handle suspend/resume and similar events correctly.
876 cpu_tick_calibrate(int reset)
878 static uint64_t c_last;
879 uint64_t c_this, c_delta;
880 static struct bintime t_last;
881 struct bintime t_this, t_delta;
885 /* The clock was stepped, abort & reset */
890 /* we don't calibrate fixed rate cputicks */
891 if (!cpu_tick_variable)
894 getbinuptime(&t_this);
895 c_this = cpu_ticks();
896 if (t_last.sec != 0) {
897 c_delta = c_this - c_last;
899 bintime_sub(&t_delta, &t_last);
902 * 2^(64-20) / 16[s] =
904 * 17.592.186.044.416 / 16 =
905 * 1.099.511.627.776 [Hz]
907 divi = t_delta.sec << 20;
908 divi |= t_delta.frac >> (64 - 20);
911 if (c_delta > cpu_tick_frequency) {
912 if (0 && bootverbose)
913 printf("cpu_tick increased to %ju Hz\n",
915 cpu_tick_frequency = c_delta;
923 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
927 cpu_ticks = tc_cpu_ticks;
929 cpu_tick_frequency = freq;
930 cpu_tick_variable = var;
939 if (cpu_ticks == tc_cpu_ticks)
940 return (tc_getfrequency());
941 return (cpu_tick_frequency);
945 * We need to be slightly careful converting cputicks to microseconds.
946 * There is plenty of margin in 64 bits of microseconds (half a million
947 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
948 * before divide conversion (to retain precision) we find that the
949 * margin shrinks to 1.5 hours (one millionth of 146y).
950 * With a three prong approach we never lose significant bits, no
951 * matter what the cputick rate and length of timeinterval is.
955 cputick2usec(uint64_t tick)
958 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
959 return (tick / (cpu_tickrate() / 1000000LL));
960 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
961 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
963 return ((tick * 1000000LL) / cpu_tickrate());
966 cpu_tick_f *cpu_ticks = tc_cpu_ticks;
968 static int vdso_th_enable = 1;
970 sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
972 int old_vdso_th_enable, error;
974 old_vdso_th_enable = vdso_th_enable;
975 error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
978 vdso_th_enable = old_vdso_th_enable;
979 timekeep_push_vdso();
982 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
983 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
984 NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
987 tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
989 struct timehands *th;
993 vdso_th->th_algo = VDSO_TH_ALGO_1;
994 vdso_th->th_scale = th->th_scale;
995 vdso_th->th_offset_count = th->th_offset_count;
996 vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
997 vdso_th->th_offset = th->th_offset;
998 vdso_th->th_boottime = boottimebin;
999 enabled = cpu_fill_vdso_timehands(vdso_th);
1000 if (!vdso_th_enable)
1005 #ifdef COMPAT_FREEBSD32
1007 tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
1009 struct timehands *th;
1013 vdso_th32->th_algo = VDSO_TH_ALGO_1;
1014 *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
1015 vdso_th32->th_offset_count = th->th_offset_count;
1016 vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
1017 vdso_th32->th_offset.sec = th->th_offset.sec;
1018 *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
1019 vdso_th32->th_boottime.sec = boottimebin.sec;
1020 *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
1021 enabled = cpu_fill_vdso_timehands32(vdso_th32);
1022 if (!vdso_th_enable)