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