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