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