The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_tc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 
   10 #include <sys/cdefs.h>
   11 __FBSDID("$FreeBSD: releng/5.2/sys/kern/kern_tc.c 122610 2003-11-13 10:03:58Z phk $");
   12 
   13 #include "opt_ntp.h"
   14 
   15 #include <sys/param.h>
   16 #include <sys/kernel.h>
   17 #include <sys/sysctl.h>
   18 #include <sys/systm.h>
   19 #include <sys/timepps.h>
   20 #include <sys/timetc.h>
   21 #include <sys/timex.h>
   22 
   23 /*
   24  * A large step happens on boot.  This constant detects such steps.
   25  * It is relatively small so that ntp_update_second gets called enough
   26  * in the typical 'missed a couple of seconds' case, but doesn't loop
   27  * forever when the time step is large.
   28  */
   29 #define LARGE_STEP      200
   30 
   31 /*
   32  * Implement a dummy timecounter which we can use until we get a real one
   33  * in the air.  This allows the console and other early stuff to use
   34  * time services.
   35  */
   36 
   37 static u_int
   38 dummy_get_timecount(struct timecounter *tc)
   39 {
   40         static u_int now;
   41 
   42         return (++now);
   43 }
   44 
   45 static struct timecounter dummy_timecounter = {
   46         dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
   47 };
   48 
   49 struct timehands {
   50         /* These fields must be initialized by the driver. */
   51         struct timecounter      *th_counter;
   52         int64_t                 th_adjustment;
   53         u_int64_t               th_scale;
   54         u_int                   th_offset_count;
   55         struct bintime          th_offset;
   56         struct timeval          th_microtime;
   57         struct timespec         th_nanotime;
   58         /* Fields not to be copied in tc_windup start with th_generation. */
   59         volatile u_int          th_generation;
   60         struct timehands        *th_next;
   61 };
   62 
   63 extern struct timehands th0;
   64 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
   65 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
   66 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
   67 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
   68 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
   69 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
   70 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
   71 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
   72 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
   73 static struct timehands th0 = {
   74         &dummy_timecounter,
   75         0,
   76         (uint64_t)-1 / 1000000,
   77         0,
   78         {1, 0},
   79         {0, 0},
   80         {0, 0},
   81         1,
   82         &th1
   83 };
   84 
   85 static struct timehands *volatile timehands = &th0;
   86 struct timecounter *timecounter = &dummy_timecounter;
   87 static struct timecounter *timecounters = &dummy_timecounter;
   88 
   89 time_t time_second = 1;
   90 time_t time_uptime = 0;
   91 
   92 static struct bintime boottimebin;
   93 struct timeval boottime;
   94 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
   95     &boottime, timeval, "System boottime");
   96 
   97 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
   98 
   99 #define TC_STATS(foo) \
  100         static u_int foo; \
  101         SYSCTL_UINT(_kern_timecounter, OID_AUTO, foo, CTLFLAG_RD, &foo, 0, "");\
  102         struct __hack
  103 
  104 TC_STATS(nbinuptime);    TC_STATS(nnanouptime);    TC_STATS(nmicrouptime);
  105 TC_STATS(nbintime);      TC_STATS(nnanotime);      TC_STATS(nmicrotime);
  106 TC_STATS(ngetbinuptime); TC_STATS(ngetnanouptime); TC_STATS(ngetmicrouptime);
  107 TC_STATS(ngetbintime);   TC_STATS(ngetnanotime);   TC_STATS(ngetmicrotime);
  108 TC_STATS(nsetclock);
  109 
  110 #undef TC_STATS
  111 
  112 static void tc_windup(void);
  113 
  114 /*
  115  * Return the difference between the timehands' counter value now and what
  116  * was when we copied it to the timehands' offset_count.
  117  */
  118 static __inline u_int
  119 tc_delta(struct timehands *th)
  120 {
  121         struct timecounter *tc;
  122 
  123         tc = th->th_counter;
  124         return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
  125             tc->tc_counter_mask);
  126 }
  127 
  128 /*
  129  * Functions for reading the time.  We have to loop until we are sure that
  130  * the timehands that we operated on was not updated under our feet.  See
  131  * the comment in <sys/time.h> for a description of these 12 functions.
  132  */
  133 
  134 void
  135 binuptime(struct bintime *bt)
  136 {
  137         struct timehands *th;
  138         u_int gen;
  139 
  140         nbinuptime++;
  141         do {
  142                 th = timehands;
  143                 gen = th->th_generation;
  144                 *bt = th->th_offset;
  145                 bintime_addx(bt, th->th_scale * tc_delta(th));
  146         } while (gen == 0 || gen != th->th_generation);
  147 }
  148 
  149 void
  150 nanouptime(struct timespec *tsp)
  151 {
  152         struct bintime bt;
  153 
  154         nnanouptime++;
  155         binuptime(&bt);
  156         bintime2timespec(&bt, tsp);
  157 }
  158 
  159 void
  160 microuptime(struct timeval *tvp)
  161 {
  162         struct bintime bt;
  163 
  164         nmicrouptime++;
  165         binuptime(&bt);
  166         bintime2timeval(&bt, tvp);
  167 }
  168 
  169 void
  170 bintime(struct bintime *bt)
  171 {
  172 
  173         nbintime++;
  174         binuptime(bt);
  175         bintime_add(bt, &boottimebin);
  176 }
  177 
  178 void
  179 nanotime(struct timespec *tsp)
  180 {
  181         struct bintime bt;
  182 
  183         nnanotime++;
  184         bintime(&bt);
  185         bintime2timespec(&bt, tsp);
  186 }
  187 
  188 void
  189 microtime(struct timeval *tvp)
  190 {
  191         struct bintime bt;
  192 
  193         nmicrotime++;
  194         bintime(&bt);
  195         bintime2timeval(&bt, tvp);
  196 }
  197 
  198 void
  199 getbinuptime(struct bintime *bt)
  200 {
  201         struct timehands *th;
  202         u_int gen;
  203 
  204         ngetbinuptime++;
  205         do {
  206                 th = timehands;
  207                 gen = th->th_generation;
  208                 *bt = th->th_offset;
  209         } while (gen == 0 || gen != th->th_generation);
  210 }
  211 
  212 void
  213 getnanouptime(struct timespec *tsp)
  214 {
  215         struct timehands *th;
  216         u_int gen;
  217 
  218         ngetnanouptime++;
  219         do {
  220                 th = timehands;
  221                 gen = th->th_generation;
  222                 bintime2timespec(&th->th_offset, tsp);
  223         } while (gen == 0 || gen != th->th_generation);
  224 }
  225 
  226 void
  227 getmicrouptime(struct timeval *tvp)
  228 {
  229         struct timehands *th;
  230         u_int gen;
  231 
  232         ngetmicrouptime++;
  233         do {
  234                 th = timehands;
  235                 gen = th->th_generation;
  236                 bintime2timeval(&th->th_offset, tvp);
  237         } while (gen == 0 || gen != th->th_generation);
  238 }
  239 
  240 void
  241 getbintime(struct bintime *bt)
  242 {
  243         struct timehands *th;
  244         u_int gen;
  245 
  246         ngetbintime++;
  247         do {
  248                 th = timehands;
  249                 gen = th->th_generation;
  250                 *bt = th->th_offset;
  251         } while (gen == 0 || gen != th->th_generation);
  252         bintime_add(bt, &boottimebin);
  253 }
  254 
  255 void
  256 getnanotime(struct timespec *tsp)
  257 {
  258         struct timehands *th;
  259         u_int gen;
  260 
  261         ngetnanotime++;
  262         do {
  263                 th = timehands;
  264                 gen = th->th_generation;
  265                 *tsp = th->th_nanotime;
  266         } while (gen == 0 || gen != th->th_generation);
  267 }
  268 
  269 void
  270 getmicrotime(struct timeval *tvp)
  271 {
  272         struct timehands *th;
  273         u_int gen;
  274 
  275         ngetmicrotime++;
  276         do {
  277                 th = timehands;
  278                 gen = th->th_generation;
  279                 *tvp = th->th_microtime;
  280         } while (gen == 0 || gen != th->th_generation);
  281 }
  282 
  283 /*
  284  * Initialize a new timecounter and possibly use it.
  285  */
  286 void
  287 tc_init(struct timecounter *tc)
  288 {
  289         u_int u;
  290 
  291         u = tc->tc_frequency / tc->tc_counter_mask;
  292         /* XXX: We need some margin here, 10% is a guess */
  293         u *= 11;
  294         u /= 10;
  295         if (u > hz && tc->tc_quality >= 0) {
  296                 tc->tc_quality = -2000;
  297                 if (bootverbose) {
  298                         printf("Timecounter \"%s\" frequency %ju Hz",
  299                             tc->tc_name, (uintmax_t)tc->tc_frequency);
  300                         printf(" -- Insufficient hz, needs at least %u\n", u);
  301                 }
  302         } else if (tc->tc_quality >= 0 || bootverbose) {
  303                 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
  304                     tc->tc_name, (uintmax_t)tc->tc_frequency,
  305                     tc->tc_quality);
  306         }
  307 
  308         tc->tc_next = timecounters;
  309         timecounters = tc;
  310         /*
  311          * Never automatically use a timecounter with negative quality.
  312          * Even though we run on the dummy counter, switching here may be
  313          * worse since this timecounter may not be monotonous.
  314          */
  315         if (tc->tc_quality < 0)
  316                 return;
  317         if (tc->tc_quality < timecounter->tc_quality)
  318                 return;
  319         if (tc->tc_quality == timecounter->tc_quality &&
  320             tc->tc_frequency < timecounter->tc_frequency)
  321                 return;
  322         (void)tc->tc_get_timecount(tc);
  323         (void)tc->tc_get_timecount(tc);
  324         timecounter = tc;
  325 }
  326 
  327 /* Report the frequency of the current timecounter. */
  328 u_int64_t
  329 tc_getfrequency(void)
  330 {
  331 
  332         return (timehands->th_counter->tc_frequency);
  333 }
  334 
  335 /*
  336  * Step our concept of UTC.  This is done by modifying our estimate of
  337  * when we booted.  XXX: needs further work.
  338  */
  339 void
  340 tc_setclock(struct timespec *ts)
  341 {
  342         struct timespec ts2;
  343 
  344         nsetclock++;
  345         nanouptime(&ts2);
  346         boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
  347         /* XXX boottime should probably be a timespec. */
  348         boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
  349         if (boottime.tv_usec < 0) {
  350                 boottime.tv_usec += 1000000;
  351                 boottime.tv_sec--;
  352         }
  353         timeval2bintime(&boottime, &boottimebin);
  354 
  355         /* XXX fiddle all the little crinkly bits around the fiords... */
  356         tc_windup();
  357 }
  358 
  359 /*
  360  * Initialize the next struct timehands in the ring and make
  361  * it the active timehands.  Along the way we might switch to a different
  362  * timecounter and/or do seconds processing in NTP.  Slightly magic.
  363  */
  364 static void
  365 tc_windup(void)
  366 {
  367         struct bintime bt;
  368         struct timehands *th, *tho;
  369         u_int64_t scale;
  370         u_int delta, ncount, ogen;
  371         int i;
  372         time_t t;
  373 
  374         /*
  375          * Make the next timehands a copy of the current one, but do not
  376          * overwrite the generation or next pointer.  While we update
  377          * the contents, the generation must be zero.
  378          */
  379         tho = timehands;
  380         th = tho->th_next;
  381         ogen = th->th_generation;
  382         th->th_generation = 0;
  383         bcopy(tho, th, offsetof(struct timehands, th_generation));
  384 
  385         /*
  386          * Capture a timecounter delta on the current timecounter and if
  387          * changing timecounters, a counter value from the new timecounter.
  388          * Update the offset fields accordingly.
  389          */
  390         delta = tc_delta(th);
  391         if (th->th_counter != timecounter)
  392                 ncount = timecounter->tc_get_timecount(timecounter);
  393         else
  394                 ncount = 0;
  395         th->th_offset_count += delta;
  396         th->th_offset_count &= th->th_counter->tc_counter_mask;
  397         bintime_addx(&th->th_offset, th->th_scale * delta);
  398 
  399         /*
  400          * Hardware latching timecounters may not generate interrupts on
  401          * PPS events, so instead we poll them.  There is a finite risk that
  402          * the hardware might capture a count which is later than the one we
  403          * got above, and therefore possibly in the next NTP second which might
  404          * have a different rate than the current NTP second.  It doesn't
  405          * matter in practice.
  406          */
  407         if (tho->th_counter->tc_poll_pps)
  408                 tho->th_counter->tc_poll_pps(tho->th_counter);
  409 
  410         /*
  411          * Deal with NTP second processing.  The for loop normally
  412          * iterates at most once, but in extreme situations it might
  413          * keep NTP sane if timeouts are not run for several seconds.
  414          * At boot, the time step can be large when the TOD hardware
  415          * has been read, so on really large steps, we call
  416          * ntp_update_second only twice.  We need to call it twice in
  417          * case we missed a leap second.
  418          */
  419         bt = th->th_offset;
  420         bintime_add(&bt, &boottimebin);
  421         i = bt.sec - tho->th_microtime.tv_sec;
  422         if (i > LARGE_STEP)
  423                 i = 2;
  424         for (; i > 0; i--) {
  425                 t = bt.sec;
  426                 ntp_update_second(&th->th_adjustment, &bt.sec);
  427                 if (bt.sec != t)
  428                         boottimebin.sec += bt.sec - t;
  429         }
  430         /* Update the UTC timestamps used by the get*() functions. */
  431         /* XXX shouldn't do this here.  Should force non-`get' versions. */
  432         bintime2timeval(&bt, &th->th_microtime);
  433         bintime2timespec(&bt, &th->th_nanotime);
  434 
  435         /* Now is a good time to change timecounters. */
  436         if (th->th_counter != timecounter) {
  437                 th->th_counter = timecounter;
  438                 th->th_offset_count = ncount;
  439         }
  440 
  441         /*-
  442          * Recalculate the scaling factor.  We want the number of 1/2^64
  443          * fractions of a second per period of the hardware counter, taking
  444          * into account the th_adjustment factor which the NTP PLL/adjtime(2)
  445          * processing provides us with.
  446          *
  447          * The th_adjustment is nanoseconds per second with 32 bit binary
  448          * fraction and we want 64 bit binary fraction of second:
  449          *
  450          *       x = a * 2^32 / 10^9 = a * 4.294967296
  451          *
  452          * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
  453          * we can only multiply by about 850 without overflowing, but that
  454          * leaves suitably precise fractions for multiply before divide.
  455          *
  456          * Divide before multiply with a fraction of 2199/512 results in a
  457          * systematic undercompensation of 10PPM of th_adjustment.  On a
  458          * 5000PPM adjustment this is a 0.05PPM error.  This is acceptable.
  459          *
  460          * We happily sacrifice the lowest of the 64 bits of our result
  461          * to the goddess of code clarity.
  462          *
  463          */
  464         scale = (u_int64_t)1 << 63;
  465         scale += (th->th_adjustment / 1024) * 2199;
  466         scale /= th->th_counter->tc_frequency;
  467         th->th_scale = scale * 2;
  468 
  469         /*
  470          * Now that the struct timehands is again consistent, set the new
  471          * generation number, making sure to not make it zero.
  472          */
  473         if (++ogen == 0)
  474                 ogen = 1;
  475         th->th_generation = ogen;
  476 
  477         /* Go live with the new struct timehands. */
  478         time_second = th->th_microtime.tv_sec;
  479         time_uptime = th->th_offset.sec;
  480         timehands = th;
  481 }
  482 
  483 /* Report or change the active timecounter hardware. */
  484 static int
  485 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
  486 {
  487         char newname[32];
  488         struct timecounter *newtc, *tc;
  489         int error;
  490 
  491         tc = timecounter;
  492         strlcpy(newname, tc->tc_name, sizeof(newname));
  493 
  494         error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
  495         if (error != 0 || req->newptr == NULL ||
  496             strcmp(newname, tc->tc_name) == 0)
  497                 return (error);
  498         for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
  499                 if (strcmp(newname, newtc->tc_name) != 0)
  500                         continue;
  501 
  502                 /* Warm up new timecounter. */
  503                 (void)newtc->tc_get_timecount(newtc);
  504                 (void)newtc->tc_get_timecount(newtc);
  505 
  506                 timecounter = newtc;
  507                 return (0);
  508         }
  509         return (EINVAL);
  510 }
  511 
  512 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
  513     0, 0, sysctl_kern_timecounter_hardware, "A", "");
  514 
  515 
  516 /* Report or change the active timecounter hardware. */
  517 static int
  518 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
  519 {
  520         char buf[32], *spc;
  521         struct timecounter *tc;
  522         int error;
  523 
  524         spc = "";
  525         error = 0;
  526         for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
  527                 sprintf(buf, "%s%s(%d)",
  528                     spc, tc->tc_name, tc->tc_quality);
  529                 error = SYSCTL_OUT(req, buf, strlen(buf));
  530                 spc = " ";
  531         }
  532         return (error);
  533 }
  534 
  535 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
  536     0, 0, sysctl_kern_timecounter_choice, "A", "");
  537 
  538 /*
  539  * RFC 2783 PPS-API implementation.
  540  */
  541 
  542 int
  543 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
  544 {
  545         pps_params_t *app;
  546         struct pps_fetch_args *fapi;
  547 #ifdef PPS_SYNC
  548         struct pps_kcbind_args *kapi;
  549 #endif
  550 
  551         switch (cmd) {
  552         case PPS_IOC_CREATE:
  553                 return (0);
  554         case PPS_IOC_DESTROY:
  555                 return (0);
  556         case PPS_IOC_SETPARAMS:
  557                 app = (pps_params_t *)data;
  558                 if (app->mode & ~pps->ppscap)
  559                         return (EINVAL);
  560                 pps->ppsparam = *app;
  561                 return (0);
  562         case PPS_IOC_GETPARAMS:
  563                 app = (pps_params_t *)data;
  564                 *app = pps->ppsparam;
  565                 app->api_version = PPS_API_VERS_1;
  566                 return (0);
  567         case PPS_IOC_GETCAP:
  568                 *(int*)data = pps->ppscap;
  569                 return (0);
  570         case PPS_IOC_FETCH:
  571                 fapi = (struct pps_fetch_args *)data;
  572                 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
  573                         return (EINVAL);
  574                 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
  575                         return (EOPNOTSUPP);
  576                 pps->ppsinfo.current_mode = pps->ppsparam.mode;
  577                 fapi->pps_info_buf = pps->ppsinfo;
  578                 return (0);
  579         case PPS_IOC_KCBIND:
  580 #ifdef PPS_SYNC
  581                 kapi = (struct pps_kcbind_args *)data;
  582                 /* XXX Only root should be able to do this */
  583                 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
  584                         return (EINVAL);
  585                 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
  586                         return (EINVAL);
  587                 if (kapi->edge & ~pps->ppscap)
  588                         return (EINVAL);
  589                 pps->kcmode = kapi->edge;
  590                 return (0);
  591 #else
  592                 return (EOPNOTSUPP);
  593 #endif
  594         default:
  595                 return (ENOTTY);
  596         }
  597 }
  598 
  599 void
  600 pps_init(struct pps_state *pps)
  601 {
  602         pps->ppscap |= PPS_TSFMT_TSPEC;
  603         if (pps->ppscap & PPS_CAPTUREASSERT)
  604                 pps->ppscap |= PPS_OFFSETASSERT;
  605         if (pps->ppscap & PPS_CAPTURECLEAR)
  606                 pps->ppscap |= PPS_OFFSETCLEAR;
  607 }
  608 
  609 void
  610 pps_capture(struct pps_state *pps)
  611 {
  612         struct timehands *th;
  613 
  614         th = timehands;
  615         pps->capgen = th->th_generation;
  616         pps->capth = th;
  617         pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
  618         if (pps->capgen != th->th_generation)
  619                 pps->capgen = 0;
  620 }
  621 
  622 void
  623 pps_event(struct pps_state *pps, int event)
  624 {
  625         struct bintime bt;
  626         struct timespec ts, *tsp, *osp;
  627         u_int tcount, *pcount;
  628         int foff, fhard;
  629         pps_seq_t *pseq;
  630 
  631         /* If the timecounter was wound up underneath us, bail out. */
  632         if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
  633                 return;
  634 
  635         /* Things would be easier with arrays. */
  636         if (event == PPS_CAPTUREASSERT) {
  637                 tsp = &pps->ppsinfo.assert_timestamp;
  638                 osp = &pps->ppsparam.assert_offset;
  639                 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
  640                 fhard = pps->kcmode & PPS_CAPTUREASSERT;
  641                 pcount = &pps->ppscount[0];
  642                 pseq = &pps->ppsinfo.assert_sequence;
  643         } else {
  644                 tsp = &pps->ppsinfo.clear_timestamp;
  645                 osp = &pps->ppsparam.clear_offset;
  646                 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
  647                 fhard = pps->kcmode & PPS_CAPTURECLEAR;
  648                 pcount = &pps->ppscount[1];
  649                 pseq = &pps->ppsinfo.clear_sequence;
  650         }
  651 
  652         /*
  653          * If the timecounter changed, we cannot compare the count values, so
  654          * we have to drop the rest of the PPS-stuff until the next event.
  655          */
  656         if (pps->ppstc != pps->capth->th_counter) {
  657                 pps->ppstc = pps->capth->th_counter;
  658                 *pcount = pps->capcount;
  659                 pps->ppscount[2] = pps->capcount;
  660                 return;
  661         }
  662 
  663         /* Return if nothing really happened. */
  664         if (*pcount == pps->capcount)
  665                 return;
  666 
  667         /* Convert the count to a timespec. */
  668         tcount = pps->capcount - pps->capth->th_offset_count;
  669         tcount &= pps->capth->th_counter->tc_counter_mask;
  670         bt = pps->capth->th_offset;
  671         bintime_addx(&bt, pps->capth->th_scale * tcount);
  672         bintime_add(&bt, &boottimebin);
  673         bintime2timespec(&bt, &ts);
  674 
  675         /* If the timecounter was wound up underneath us, bail out. */
  676         if (pps->capgen != pps->capth->th_generation)
  677                 return;
  678 
  679         *pcount = pps->capcount;
  680         (*pseq)++;
  681         *tsp = ts;
  682 
  683         if (foff) {
  684                 timespecadd(tsp, osp);
  685                 if (tsp->tv_nsec < 0) {
  686                         tsp->tv_nsec += 1000000000;
  687                         tsp->tv_sec -= 1;
  688                 }
  689         }
  690 #ifdef PPS_SYNC
  691         if (fhard) {
  692                 u_int64_t scale;
  693 
  694                 /*
  695                  * Feed the NTP PLL/FLL.
  696                  * The FLL wants to know how many (hardware) nanoseconds
  697                  * elapsed since the previous event.
  698                  */
  699                 tcount = pps->capcount - pps->ppscount[2];
  700                 pps->ppscount[2] = pps->capcount;
  701                 tcount &= pps->capth->th_counter->tc_counter_mask;
  702                 scale = (u_int64_t)1 << 63;
  703                 scale /= pps->capth->th_counter->tc_frequency;
  704                 scale *= 2;
  705                 bt.sec = 0;
  706                 bt.frac = 0;
  707                 bintime_addx(&bt, scale * tcount);
  708                 bintime2timespec(&bt, &ts);
  709                 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
  710         }
  711 #endif
  712 }
  713 
  714 /*
  715  * Timecounters need to be updated every so often to prevent the hardware
  716  * counter from overflowing.  Updating also recalculates the cached values
  717  * used by the get*() family of functions, so their precision depends on
  718  * the update frequency.
  719  */
  720 
  721 static int tc_tick;
  722 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
  723 
  724 void
  725 tc_ticktock(void)
  726 {
  727         static int count;
  728 
  729         if (++count < tc_tick)
  730                 return;
  731         count = 0;
  732         tc_windup();
  733 }
  734 
  735 static void
  736 inittimecounter(void *dummy)
  737 {
  738         u_int p;
  739 
  740         /*
  741          * Set the initial timeout to
  742          * max(1, <approx. number of hardclock ticks in a millisecond>).
  743          * People should probably not use the sysctl to set the timeout
  744          * to smaller than its inital value, since that value is the
  745          * smallest reasonable one.  If they want better timestamps they
  746          * should use the non-"get"* functions.
  747          */
  748         if (hz > 1000)
  749                 tc_tick = (hz + 500) / 1000;
  750         else
  751                 tc_tick = 1;
  752         p = (tc_tick * 1000000) / hz;
  753         printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
  754 
  755         /* warm up new timecounter (again) and get rolling. */
  756         (void)timecounter->tc_get_timecount(timecounter);
  757         (void)timecounter->tc_get_timecount(timecounter);
  758 }
  759 
  760 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL)

Cache object: 0dcda3999ee9ab2dd232582be1f41c2e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.