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