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