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 * ----------------------------------------------------------------------------
14 #include <sys/param.h>
15 #include <sys/timetc.h>
16 #include <sys/malloc.h>
17 #include <sys/kernel.h>
18 #include <sys/sysctl.h>
19 #include <sys/systm.h>
20 #include <sys/timex.h>
21 #include <sys/timepps.h>
24 * Number of timecounters used to implement stable storage
27 #define NTIMECOUNTER hz
30 static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter",
31 "Timecounter stable storage");
33 static void tco_setscales(struct timecounter *tc);
34 static __inline unsigned tco_delta(struct timecounter *tc);
38 struct bintime boottimebin;
39 struct timeval boottime;
40 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
41 &boottime, timeval, "System boottime");
43 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
45 static unsigned nbintime;
46 static unsigned nbinuptime;
47 static unsigned nmicrotime;
48 static unsigned nnanotime;
49 static unsigned ngetmicrotime;
50 static unsigned ngetnanotime;
51 static unsigned nmicrouptime;
52 static unsigned nnanouptime;
53 static unsigned ngetmicrouptime;
54 static unsigned ngetnanouptime;
55 SYSCTL_INT(_kern_timecounter, OID_AUTO, nbintime, CTLFLAG_RD, &nbintime, 0, "");
56 SYSCTL_INT(_kern_timecounter, OID_AUTO, nbinuptime, CTLFLAG_RD, &nbinuptime, 0, "");
57 SYSCTL_INT(_kern_timecounter, OID_AUTO, nmicrotime, CTLFLAG_RD, &nmicrotime, 0, "");
58 SYSCTL_INT(_kern_timecounter, OID_AUTO, nnanotime, CTLFLAG_RD, &nnanotime, 0, "");
59 SYSCTL_INT(_kern_timecounter, OID_AUTO, nmicrouptime, CTLFLAG_RD, &nmicrouptime, 0, "");
60 SYSCTL_INT(_kern_timecounter, OID_AUTO, nnanouptime, CTLFLAG_RD, &nnanouptime, 0, "");
61 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetmicrotime, CTLFLAG_RD, &ngetmicrotime, 0, "");
62 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetnanotime, CTLFLAG_RD, &ngetnanotime, 0, "");
63 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetmicrouptime, CTLFLAG_RD, &ngetmicrouptime, 0, "");
64 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetnanouptime, CTLFLAG_RD, &ngetnanouptime, 0, "");
67 * Implement a dummy timecounter which we can use until we get a real one
68 * in the air. This allows the console and other early stuff to use
73 dummy_get_timecount(struct timecounter *tc)
77 if (tc->tc_generation == 0)
82 static struct timecounter dummy_timecounter = {
90 struct timecounter *volatile timecounter = &dummy_timecounter;
92 static __inline unsigned
93 tco_delta(struct timecounter *tc)
96 return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
101 * We have eight functions for looking at the clock, four for
102 * microseconds and four for nanoseconds. For each there is fast
103 * but less precise version "get{nano|micro}[up]time" which will
104 * return a time which is up to 1/HZ previous to the call, whereas
105 * the raw version "{nano|micro}[up]time" will return a timestamp
106 * which is as precise as possible. The "up" variants return the
107 * time relative to system boot, these are well suited for time
108 * interval measurements.
112 binuptime(struct bintime *bt)
114 struct timecounter *tc;
120 gen = tc->tc_generation;
122 bintime_addx(bt, tc->tc_scale * tco_delta(tc));
123 } while (gen == 0 || gen != tc->tc_generation);
127 bintime(struct bintime *bt)
132 bintime_add(bt, &boottimebin);
136 getmicrotime(struct timeval *tvp)
138 struct timecounter *tc;
144 gen = tc->tc_generation;
145 *tvp = tc->tc_microtime;
146 } while (gen == 0 || gen != tc->tc_generation);
150 getnanotime(struct timespec *tsp)
152 struct timecounter *tc;
158 gen = tc->tc_generation;
159 *tsp = tc->tc_nanotime;
160 } while (gen == 0 || gen != tc->tc_generation);
164 microtime(struct timeval *tv)
170 bintime2timeval(&bt, tv);
174 nanotime(struct timespec *ts)
180 bintime2timespec(&bt, ts);
184 getmicrouptime(struct timeval *tvp)
186 struct timecounter *tc;
192 gen = tc->tc_generation;
193 bintime2timeval(&tc->tc_offset, tvp);
194 } while (gen == 0 || gen != tc->tc_generation);
198 getnanouptime(struct timespec *tsp)
200 struct timecounter *tc;
206 gen = tc->tc_generation;
207 bintime2timespec(&tc->tc_offset, tsp);
208 } while (gen == 0 || gen != tc->tc_generation);
212 microuptime(struct timeval *tv)
218 bintime2timeval(&bt, tv);
222 nanouptime(struct timespec *ts)
228 bintime2timespec(&bt, ts);
232 tco_setscales(struct timecounter *tc)
236 /* Sacrifice the lower bit to the deity for code clarity */
239 * We get nanoseconds with 32 bit binary fraction and want
240 * 64 bit binary fraction: x = a * 2^32 / 10^9 = a * 4.294967296
241 * The range is +/- 500PPM so we can multiply by about 8500
242 * without overflowing. 4398/1024 = is very close to ideal.
244 scale += (tc->tc_adjustment * 4398) >> 10;
245 scale /= tc->tc_tweak->tc_frequency;
246 tc->tc_scale = scale * 2;
250 tc_update(struct timecounter *tc)
256 tc_init(struct timecounter *tc)
258 struct timecounter *t1, *t2, *t3;
261 tc->tc_adjustment = 0;
264 tc->tc_offset_count = tc->tc_get_timecount(tc);
265 if (timecounter == &dummy_timecounter)
268 tc->tc_avail = timecounter->tc_tweak->tc_avail;
269 timecounter->tc_tweak->tc_avail = tc;
271 MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK | M_ZERO);
276 for (i = 1; i < NTIMECOUNTER; i++) {
277 MALLOC(t3, struct timecounter *, sizeof *t3,
278 M_TIMECOUNTER, M_WAITOK | M_ZERO);
286 printf("Timecounter \"%s\" frequency %lu Hz\n",
287 tc->tc_name, (u_long)tc->tc_frequency);
289 /* XXX: For now always start using the counter. */
290 tc->tc_offset_count = tc->tc_get_timecount(tc);
291 binuptime(&tc->tc_offset);
297 tc_setclock(struct timespec *ts)
302 boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
303 boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
304 if (boottime.tv_usec < 0) {
305 boottime.tv_usec += 1000000;
308 timeval2bintime(&boottime, &boottimebin);
309 /* fiddle all the little crinkly bits around the fiords... */
314 switch_timecounter(struct timecounter *newtc)
317 struct timecounter *tc;
321 if (newtc->tc_tweak == tc->tc_tweak) {
325 newtc = newtc->tc_tweak->tc_next;
326 binuptime(&newtc->tc_offset);
327 newtc->tc_offset_count = newtc->tc_get_timecount(newtc);
328 tco_setscales(newtc);
329 newtc->tc_generation = 0;
338 struct timecounter *tc, *tco;
341 unsigned ogen, delta;
346 ogen = tc->tc_generation;
347 tc->tc_generation = 0;
348 bcopy(tco, tc, __offsetof(struct timecounter, tc_generation));
349 delta = tco_delta(tc);
350 tc->tc_offset_count += delta;
351 tc->tc_offset_count &= tc->tc_counter_mask;
352 bintime_addx(&tc->tc_offset, tc->tc_scale * delta);
354 * We may be inducing a tiny error here, the tc_poll_pps() may
355 * process a latched count which happens after the tco_delta()
356 * in sync_other_counter(), which would extend the previous
357 * counters parameters into the domain of this new one.
358 * Since the timewindow is very small for this, the error is
359 * going to be only a few weenieseconds (as Dave Mills would
360 * say), so lets just not talk more about it, OK ?
362 if (tco->tc_poll_pps)
363 tco->tc_poll_pps(tco);
364 if (timedelta != 0) {
366 tvt.tv_usec += tickdelta;
367 if (tvt.tv_usec >= 1000000) {
369 tvt.tv_usec -= 1000000;
370 } else if (tvt.tv_usec < 0) {
372 tvt.tv_usec += 1000000;
375 timeval2bintime(&boottime, &boottimebin);
376 timedelta -= tickdelta;
378 for (i = tc->tc_offset.sec - tco->tc_offset.sec; i > 0; i--) {
379 ntp_update_second(tc); /* XXX only needed if xntpd runs */
384 bintime_add(&bt, &boottimebin);
385 bintime2timeval(&bt, &tc->tc_microtime);
386 bintime2timespec(&bt, &tc->tc_nanotime);
390 tc->tc_generation = ogen;
391 time_second = tc->tc_microtime.tv_sec;
396 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
399 struct timecounter *newtc, *tc;
402 tc = timecounter->tc_tweak;
403 strncpy(newname, tc->tc_name, sizeof(newname));
404 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
405 if (error == 0 && req->newptr != NULL &&
406 strcmp(newname, tc->tc_name) != 0) {
407 for (newtc = tc->tc_avail; newtc != tc;
408 newtc = newtc->tc_avail) {
409 if (strcmp(newname, newtc->tc_name) == 0) {
410 /* Warm up new timecounter. */
411 (void)newtc->tc_get_timecount(newtc);
413 switch_timecounter(newtc);
422 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
423 0, 0, sysctl_kern_timecounter_hardware, "A", "");
427 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
430 struct pps_fetch_args *fapi;
432 struct pps_kcbind_args *kapi;
438 case PPS_IOC_DESTROY:
440 case PPS_IOC_SETPARAMS:
441 app = (pps_params_t *)data;
442 if (app->mode & ~pps->ppscap)
444 pps->ppsparam = *app;
446 case PPS_IOC_GETPARAMS:
447 app = (pps_params_t *)data;
448 *app = pps->ppsparam;
449 app->api_version = PPS_API_VERS_1;
452 *(int*)data = pps->ppscap;
455 fapi = (struct pps_fetch_args *)data;
456 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
458 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
460 pps->ppsinfo.current_mode = pps->ppsparam.mode;
461 fapi->pps_info_buf = pps->ppsinfo;
465 kapi = (struct pps_kcbind_args *)data;
466 /* XXX Only root should be able to do this */
467 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
469 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
471 if (kapi->edge & ~pps->ppscap)
473 pps->kcmode = kapi->edge;
484 pps_init(struct pps_state *pps)
486 pps->ppscap |= PPS_TSFMT_TSPEC;
487 if (pps->ppscap & PPS_CAPTUREASSERT)
488 pps->ppscap |= PPS_OFFSETASSERT;
489 if (pps->ppscap & PPS_CAPTURECLEAR)
490 pps->ppscap |= PPS_OFFSETCLEAR;
494 pps_event(struct pps_state *pps, struct timecounter *tc, unsigned count, int event)
496 struct timespec ts, *tsp, *osp;
497 unsigned tcount, *pcount;
502 /* Things would be easier with arrays... */
503 if (event == PPS_CAPTUREASSERT) {
504 tsp = &pps->ppsinfo.assert_timestamp;
505 osp = &pps->ppsparam.assert_offset;
506 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
507 fhard = pps->kcmode & PPS_CAPTUREASSERT;
508 pcount = &pps->ppscount[0];
509 pseq = &pps->ppsinfo.assert_sequence;
511 tsp = &pps->ppsinfo.clear_timestamp;
512 osp = &pps->ppsparam.clear_offset;
513 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
514 fhard = pps->kcmode & PPS_CAPTURECLEAR;
515 pcount = &pps->ppscount[1];
516 pseq = &pps->ppsinfo.clear_sequence;
519 /* The timecounter changed: bail */
521 pps->ppstc->tc_name != tc->tc_name ||
522 tc->tc_name != timecounter->tc_name) {
528 /* Nothing really happened */
529 if (*pcount == count)
534 /* Convert the count to timespec */
535 tcount = count - tc->tc_offset_count;
536 tcount &= tc->tc_counter_mask;
538 bintime_addx(&bt, tc->tc_scale * tcount);
539 bintime2timespec(&bt, &ts);
545 timespecadd(tsp, osp);
546 if (tsp->tv_nsec < 0) {
547 tsp->tv_nsec += 1000000000;
553 /* magic, at its best... */
554 tcount = count - pps->ppscount[2];
555 pps->ppscount[2] = count;
556 tcount &= tc->tc_counter_mask;
559 bintime_addx(&bt, tc->tc_scale * tcount);
560 bintime2timespec(&bt, &ts);
561 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);