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