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/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> 22 23 /* 24 * Number of timecounters used to implement stable storage 25 */ 26 #ifndef NTIMECOUNTER 27 #define NTIMECOUNTER 45 28 #endif 29 30 static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter", 31 "Timecounter stable storage"); 32 33 static void tco_setscales __P((struct timecounter *tc)); 34 static __inline unsigned tco_delta __P((struct timecounter *tc)); 35 36 time_t time_second; 37 38 struct timeval boottime; 39 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD, 40 &boottime, timeval, "System boottime"); 41 42 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, ""); 43 44 static unsigned nmicrotime; 45 static unsigned nnanotime; 46 static unsigned ngetmicrotime; 47 static unsigned ngetnanotime; 48 static unsigned nmicrouptime; 49 static unsigned nnanouptime; 50 static unsigned ngetmicrouptime; 51 static unsigned ngetnanouptime; 52 SYSCTL_INT(_kern_timecounter, OID_AUTO, nmicrotime, CTLFLAG_RD, &nmicrotime, 0, ""); 53 SYSCTL_INT(_kern_timecounter, OID_AUTO, nnanotime, CTLFLAG_RD, &nnanotime, 0, ""); 54 SYSCTL_INT(_kern_timecounter, OID_AUTO, nmicrouptime, CTLFLAG_RD, &nmicrouptime, 0, ""); 55 SYSCTL_INT(_kern_timecounter, OID_AUTO, nnanouptime, CTLFLAG_RD, &nnanouptime, 0, ""); 56 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetmicrotime, CTLFLAG_RD, &ngetmicrotime, 0, ""); 57 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetnanotime, CTLFLAG_RD, &ngetnanotime, 0, ""); 58 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetmicrouptime, CTLFLAG_RD, &ngetmicrouptime, 0, ""); 59 SYSCTL_INT(_kern_timecounter, OID_AUTO, ngetnanouptime, CTLFLAG_RD, &ngetnanouptime, 0, ""); 60 61 /* 62 * Implement a dummy timecounter which we can use until we get a real one 63 * in the air. This allows the console and other early stuff to use 64 * timeservices. 65 */ 66 67 static unsigned 68 dummy_get_timecount(struct timecounter *tc) 69 { 70 static unsigned now; 71 72 return (++now); 73 } 74 75 static struct timecounter dummy_timecounter = { 76 dummy_get_timecount, 77 0, 78 ~0u, 79 1000000, 80 "dummy" 81 }; 82 83 struct timecounter *timecounter = &dummy_timecounter; 84 85 static __inline unsigned 86 tco_delta(struct timecounter *tc) 87 { 88 89 return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) & 90 tc->tc_counter_mask); 91 } 92 93 /* 94 * We have eight functions for looking at the clock, four for 95 * microseconds and four for nanoseconds. For each there is fast 96 * but less precise version "get{nano|micro}[up]time" which will 97 * return a time which is up to 1/HZ previous to the call, whereas 98 * the raw version "{nano|micro}[up]time" will return a timestamp 99 * which is as precise as possible. The "up" variants return the 100 * time relative to system boot, these are well suited for time 101 * interval measurements. 102 */ 103 104 void 105 getmicrotime(struct timeval *tvp) 106 { 107 struct timecounter *tc; 108 109 ngetmicrotime++; 110 tc = timecounter; 111 *tvp = tc->tc_microtime; 112 } 113 114 void 115 getnanotime(struct timespec *tsp) 116 { 117 struct timecounter *tc; 118 119 ngetnanotime++; 120 tc = timecounter; 121 *tsp = tc->tc_nanotime; 122 } 123 124 void 125 microtime(struct timeval *tv) 126 { 127 struct timecounter *tc; 128 129 nmicrotime++; 130 tc = timecounter; 131 tv->tv_sec = tc->tc_offset_sec; 132 tv->tv_usec = tc->tc_offset_micro; 133 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32; 134 tv->tv_usec += boottime.tv_usec; 135 tv->tv_sec += boottime.tv_sec; 136 while (tv->tv_usec >= 1000000) { 137 tv->tv_usec -= 1000000; 138 tv->tv_sec++; 139 } 140 } 141 142 void 143 nanotime(struct timespec *ts) 144 { 145 unsigned count; 146 u_int64_t delta; 147 struct timecounter *tc; 148 149 nnanotime++; 150 tc = timecounter; 151 #ifdef KTR 152 if (tc == NULL) { /* called before initialization */ 153 ts->tv_sec = 0; 154 ts->tv_nsec = 0; 155 return; 156 } 157 #endif 158 ts->tv_sec = tc->tc_offset_sec; 159 count = tco_delta(tc); 160 delta = tc->tc_offset_nano; 161 delta += ((u_int64_t)count * tc->tc_scale_nano_f); 162 delta >>= 32; 163 delta += ((u_int64_t)count * tc->tc_scale_nano_i); 164 delta += boottime.tv_usec * 1000; 165 ts->tv_sec += boottime.tv_sec; 166 while (delta >= 1000000000) { 167 delta -= 1000000000; 168 ts->tv_sec++; 169 } 170 ts->tv_nsec = delta; 171 } 172 173 void 174 getmicrouptime(struct timeval *tvp) 175 { 176 struct timecounter *tc; 177 178 ngetmicrouptime++; 179 tc = timecounter; 180 tvp->tv_sec = tc->tc_offset_sec; 181 tvp->tv_usec = tc->tc_offset_micro; 182 } 183 184 void 185 getnanouptime(struct timespec *tsp) 186 { 187 struct timecounter *tc; 188 189 ngetnanouptime++; 190 tc = timecounter; 191 tsp->tv_sec = tc->tc_offset_sec; 192 tsp->tv_nsec = tc->tc_offset_nano >> 32; 193 } 194 195 void 196 microuptime(struct timeval *tv) 197 { 198 struct timecounter *tc; 199 200 nmicrouptime++; 201 tc = timecounter; 202 tv->tv_sec = tc->tc_offset_sec; 203 tv->tv_usec = tc->tc_offset_micro; 204 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32; 205 if (tv->tv_usec >= 1000000) { 206 tv->tv_usec -= 1000000; 207 tv->tv_sec++; 208 } 209 } 210 211 void 212 nanouptime(struct timespec *ts) 213 { 214 unsigned count; 215 u_int64_t delta; 216 struct timecounter *tc; 217 218 nnanouptime++; 219 tc = timecounter; 220 ts->tv_sec = tc->tc_offset_sec; 221 count = tco_delta(tc); 222 delta = tc->tc_offset_nano; 223 delta += ((u_int64_t)count * tc->tc_scale_nano_f); 224 delta >>= 32; 225 delta += ((u_int64_t)count * tc->tc_scale_nano_i); 226 if (delta >= 1000000000) { 227 delta -= 1000000000; 228 ts->tv_sec++; 229 } 230 ts->tv_nsec = delta; 231 } 232 233 static void 234 tco_setscales(struct timecounter *tc) 235 { 236 u_int64_t scale; 237 238 scale = 1000000000LL << 32; 239 scale += tc->tc_adjustment; 240 scale /= tc->tc_tweak->tc_frequency; 241 tc->tc_scale_micro = scale / 1000; 242 tc->tc_scale_nano_f = scale & 0xffffffff; 243 tc->tc_scale_nano_i = scale >> 32; 244 } 245 246 void 247 tc_update(struct timecounter *tc) 248 { 249 tco_setscales(tc); 250 } 251 252 void 253 tc_init(struct timecounter *tc) 254 { 255 struct timespec ts1; 256 struct timecounter *t1, *t2, *t3; 257 int i; 258 259 tc->tc_adjustment = 0; 260 tc->tc_tweak = tc; 261 tco_setscales(tc); 262 tc->tc_offset_count = tc->tc_get_timecount(tc); 263 if (timecounter == &dummy_timecounter) 264 tc->tc_avail = tc; 265 else { 266 tc->tc_avail = timecounter->tc_tweak->tc_avail; 267 timecounter->tc_tweak->tc_avail = tc; 268 } 269 MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK); 270 tc->tc_other = t1; 271 *t1 = *tc; 272 t2 = t1; 273 for (i = 1; i < NTIMECOUNTER; i++) { 274 MALLOC(t3, struct timecounter *, sizeof *t3, 275 M_TIMECOUNTER, M_WAITOK); 276 *t3 = *tc; 277 t3->tc_other = t2; 278 t2 = t3; 279 } 280 t1->tc_other = t3; 281 tc = t1; 282 283 printf("Timecounter \"%s\" frequency %lu Hz\n", 284 tc->tc_name, (u_long)tc->tc_frequency); 285 286 /* XXX: For now always start using the counter. */ 287 tc->tc_offset_count = tc->tc_get_timecount(tc); 288 nanouptime(&ts1); 289 tc->tc_offset_nano = (u_int64_t)ts1.tv_nsec << 32; 290 tc->tc_offset_micro = ts1.tv_nsec / 1000; 291 tc->tc_offset_sec = ts1.tv_sec; 292 timecounter = tc; 293 } 294 295 void 296 tc_setclock(struct timespec *ts) 297 { 298 struct timespec ts2; 299 300 nanouptime(&ts2); 301 boottime.tv_sec = ts->tv_sec - ts2.tv_sec; 302 boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000; 303 if (boottime.tv_usec < 0) { 304 boottime.tv_usec += 1000000; 305 boottime.tv_sec--; 306 } 307 /* fiddle all the little crinkly bits around the fiords... */ 308 tc_windup(); 309 } 310 311 static void 312 switch_timecounter(struct timecounter *newtc) 313 { 314 int s; 315 struct timecounter *tc; 316 struct timespec ts; 317 318 s = splclock(); 319 tc = timecounter; 320 if (newtc->tc_tweak == tc->tc_tweak) { 321 splx(s); 322 return; 323 } 324 newtc = newtc->tc_tweak->tc_other; 325 nanouptime(&ts); 326 newtc->tc_offset_sec = ts.tv_sec; 327 newtc->tc_offset_nano = (u_int64_t)ts.tv_nsec << 32; 328 newtc->tc_offset_micro = ts.tv_nsec / 1000; 329 newtc->tc_offset_count = newtc->tc_get_timecount(newtc); 330 tco_setscales(newtc); 331 timecounter = newtc; 332 splx(s); 333 } 334 335 static struct timecounter * 336 sync_other_counter(void) 337 { 338 struct timecounter *tc, *tcn, *tco; 339 unsigned delta; 340 341 tco = timecounter; 342 tc = tco->tc_other; 343 tcn = tc->tc_other; 344 *tc = *tco; 345 tc->tc_other = tcn; 346 delta = tco_delta(tc); 347 tc->tc_offset_count += delta; 348 tc->tc_offset_count &= tc->tc_counter_mask; 349 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_f; 350 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_i << 32; 351 return (tc); 352 } 353 354 void 355 tc_windup(void) 356 { 357 struct timecounter *tc, *tco; 358 struct timeval tvt; 359 360 tco = timecounter; 361 tc = sync_other_counter(); 362 /* 363 * We may be inducing a tiny error here, the tc_poll_pps() may 364 * process a latched count which happens after the tco_delta() 365 * in sync_other_counter(), which would extend the previous 366 * counters parameters into the domain of this new one. 367 * Since the timewindow is very small for this, the error is 368 * going to be only a few weenieseconds (as Dave Mills would 369 * say), so lets just not talk more about it, OK ? 370 */ 371 if (tco->tc_poll_pps) 372 tco->tc_poll_pps(tco); 373 if (timedelta != 0) { 374 tvt = boottime; 375 tvt.tv_usec += tickdelta; 376 if (tvt.tv_usec >= 1000000) { 377 tvt.tv_sec++; 378 tvt.tv_usec -= 1000000; 379 } else if (tvt.tv_usec < 0) { 380 tvt.tv_sec--; 381 tvt.tv_usec += 1000000; 382 } 383 boottime = tvt; 384 timedelta -= tickdelta; 385 } 386 387 while (tc->tc_offset_nano >= 1000000000ULL << 32) { 388 tc->tc_offset_nano -= 1000000000ULL << 32; 389 tc->tc_offset_sec++; 390 ntp_update_second(tc); /* XXX only needed if xntpd runs */ 391 tco_setscales(tc); 392 } 393 394 tc->tc_offset_micro = (tc->tc_offset_nano / 1000) >> 32; 395 396 /* Figure out the wall-clock time */ 397 tc->tc_nanotime.tv_sec = tc->tc_offset_sec + boottime.tv_sec; 398 tc->tc_nanotime.tv_nsec = 399 (tc->tc_offset_nano >> 32) + boottime.tv_usec * 1000; 400 tc->tc_microtime.tv_usec = tc->tc_offset_micro + boottime.tv_usec; 401 if (tc->tc_nanotime.tv_nsec >= 1000000000) { 402 tc->tc_nanotime.tv_nsec -= 1000000000; 403 tc->tc_microtime.tv_usec -= 1000000; 404 tc->tc_nanotime.tv_sec++; 405 } 406 time_second = tc->tc_microtime.tv_sec = tc->tc_nanotime.tv_sec; 407 408 timecounter = tc; 409 } 410 411 static int 412 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS) 413 { 414 char newname[32]; 415 struct timecounter *newtc, *tc; 416 int error; 417 418 tc = timecounter->tc_tweak; 419 strncpy(newname, tc->tc_name, sizeof(newname)); 420 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req); 421 if (error == 0 && req->newptr != NULL && 422 strcmp(newname, tc->tc_name) != 0) { 423 for (newtc = tc->tc_avail; newtc != tc; 424 newtc = newtc->tc_avail) { 425 if (strcmp(newname, newtc->tc_name) == 0) { 426 /* Warm up new timecounter. */ 427 (void)newtc->tc_get_timecount(newtc); 428 429 switch_timecounter(newtc); 430 return (0); 431 } 432 } 433 return (EINVAL); 434 } 435 return (error); 436 } 437 438 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW, 439 0, 0, sysctl_kern_timecounter_hardware, "A", ""); 440 441 442 int 443 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps) 444 { 445 pps_params_t *app; 446 struct pps_fetch_args *fapi; 447 #ifdef PPS_SYNC 448 struct pps_kcbind_args *kapi; 449 #endif 450 451 switch (cmd) { 452 case PPS_IOC_CREATE: 453 return (0); 454 case PPS_IOC_DESTROY: 455 return (0); 456 case PPS_IOC_SETPARAMS: 457 app = (pps_params_t *)data; 458 if (app->mode & ~pps->ppscap) 459 return (EINVAL); 460 pps->ppsparam = *app; 461 return (0); 462 case PPS_IOC_GETPARAMS: 463 app = (pps_params_t *)data; 464 *app = pps->ppsparam; 465 app->api_version = PPS_API_VERS_1; 466 return (0); 467 case PPS_IOC_GETCAP: 468 *(int*)data = pps->ppscap; 469 return (0); 470 case PPS_IOC_FETCH: 471 fapi = (struct pps_fetch_args *)data; 472 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC) 473 return (EINVAL); 474 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) 475 return (EOPNOTSUPP); 476 pps->ppsinfo.current_mode = pps->ppsparam.mode; 477 fapi->pps_info_buf = pps->ppsinfo; 478 return (0); 479 case PPS_IOC_KCBIND: 480 #ifdef PPS_SYNC 481 kapi = (struct pps_kcbind_args *)data; 482 /* XXX Only root should be able to do this */ 483 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC) 484 return (EINVAL); 485 if (kapi->kernel_consumer != PPS_KC_HARDPPS) 486 return (EINVAL); 487 if (kapi->edge & ~pps->ppscap) 488 return (EINVAL); 489 pps->kcmode = kapi->edge; 490 return (0); 491 #else 492 return (EOPNOTSUPP); 493 #endif 494 default: 495 return (ENOTTY); 496 } 497 } 498 499 void 500 pps_init(struct pps_state *pps) 501 { 502 pps->ppscap |= PPS_TSFMT_TSPEC; 503 if (pps->ppscap & PPS_CAPTUREASSERT) 504 pps->ppscap |= PPS_OFFSETASSERT; 505 if (pps->ppscap & PPS_CAPTURECLEAR) 506 pps->ppscap |= PPS_OFFSETCLEAR; 507 } 508 509 void 510 pps_event(struct pps_state *pps, struct timecounter *tc, unsigned count, int event) 511 { 512 struct timespec ts, *tsp, *osp; 513 u_int64_t delta; 514 unsigned tcount, *pcount; 515 int foff, fhard; 516 pps_seq_t *pseq; 517 518 /* Things would be easier with arrays... */ 519 if (event == PPS_CAPTUREASSERT) { 520 tsp = &pps->ppsinfo.assert_timestamp; 521 osp = &pps->ppsparam.assert_offset; 522 foff = pps->ppsparam.mode & PPS_OFFSETASSERT; 523 fhard = pps->kcmode & PPS_CAPTUREASSERT; 524 pcount = &pps->ppscount[0]; 525 pseq = &pps->ppsinfo.assert_sequence; 526 } else { 527 tsp = &pps->ppsinfo.clear_timestamp; 528 osp = &pps->ppsparam.clear_offset; 529 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR; 530 fhard = pps->kcmode & PPS_CAPTURECLEAR; 531 pcount = &pps->ppscount[1]; 532 pseq = &pps->ppsinfo.clear_sequence; 533 } 534 535 /* The timecounter changed: bail */ 536 if (!pps->ppstc || 537 pps->ppstc->tc_name != tc->tc_name || 538 tc->tc_name != timecounter->tc_name) { 539 pps->ppstc = tc; 540 *pcount = count; 541 return; 542 } 543 544 /* Nothing really happened */ 545 if (*pcount == count) 546 return; 547 548 *pcount = count; 549 550 /* Convert the count to timespec */ 551 ts.tv_sec = tc->tc_offset_sec; 552 tcount = count - tc->tc_offset_count; 553 tcount &= tc->tc_counter_mask; 554 delta = tc->tc_offset_nano; 555 delta += ((u_int64_t)tcount * tc->tc_scale_nano_f); 556 delta >>= 32; 557 delta += ((u_int64_t)tcount * tc->tc_scale_nano_i); 558 delta += boottime.tv_usec * 1000; 559 ts.tv_sec += boottime.tv_sec; 560 while (delta >= 1000000000) { 561 delta -= 1000000000; 562 ts.tv_sec++; 563 } 564 ts.tv_nsec = delta; 565 566 (*pseq)++; 567 *tsp = ts; 568 569 if (foff) { 570 timespecadd(tsp, osp); 571 if (tsp->tv_nsec < 0) { 572 tsp->tv_nsec += 1000000000; 573 tsp->tv_sec -= 1; 574 } 575 } 576 #ifdef PPS_SYNC 577 if (fhard) { 578 /* magic, at its best... */ 579 tcount = count - pps->ppscount[2]; 580 pps->ppscount[2] = count; 581 tcount &= tc->tc_counter_mask; 582 delta = ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_f); 583 delta >>= 32; 584 delta += ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_i); 585 hardpps(tsp, delta); 586 } 587 #endif 588 } 589