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