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