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