1 | /*- |
---|
2 | * ---------------------------------------------------------------------------- |
---|
3 | * "THE BEER-WARE LICENSE" (Revision 42): |
---|
4 | * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you |
---|
5 | * can do whatever you want with this stuff. If we meet some day, and you think |
---|
6 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
---|
7 | * ---------------------------------------------------------------------------- |
---|
8 | * |
---|
9 | * Copyright (c) 2011 The FreeBSD Foundation |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * Portions of this software were developed by Julien Ridoux at the University |
---|
13 | * of Melbourne under sponsorship from the FreeBSD Foundation. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifdef __rtems__ |
---|
17 | #define _KERNEL |
---|
18 | #define bintime _Timecounter_Bintime |
---|
19 | #define binuptime _Timecounter_Binuptime |
---|
20 | #define boottimebin _Timecounter_Boottimebin |
---|
21 | #define getbintime _Timecounter_Getbintime |
---|
22 | #define getbinuptime _Timecounter_Getbinuptime |
---|
23 | #define getmicrotime _Timecounter_Getmicrotime |
---|
24 | #define getmicrouptime _Timecounter_Getmicrouptime |
---|
25 | #define getnanotime _Timecounter_Getnanotime |
---|
26 | #define getnanouptime _Timecounter_Getnanouptime |
---|
27 | #define microtime _Timecounter_Microtime |
---|
28 | #define microuptime _Timecounter_Microuptime |
---|
29 | #define nanotime _Timecounter_Nanotime |
---|
30 | #define nanouptime _Timecounter_Nanouptime |
---|
31 | #define tc_init _Timecounter_Install |
---|
32 | #define timecounter _Timecounter |
---|
33 | #define time_second _Timecounter_Time_second |
---|
34 | #define time_uptime _Timecounter_Time_uptime |
---|
35 | #include <rtems/score/timecounterimpl.h> |
---|
36 | #include <rtems/score/watchdogimpl.h> |
---|
37 | #endif /* __rtems__ */ |
---|
38 | #include <sys/cdefs.h> |
---|
39 | __FBSDID("$FreeBSD r282424 2015-05-04T17:59:39Z$"); |
---|
40 | |
---|
41 | #include "opt_compat.h" |
---|
42 | #include "opt_ntp.h" |
---|
43 | #include "opt_ffclock.h" |
---|
44 | |
---|
45 | #include <sys/param.h> |
---|
46 | #ifndef __rtems__ |
---|
47 | #include <sys/kernel.h> |
---|
48 | #include <sys/limits.h> |
---|
49 | #include <sys/lock.h> |
---|
50 | #include <sys/mutex.h> |
---|
51 | #include <sys/sbuf.h> |
---|
52 | #include <sys/sysctl.h> |
---|
53 | #include <sys/syslog.h> |
---|
54 | #include <sys/systm.h> |
---|
55 | #endif /* __rtems__ */ |
---|
56 | #include <sys/timeffc.h> |
---|
57 | #include <sys/timepps.h> |
---|
58 | #include <sys/timetc.h> |
---|
59 | #include <sys/timex.h> |
---|
60 | #ifndef __rtems__ |
---|
61 | #include <sys/vdso.h> |
---|
62 | #endif /* __rtems__ */ |
---|
63 | #ifdef __rtems__ |
---|
64 | #include <limits.h> |
---|
65 | #include <rtems.h> |
---|
66 | ISR_LOCK_DEFINE(static, _Timecounter_Lock, "Timecounter"); |
---|
67 | #define hz rtems_clock_get_ticks_per_second() |
---|
68 | #define printf(...) |
---|
69 | #define log(...) |
---|
70 | static inline int |
---|
71 | fls(int x) |
---|
72 | { |
---|
73 | return x ? sizeof(x) * 8 - __builtin_clz(x) : 0; |
---|
74 | } |
---|
75 | /* FIXME: https://devel.rtems.org/ticket/2348 */ |
---|
76 | #define ntp_update_second(a, b) do { (void) a; (void) b; } while (0) |
---|
77 | #endif /* __rtems__ */ |
---|
78 | |
---|
79 | /* |
---|
80 | * A large step happens on boot. This constant detects such steps. |
---|
81 | * It is relatively small so that ntp_update_second gets called enough |
---|
82 | * in the typical 'missed a couple of seconds' case, but doesn't loop |
---|
83 | * forever when the time step is large. |
---|
84 | */ |
---|
85 | #define LARGE_STEP 200 |
---|
86 | |
---|
87 | /* |
---|
88 | * Implement a dummy timecounter which we can use until we get a real one |
---|
89 | * in the air. This allows the console and other early stuff to use |
---|
90 | * time services. |
---|
91 | */ |
---|
92 | |
---|
93 | static uint32_t |
---|
94 | dummy_get_timecount(struct timecounter *tc) |
---|
95 | { |
---|
96 | #ifndef __rtems__ |
---|
97 | static uint32_t now; |
---|
98 | |
---|
99 | return (++now); |
---|
100 | #else /* __rtems__ */ |
---|
101 | return 0; |
---|
102 | #endif /* __rtems__ */ |
---|
103 | } |
---|
104 | |
---|
105 | static struct timecounter dummy_timecounter = { |
---|
106 | dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000 |
---|
107 | }; |
---|
108 | |
---|
109 | struct timehands { |
---|
110 | /* These fields must be initialized by the driver. */ |
---|
111 | struct timecounter *th_counter; |
---|
112 | int64_t th_adjustment; |
---|
113 | uint64_t th_scale; |
---|
114 | uint32_t th_offset_count; |
---|
115 | struct bintime th_offset; |
---|
116 | struct timeval th_microtime; |
---|
117 | struct timespec th_nanotime; |
---|
118 | /* Fields not to be copied in tc_windup start with th_generation. */ |
---|
119 | volatile uint32_t th_generation; |
---|
120 | struct timehands *th_next; |
---|
121 | }; |
---|
122 | |
---|
123 | #if defined(RTEMS_SMP) |
---|
124 | static struct timehands th0; |
---|
125 | static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0}; |
---|
126 | static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9}; |
---|
127 | static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8}; |
---|
128 | static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7}; |
---|
129 | static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6}; |
---|
130 | static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5}; |
---|
131 | static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4}; |
---|
132 | static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3}; |
---|
133 | static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2}; |
---|
134 | #endif |
---|
135 | static struct timehands th0 = { |
---|
136 | &dummy_timecounter, |
---|
137 | 0, |
---|
138 | (uint64_t)-1 / 1000000, |
---|
139 | 0, |
---|
140 | {1, 0}, |
---|
141 | {0, 0}, |
---|
142 | {0, 0}, |
---|
143 | 1, |
---|
144 | #if defined(RTEMS_SMP) |
---|
145 | &th1 |
---|
146 | #else |
---|
147 | &th0 |
---|
148 | #endif |
---|
149 | }; |
---|
150 | |
---|
151 | static struct timehands *volatile timehands = &th0; |
---|
152 | struct timecounter *timecounter = &dummy_timecounter; |
---|
153 | static struct timecounter *timecounters = &dummy_timecounter; |
---|
154 | |
---|
155 | #ifndef __rtems__ |
---|
156 | int tc_min_ticktock_freq = 1; |
---|
157 | #endif /* __rtems__ */ |
---|
158 | |
---|
159 | volatile time_t time_second = 1; |
---|
160 | volatile time_t time_uptime = 1; |
---|
161 | |
---|
162 | struct bintime boottimebin; |
---|
163 | #ifndef __rtems__ |
---|
164 | struct timeval boottime; |
---|
165 | static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS); |
---|
166 | SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD, |
---|
167 | NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime"); |
---|
168 | |
---|
169 | SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, ""); |
---|
170 | static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, ""); |
---|
171 | |
---|
172 | static int timestepwarnings; |
---|
173 | SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW, |
---|
174 | ×tepwarnings, 0, "Log time steps"); |
---|
175 | |
---|
176 | struct bintime bt_timethreshold; |
---|
177 | struct bintime bt_tickthreshold; |
---|
178 | sbintime_t sbt_timethreshold; |
---|
179 | sbintime_t sbt_tickthreshold; |
---|
180 | struct bintime tc_tick_bt; |
---|
181 | sbintime_t tc_tick_sbt; |
---|
182 | int tc_precexp; |
---|
183 | int tc_timepercentage = TC_DEFAULTPERC; |
---|
184 | static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS); |
---|
185 | SYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation, |
---|
186 | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0, |
---|
187 | sysctl_kern_timecounter_adjprecision, "I", |
---|
188 | "Allowed time interval deviation in percents"); |
---|
189 | #endif /* __rtems__ */ |
---|
190 | |
---|
191 | static void tc_windup(void); |
---|
192 | #ifndef __rtems__ |
---|
193 | static void cpu_tick_calibrate(int); |
---|
194 | #endif /* __rtems__ */ |
---|
195 | |
---|
196 | void dtrace_getnanotime(struct timespec *tsp); |
---|
197 | |
---|
198 | #ifndef __rtems__ |
---|
199 | static int |
---|
200 | sysctl_kern_boottime(SYSCTL_HANDLER_ARGS) |
---|
201 | { |
---|
202 | #ifndef __mips__ |
---|
203 | #ifdef SCTL_MASK32 |
---|
204 | int tv[2]; |
---|
205 | |
---|
206 | if (req->flags & SCTL_MASK32) { |
---|
207 | tv[0] = boottime.tv_sec; |
---|
208 | tv[1] = boottime.tv_usec; |
---|
209 | return SYSCTL_OUT(req, tv, sizeof(tv)); |
---|
210 | } else |
---|
211 | #endif |
---|
212 | #endif |
---|
213 | return SYSCTL_OUT(req, &boottime, sizeof(boottime)); |
---|
214 | } |
---|
215 | |
---|
216 | static int |
---|
217 | sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS) |
---|
218 | { |
---|
219 | uint32_t ncount; |
---|
220 | struct timecounter *tc = arg1; |
---|
221 | |
---|
222 | ncount = tc->tc_get_timecount(tc); |
---|
223 | return sysctl_handle_int(oidp, &ncount, 0, req); |
---|
224 | } |
---|
225 | |
---|
226 | static int |
---|
227 | sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS) |
---|
228 | { |
---|
229 | uint64_t freq; |
---|
230 | struct timecounter *tc = arg1; |
---|
231 | |
---|
232 | freq = tc->tc_frequency; |
---|
233 | return sysctl_handle_64(oidp, &freq, 0, req); |
---|
234 | } |
---|
235 | #endif /* __rtems__ */ |
---|
236 | |
---|
237 | /* |
---|
238 | * Return the difference between the timehands' counter value now and what |
---|
239 | * was when we copied it to the timehands' offset_count. |
---|
240 | */ |
---|
241 | static __inline uint32_t |
---|
242 | tc_delta(struct timehands *th) |
---|
243 | { |
---|
244 | struct timecounter *tc; |
---|
245 | |
---|
246 | tc = th->th_counter; |
---|
247 | return ((tc->tc_get_timecount(tc) - th->th_offset_count) & |
---|
248 | tc->tc_counter_mask); |
---|
249 | } |
---|
250 | |
---|
251 | /* |
---|
252 | * Functions for reading the time. We have to loop until we are sure that |
---|
253 | * the timehands that we operated on was not updated under our feet. See |
---|
254 | * the comment in <sys/time.h> for a description of these 12 functions. |
---|
255 | */ |
---|
256 | |
---|
257 | #ifdef FFCLOCK |
---|
258 | void |
---|
259 | fbclock_binuptime(struct bintime *bt) |
---|
260 | { |
---|
261 | struct timehands *th; |
---|
262 | unsigned int gen; |
---|
263 | |
---|
264 | do { |
---|
265 | th = timehands; |
---|
266 | gen = th->th_generation; |
---|
267 | *bt = th->th_offset; |
---|
268 | bintime_addx(bt, th->th_scale * tc_delta(th)); |
---|
269 | } while (gen == 0 || gen != th->th_generation); |
---|
270 | } |
---|
271 | |
---|
272 | void |
---|
273 | fbclock_nanouptime(struct timespec *tsp) |
---|
274 | { |
---|
275 | struct bintime bt; |
---|
276 | |
---|
277 | fbclock_binuptime(&bt); |
---|
278 | bintime2timespec(&bt, tsp); |
---|
279 | } |
---|
280 | |
---|
281 | void |
---|
282 | fbclock_microuptime(struct timeval *tvp) |
---|
283 | { |
---|
284 | struct bintime bt; |
---|
285 | |
---|
286 | fbclock_binuptime(&bt); |
---|
287 | bintime2timeval(&bt, tvp); |
---|
288 | } |
---|
289 | |
---|
290 | void |
---|
291 | fbclock_bintime(struct bintime *bt) |
---|
292 | { |
---|
293 | |
---|
294 | fbclock_binuptime(bt); |
---|
295 | bintime_add(bt, &boottimebin); |
---|
296 | } |
---|
297 | |
---|
298 | void |
---|
299 | fbclock_nanotime(struct timespec *tsp) |
---|
300 | { |
---|
301 | struct bintime bt; |
---|
302 | |
---|
303 | fbclock_bintime(&bt); |
---|
304 | bintime2timespec(&bt, tsp); |
---|
305 | } |
---|
306 | |
---|
307 | void |
---|
308 | fbclock_microtime(struct timeval *tvp) |
---|
309 | { |
---|
310 | struct bintime bt; |
---|
311 | |
---|
312 | fbclock_bintime(&bt); |
---|
313 | bintime2timeval(&bt, tvp); |
---|
314 | } |
---|
315 | |
---|
316 | void |
---|
317 | fbclock_getbinuptime(struct bintime *bt) |
---|
318 | { |
---|
319 | struct timehands *th; |
---|
320 | unsigned int gen; |
---|
321 | |
---|
322 | do { |
---|
323 | th = timehands; |
---|
324 | gen = th->th_generation; |
---|
325 | *bt = th->th_offset; |
---|
326 | } while (gen == 0 || gen != th->th_generation); |
---|
327 | } |
---|
328 | |
---|
329 | void |
---|
330 | fbclock_getnanouptime(struct timespec *tsp) |
---|
331 | { |
---|
332 | struct timehands *th; |
---|
333 | unsigned int gen; |
---|
334 | |
---|
335 | do { |
---|
336 | th = timehands; |
---|
337 | gen = th->th_generation; |
---|
338 | bintime2timespec(&th->th_offset, tsp); |
---|
339 | } while (gen == 0 || gen != th->th_generation); |
---|
340 | } |
---|
341 | |
---|
342 | void |
---|
343 | fbclock_getmicrouptime(struct timeval *tvp) |
---|
344 | { |
---|
345 | struct timehands *th; |
---|
346 | unsigned int gen; |
---|
347 | |
---|
348 | do { |
---|
349 | th = timehands; |
---|
350 | gen = th->th_generation; |
---|
351 | bintime2timeval(&th->th_offset, tvp); |
---|
352 | } while (gen == 0 || gen != th->th_generation); |
---|
353 | } |
---|
354 | |
---|
355 | void |
---|
356 | fbclock_getbintime(struct bintime *bt) |
---|
357 | { |
---|
358 | struct timehands *th; |
---|
359 | unsigned int gen; |
---|
360 | |
---|
361 | do { |
---|
362 | th = timehands; |
---|
363 | gen = th->th_generation; |
---|
364 | *bt = th->th_offset; |
---|
365 | } while (gen == 0 || gen != th->th_generation); |
---|
366 | bintime_add(bt, &boottimebin); |
---|
367 | } |
---|
368 | |
---|
369 | void |
---|
370 | fbclock_getnanotime(struct timespec *tsp) |
---|
371 | { |
---|
372 | struct timehands *th; |
---|
373 | unsigned int gen; |
---|
374 | |
---|
375 | do { |
---|
376 | th = timehands; |
---|
377 | gen = th->th_generation; |
---|
378 | *tsp = th->th_nanotime; |
---|
379 | } while (gen == 0 || gen != th->th_generation); |
---|
380 | } |
---|
381 | |
---|
382 | void |
---|
383 | fbclock_getmicrotime(struct timeval *tvp) |
---|
384 | { |
---|
385 | struct timehands *th; |
---|
386 | unsigned int gen; |
---|
387 | |
---|
388 | do { |
---|
389 | th = timehands; |
---|
390 | gen = th->th_generation; |
---|
391 | *tvp = th->th_microtime; |
---|
392 | } while (gen == 0 || gen != th->th_generation); |
---|
393 | } |
---|
394 | #else /* !FFCLOCK */ |
---|
395 | void |
---|
396 | binuptime(struct bintime *bt) |
---|
397 | { |
---|
398 | struct timehands *th; |
---|
399 | uint32_t gen; |
---|
400 | |
---|
401 | do { |
---|
402 | th = timehands; |
---|
403 | gen = th->th_generation; |
---|
404 | *bt = th->th_offset; |
---|
405 | bintime_addx(bt, th->th_scale * tc_delta(th)); |
---|
406 | } while (gen == 0 || gen != th->th_generation); |
---|
407 | } |
---|
408 | |
---|
409 | void |
---|
410 | nanouptime(struct timespec *tsp) |
---|
411 | { |
---|
412 | struct bintime bt; |
---|
413 | |
---|
414 | binuptime(&bt); |
---|
415 | bintime2timespec(&bt, tsp); |
---|
416 | } |
---|
417 | |
---|
418 | void |
---|
419 | microuptime(struct timeval *tvp) |
---|
420 | { |
---|
421 | struct bintime bt; |
---|
422 | |
---|
423 | binuptime(&bt); |
---|
424 | bintime2timeval(&bt, tvp); |
---|
425 | } |
---|
426 | |
---|
427 | void |
---|
428 | bintime(struct bintime *bt) |
---|
429 | { |
---|
430 | |
---|
431 | binuptime(bt); |
---|
432 | bintime_add(bt, &boottimebin); |
---|
433 | } |
---|
434 | |
---|
435 | void |
---|
436 | nanotime(struct timespec *tsp) |
---|
437 | { |
---|
438 | struct bintime bt; |
---|
439 | |
---|
440 | bintime(&bt); |
---|
441 | bintime2timespec(&bt, tsp); |
---|
442 | } |
---|
443 | |
---|
444 | void |
---|
445 | microtime(struct timeval *tvp) |
---|
446 | { |
---|
447 | struct bintime bt; |
---|
448 | |
---|
449 | bintime(&bt); |
---|
450 | bintime2timeval(&bt, tvp); |
---|
451 | } |
---|
452 | |
---|
453 | void |
---|
454 | getbinuptime(struct bintime *bt) |
---|
455 | { |
---|
456 | struct timehands *th; |
---|
457 | uint32_t gen; |
---|
458 | |
---|
459 | do { |
---|
460 | th = timehands; |
---|
461 | gen = th->th_generation; |
---|
462 | *bt = th->th_offset; |
---|
463 | } while (gen == 0 || gen != th->th_generation); |
---|
464 | } |
---|
465 | |
---|
466 | void |
---|
467 | getnanouptime(struct timespec *tsp) |
---|
468 | { |
---|
469 | struct timehands *th; |
---|
470 | uint32_t gen; |
---|
471 | |
---|
472 | do { |
---|
473 | th = timehands; |
---|
474 | gen = th->th_generation; |
---|
475 | bintime2timespec(&th->th_offset, tsp); |
---|
476 | } while (gen == 0 || gen != th->th_generation); |
---|
477 | } |
---|
478 | |
---|
479 | void |
---|
480 | getmicrouptime(struct timeval *tvp) |
---|
481 | { |
---|
482 | struct timehands *th; |
---|
483 | uint32_t gen; |
---|
484 | |
---|
485 | do { |
---|
486 | th = timehands; |
---|
487 | gen = th->th_generation; |
---|
488 | bintime2timeval(&th->th_offset, tvp); |
---|
489 | } while (gen == 0 || gen != th->th_generation); |
---|
490 | } |
---|
491 | |
---|
492 | void |
---|
493 | getbintime(struct bintime *bt) |
---|
494 | { |
---|
495 | struct timehands *th; |
---|
496 | uint32_t gen; |
---|
497 | |
---|
498 | do { |
---|
499 | th = timehands; |
---|
500 | gen = th->th_generation; |
---|
501 | *bt = th->th_offset; |
---|
502 | } while (gen == 0 || gen != th->th_generation); |
---|
503 | bintime_add(bt, &boottimebin); |
---|
504 | } |
---|
505 | |
---|
506 | void |
---|
507 | getnanotime(struct timespec *tsp) |
---|
508 | { |
---|
509 | struct timehands *th; |
---|
510 | uint32_t gen; |
---|
511 | |
---|
512 | do { |
---|
513 | th = timehands; |
---|
514 | gen = th->th_generation; |
---|
515 | *tsp = th->th_nanotime; |
---|
516 | } while (gen == 0 || gen != th->th_generation); |
---|
517 | } |
---|
518 | |
---|
519 | void |
---|
520 | getmicrotime(struct timeval *tvp) |
---|
521 | { |
---|
522 | struct timehands *th; |
---|
523 | uint32_t gen; |
---|
524 | |
---|
525 | do { |
---|
526 | th = timehands; |
---|
527 | gen = th->th_generation; |
---|
528 | *tvp = th->th_microtime; |
---|
529 | } while (gen == 0 || gen != th->th_generation); |
---|
530 | } |
---|
531 | #endif /* FFCLOCK */ |
---|
532 | |
---|
533 | #ifdef FFCLOCK |
---|
534 | /* |
---|
535 | * Support for feed-forward synchronization algorithms. This is heavily inspired |
---|
536 | * by the timehands mechanism but kept independent from it. *_windup() functions |
---|
537 | * have some connection to avoid accessing the timecounter hardware more than |
---|
538 | * necessary. |
---|
539 | */ |
---|
540 | |
---|
541 | /* Feed-forward clock estimates kept updated by the synchronization daemon. */ |
---|
542 | struct ffclock_estimate ffclock_estimate; |
---|
543 | struct bintime ffclock_boottime; /* Feed-forward boot time estimate. */ |
---|
544 | uint32_t ffclock_status; /* Feed-forward clock status. */ |
---|
545 | int8_t ffclock_updated; /* New estimates are available. */ |
---|
546 | struct mtx ffclock_mtx; /* Mutex on ffclock_estimate. */ |
---|
547 | |
---|
548 | struct fftimehands { |
---|
549 | struct ffclock_estimate cest; |
---|
550 | struct bintime tick_time; |
---|
551 | struct bintime tick_time_lerp; |
---|
552 | ffcounter tick_ffcount; |
---|
553 | uint64_t period_lerp; |
---|
554 | volatile uint8_t gen; |
---|
555 | struct fftimehands *next; |
---|
556 | }; |
---|
557 | |
---|
558 | #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x)) |
---|
559 | |
---|
560 | static struct fftimehands ffth[10]; |
---|
561 | static struct fftimehands *volatile fftimehands = ffth; |
---|
562 | |
---|
563 | static void |
---|
564 | ffclock_init(void) |
---|
565 | { |
---|
566 | struct fftimehands *cur; |
---|
567 | struct fftimehands *last; |
---|
568 | |
---|
569 | memset(ffth, 0, sizeof(ffth)); |
---|
570 | |
---|
571 | last = ffth + NUM_ELEMENTS(ffth) - 1; |
---|
572 | for (cur = ffth; cur < last; cur++) |
---|
573 | cur->next = cur + 1; |
---|
574 | last->next = ffth; |
---|
575 | |
---|
576 | ffclock_updated = 0; |
---|
577 | ffclock_status = FFCLOCK_STA_UNSYNC; |
---|
578 | mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF); |
---|
579 | } |
---|
580 | |
---|
581 | /* |
---|
582 | * Reset the feed-forward clock estimates. Called from inittodr() to get things |
---|
583 | * kick started and uses the timecounter nominal frequency as a first period |
---|
584 | * estimate. Note: this function may be called several time just after boot. |
---|
585 | * Note: this is the only function that sets the value of boot time for the |
---|
586 | * monotonic (i.e. uptime) version of the feed-forward clock. |
---|
587 | */ |
---|
588 | void |
---|
589 | ffclock_reset_clock(struct timespec *ts) |
---|
590 | { |
---|
591 | struct timecounter *tc; |
---|
592 | struct ffclock_estimate cest; |
---|
593 | |
---|
594 | tc = timehands->th_counter; |
---|
595 | memset(&cest, 0, sizeof(struct ffclock_estimate)); |
---|
596 | |
---|
597 | timespec2bintime(ts, &ffclock_boottime); |
---|
598 | timespec2bintime(ts, &(cest.update_time)); |
---|
599 | ffclock_read_counter(&cest.update_ffcount); |
---|
600 | cest.leapsec_next = 0; |
---|
601 | cest.period = ((1ULL << 63) / tc->tc_frequency) << 1; |
---|
602 | cest.errb_abs = 0; |
---|
603 | cest.errb_rate = 0; |
---|
604 | cest.status = FFCLOCK_STA_UNSYNC; |
---|
605 | cest.leapsec_total = 0; |
---|
606 | cest.leapsec = 0; |
---|
607 | |
---|
608 | mtx_lock(&ffclock_mtx); |
---|
609 | bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate)); |
---|
610 | ffclock_updated = INT8_MAX; |
---|
611 | mtx_unlock(&ffclock_mtx); |
---|
612 | |
---|
613 | printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name, |
---|
614 | (unsigned long long)tc->tc_frequency, (long)ts->tv_sec, |
---|
615 | (unsigned long)ts->tv_nsec); |
---|
616 | } |
---|
617 | |
---|
618 | /* |
---|
619 | * Sub-routine to convert a time interval measured in RAW counter units to time |
---|
620 | * in seconds stored in bintime format. |
---|
621 | * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be |
---|
622 | * larger than the max value of u_int (on 32 bit architecture). Loop to consume |
---|
623 | * extra cycles. |
---|
624 | */ |
---|
625 | static void |
---|
626 | ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt) |
---|
627 | { |
---|
628 | struct bintime bt2; |
---|
629 | ffcounter delta, delta_max; |
---|
630 | |
---|
631 | delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1; |
---|
632 | bintime_clear(bt); |
---|
633 | do { |
---|
634 | if (ffdelta > delta_max) |
---|
635 | delta = delta_max; |
---|
636 | else |
---|
637 | delta = ffdelta; |
---|
638 | bt2.sec = 0; |
---|
639 | bt2.frac = period; |
---|
640 | bintime_mul(&bt2, (unsigned int)delta); |
---|
641 | bintime_add(bt, &bt2); |
---|
642 | ffdelta -= delta; |
---|
643 | } while (ffdelta > 0); |
---|
644 | } |
---|
645 | |
---|
646 | /* |
---|
647 | * Update the fftimehands. |
---|
648 | * Push the tick ffcount and time(s) forward based on current clock estimate. |
---|
649 | * The conversion from ffcounter to bintime relies on the difference clock |
---|
650 | * principle, whose accuracy relies on computing small time intervals. If a new |
---|
651 | * clock estimate has been passed by the synchronisation daemon, make it |
---|
652 | * current, and compute the linear interpolation for monotonic time if needed. |
---|
653 | */ |
---|
654 | static void |
---|
655 | ffclock_windup(unsigned int delta) |
---|
656 | { |
---|
657 | struct ffclock_estimate *cest; |
---|
658 | struct fftimehands *ffth; |
---|
659 | struct bintime bt, gap_lerp; |
---|
660 | ffcounter ffdelta; |
---|
661 | uint64_t frac; |
---|
662 | unsigned int polling; |
---|
663 | uint8_t forward_jump, ogen; |
---|
664 | |
---|
665 | /* |
---|
666 | * Pick the next timehand, copy current ffclock estimates and move tick |
---|
667 | * times and counter forward. |
---|
668 | */ |
---|
669 | forward_jump = 0; |
---|
670 | ffth = fftimehands->next; |
---|
671 | ogen = ffth->gen; |
---|
672 | ffth->gen = 0; |
---|
673 | cest = &ffth->cest; |
---|
674 | bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate)); |
---|
675 | ffdelta = (ffcounter)delta; |
---|
676 | ffth->period_lerp = fftimehands->period_lerp; |
---|
677 | |
---|
678 | ffth->tick_time = fftimehands->tick_time; |
---|
679 | ffclock_convert_delta(ffdelta, cest->period, &bt); |
---|
680 | bintime_add(&ffth->tick_time, &bt); |
---|
681 | |
---|
682 | ffth->tick_time_lerp = fftimehands->tick_time_lerp; |
---|
683 | ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt); |
---|
684 | bintime_add(&ffth->tick_time_lerp, &bt); |
---|
685 | |
---|
686 | ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta; |
---|
687 | |
---|
688 | /* |
---|
689 | * Assess the status of the clock, if the last update is too old, it is |
---|
690 | * likely the synchronisation daemon is dead and the clock is free |
---|
691 | * running. |
---|
692 | */ |
---|
693 | if (ffclock_updated == 0) { |
---|
694 | ffdelta = ffth->tick_ffcount - cest->update_ffcount; |
---|
695 | ffclock_convert_delta(ffdelta, cest->period, &bt); |
---|
696 | if (bt.sec > 2 * FFCLOCK_SKM_SCALE) |
---|
697 | ffclock_status |= FFCLOCK_STA_UNSYNC; |
---|
698 | } |
---|
699 | |
---|
700 | /* |
---|
701 | * If available, grab updated clock estimates and make them current. |
---|
702 | * Recompute time at this tick using the updated estimates. The clock |
---|
703 | * estimates passed the feed-forward synchronisation daemon may result |
---|
704 | * in time conversion that is not monotonically increasing (just after |
---|
705 | * the update). time_lerp is a particular linear interpolation over the |
---|
706 | * synchronisation algo polling period that ensures monotonicity for the |
---|
707 | * clock ids requesting it. |
---|
708 | */ |
---|
709 | if (ffclock_updated > 0) { |
---|
710 | bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate)); |
---|
711 | ffdelta = ffth->tick_ffcount - cest->update_ffcount; |
---|
712 | ffth->tick_time = cest->update_time; |
---|
713 | ffclock_convert_delta(ffdelta, cest->period, &bt); |
---|
714 | bintime_add(&ffth->tick_time, &bt); |
---|
715 | |
---|
716 | /* ffclock_reset sets ffclock_updated to INT8_MAX */ |
---|
717 | if (ffclock_updated == INT8_MAX) |
---|
718 | ffth->tick_time_lerp = ffth->tick_time; |
---|
719 | |
---|
720 | if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >)) |
---|
721 | forward_jump = 1; |
---|
722 | else |
---|
723 | forward_jump = 0; |
---|
724 | |
---|
725 | bintime_clear(&gap_lerp); |
---|
726 | if (forward_jump) { |
---|
727 | gap_lerp = ffth->tick_time; |
---|
728 | bintime_sub(&gap_lerp, &ffth->tick_time_lerp); |
---|
729 | } else { |
---|
730 | gap_lerp = ffth->tick_time_lerp; |
---|
731 | bintime_sub(&gap_lerp, &ffth->tick_time); |
---|
732 | } |
---|
733 | |
---|
734 | /* |
---|
735 | * The reset from the RTC clock may be far from accurate, and |
---|
736 | * reducing the gap between real time and interpolated time |
---|
737 | * could take a very long time if the interpolated clock insists |
---|
738 | * on strict monotonicity. The clock is reset under very strict |
---|
739 | * conditions (kernel time is known to be wrong and |
---|
740 | * synchronization daemon has been restarted recently. |
---|
741 | * ffclock_boottime absorbs the jump to ensure boot time is |
---|
742 | * correct and uptime functions stay consistent. |
---|
743 | */ |
---|
744 | if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) && |
---|
745 | ((cest->status & FFCLOCK_STA_UNSYNC) == 0) && |
---|
746 | ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) { |
---|
747 | if (forward_jump) |
---|
748 | bintime_add(&ffclock_boottime, &gap_lerp); |
---|
749 | else |
---|
750 | bintime_sub(&ffclock_boottime, &gap_lerp); |
---|
751 | ffth->tick_time_lerp = ffth->tick_time; |
---|
752 | bintime_clear(&gap_lerp); |
---|
753 | } |
---|
754 | |
---|
755 | ffclock_status = cest->status; |
---|
756 | ffth->period_lerp = cest->period; |
---|
757 | |
---|
758 | /* |
---|
759 | * Compute corrected period used for the linear interpolation of |
---|
760 | * time. The rate of linear interpolation is capped to 5000PPM |
---|
761 | * (5ms/s). |
---|
762 | */ |
---|
763 | if (bintime_isset(&gap_lerp)) { |
---|
764 | ffdelta = cest->update_ffcount; |
---|
765 | ffdelta -= fftimehands->cest.update_ffcount; |
---|
766 | ffclock_convert_delta(ffdelta, cest->period, &bt); |
---|
767 | polling = bt.sec; |
---|
768 | bt.sec = 0; |
---|
769 | bt.frac = 5000000 * (uint64_t)18446744073LL; |
---|
770 | bintime_mul(&bt, polling); |
---|
771 | if (bintime_cmp(&gap_lerp, &bt, >)) |
---|
772 | gap_lerp = bt; |
---|
773 | |
---|
774 | /* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */ |
---|
775 | frac = 0; |
---|
776 | if (gap_lerp.sec > 0) { |
---|
777 | frac -= 1; |
---|
778 | frac /= ffdelta / gap_lerp.sec; |
---|
779 | } |
---|
780 | frac += gap_lerp.frac / ffdelta; |
---|
781 | |
---|
782 | if (forward_jump) |
---|
783 | ffth->period_lerp += frac; |
---|
784 | else |
---|
785 | ffth->period_lerp -= frac; |
---|
786 | } |
---|
787 | |
---|
788 | ffclock_updated = 0; |
---|
789 | } |
---|
790 | if (++ogen == 0) |
---|
791 | ogen = 1; |
---|
792 | ffth->gen = ogen; |
---|
793 | fftimehands = ffth; |
---|
794 | } |
---|
795 | |
---|
796 | /* |
---|
797 | * Adjust the fftimehands when the timecounter is changed. Stating the obvious, |
---|
798 | * the old and new hardware counter cannot be read simultaneously. tc_windup() |
---|
799 | * does read the two counters 'back to back', but a few cycles are effectively |
---|
800 | * lost, and not accumulated in tick_ffcount. This is a fairly radical |
---|
801 | * operation for a feed-forward synchronization daemon, and it is its job to not |
---|
802 | * pushing irrelevant data to the kernel. Because there is no locking here, |
---|
803 | * simply force to ignore pending or next update to give daemon a chance to |
---|
804 | * realize the counter has changed. |
---|
805 | */ |
---|
806 | static void |
---|
807 | ffclock_change_tc(struct timehands *th) |
---|
808 | { |
---|
809 | struct fftimehands *ffth; |
---|
810 | struct ffclock_estimate *cest; |
---|
811 | struct timecounter *tc; |
---|
812 | uint8_t ogen; |
---|
813 | |
---|
814 | tc = th->th_counter; |
---|
815 | ffth = fftimehands->next; |
---|
816 | ogen = ffth->gen; |
---|
817 | ffth->gen = 0; |
---|
818 | |
---|
819 | cest = &ffth->cest; |
---|
820 | bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate)); |
---|
821 | cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1; |
---|
822 | cest->errb_abs = 0; |
---|
823 | cest->errb_rate = 0; |
---|
824 | cest->status |= FFCLOCK_STA_UNSYNC; |
---|
825 | |
---|
826 | ffth->tick_ffcount = fftimehands->tick_ffcount; |
---|
827 | ffth->tick_time_lerp = fftimehands->tick_time_lerp; |
---|
828 | ffth->tick_time = fftimehands->tick_time; |
---|
829 | ffth->period_lerp = cest->period; |
---|
830 | |
---|
831 | /* Do not lock but ignore next update from synchronization daemon. */ |
---|
832 | ffclock_updated--; |
---|
833 | |
---|
834 | if (++ogen == 0) |
---|
835 | ogen = 1; |
---|
836 | ffth->gen = ogen; |
---|
837 | fftimehands = ffth; |
---|
838 | } |
---|
839 | |
---|
840 | /* |
---|
841 | * Retrieve feed-forward counter and time of last kernel tick. |
---|
842 | */ |
---|
843 | void |
---|
844 | ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags) |
---|
845 | { |
---|
846 | struct fftimehands *ffth; |
---|
847 | uint8_t gen; |
---|
848 | |
---|
849 | /* |
---|
850 | * No locking but check generation has not changed. Also need to make |
---|
851 | * sure ffdelta is positive, i.e. ffcount > tick_ffcount. |
---|
852 | */ |
---|
853 | do { |
---|
854 | ffth = fftimehands; |
---|
855 | gen = ffth->gen; |
---|
856 | if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) |
---|
857 | *bt = ffth->tick_time_lerp; |
---|
858 | else |
---|
859 | *bt = ffth->tick_time; |
---|
860 | *ffcount = ffth->tick_ffcount; |
---|
861 | } while (gen == 0 || gen != ffth->gen); |
---|
862 | } |
---|
863 | |
---|
864 | /* |
---|
865 | * Absolute clock conversion. Low level function to convert ffcounter to |
---|
866 | * bintime. The ffcounter is converted using the current ffclock period estimate |
---|
867 | * or the "interpolated period" to ensure monotonicity. |
---|
868 | * NOTE: this conversion may have been deferred, and the clock updated since the |
---|
869 | * hardware counter has been read. |
---|
870 | */ |
---|
871 | void |
---|
872 | ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags) |
---|
873 | { |
---|
874 | struct fftimehands *ffth; |
---|
875 | struct bintime bt2; |
---|
876 | ffcounter ffdelta; |
---|
877 | uint8_t gen; |
---|
878 | |
---|
879 | /* |
---|
880 | * No locking but check generation has not changed. Also need to make |
---|
881 | * sure ffdelta is positive, i.e. ffcount > tick_ffcount. |
---|
882 | */ |
---|
883 | do { |
---|
884 | ffth = fftimehands; |
---|
885 | gen = ffth->gen; |
---|
886 | if (ffcount > ffth->tick_ffcount) |
---|
887 | ffdelta = ffcount - ffth->tick_ffcount; |
---|
888 | else |
---|
889 | ffdelta = ffth->tick_ffcount - ffcount; |
---|
890 | |
---|
891 | if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) { |
---|
892 | *bt = ffth->tick_time_lerp; |
---|
893 | ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2); |
---|
894 | } else { |
---|
895 | *bt = ffth->tick_time; |
---|
896 | ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2); |
---|
897 | } |
---|
898 | |
---|
899 | if (ffcount > ffth->tick_ffcount) |
---|
900 | bintime_add(bt, &bt2); |
---|
901 | else |
---|
902 | bintime_sub(bt, &bt2); |
---|
903 | } while (gen == 0 || gen != ffth->gen); |
---|
904 | } |
---|
905 | |
---|
906 | /* |
---|
907 | * Difference clock conversion. |
---|
908 | * Low level function to Convert a time interval measured in RAW counter units |
---|
909 | * into bintime. The difference clock allows measuring small intervals much more |
---|
910 | * reliably than the absolute clock. |
---|
911 | */ |
---|
912 | void |
---|
913 | ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt) |
---|
914 | { |
---|
915 | struct fftimehands *ffth; |
---|
916 | uint8_t gen; |
---|
917 | |
---|
918 | /* No locking but check generation has not changed. */ |
---|
919 | do { |
---|
920 | ffth = fftimehands; |
---|
921 | gen = ffth->gen; |
---|
922 | ffclock_convert_delta(ffdelta, ffth->cest.period, bt); |
---|
923 | } while (gen == 0 || gen != ffth->gen); |
---|
924 | } |
---|
925 | |
---|
926 | /* |
---|
927 | * Access to current ffcounter value. |
---|
928 | */ |
---|
929 | void |
---|
930 | ffclock_read_counter(ffcounter *ffcount) |
---|
931 | { |
---|
932 | struct timehands *th; |
---|
933 | struct fftimehands *ffth; |
---|
934 | unsigned int gen, delta; |
---|
935 | |
---|
936 | /* |
---|
937 | * ffclock_windup() called from tc_windup(), safe to rely on |
---|
938 | * th->th_generation only, for correct delta and ffcounter. |
---|
939 | */ |
---|
940 | do { |
---|
941 | th = timehands; |
---|
942 | gen = th->th_generation; |
---|
943 | ffth = fftimehands; |
---|
944 | delta = tc_delta(th); |
---|
945 | *ffcount = ffth->tick_ffcount; |
---|
946 | } while (gen == 0 || gen != th->th_generation); |
---|
947 | |
---|
948 | *ffcount += delta; |
---|
949 | } |
---|
950 | |
---|
951 | void |
---|
952 | binuptime(struct bintime *bt) |
---|
953 | { |
---|
954 | |
---|
955 | binuptime_fromclock(bt, sysclock_active); |
---|
956 | } |
---|
957 | |
---|
958 | void |
---|
959 | nanouptime(struct timespec *tsp) |
---|
960 | { |
---|
961 | |
---|
962 | nanouptime_fromclock(tsp, sysclock_active); |
---|
963 | } |
---|
964 | |
---|
965 | void |
---|
966 | microuptime(struct timeval *tvp) |
---|
967 | { |
---|
968 | |
---|
969 | microuptime_fromclock(tvp, sysclock_active); |
---|
970 | } |
---|
971 | |
---|
972 | void |
---|
973 | bintime(struct bintime *bt) |
---|
974 | { |
---|
975 | |
---|
976 | bintime_fromclock(bt, sysclock_active); |
---|
977 | } |
---|
978 | |
---|
979 | void |
---|
980 | nanotime(struct timespec *tsp) |
---|
981 | { |
---|
982 | |
---|
983 | nanotime_fromclock(tsp, sysclock_active); |
---|
984 | } |
---|
985 | |
---|
986 | void |
---|
987 | microtime(struct timeval *tvp) |
---|
988 | { |
---|
989 | |
---|
990 | microtime_fromclock(tvp, sysclock_active); |
---|
991 | } |
---|
992 | |
---|
993 | void |
---|
994 | getbinuptime(struct bintime *bt) |
---|
995 | { |
---|
996 | |
---|
997 | getbinuptime_fromclock(bt, sysclock_active); |
---|
998 | } |
---|
999 | |
---|
1000 | void |
---|
1001 | getnanouptime(struct timespec *tsp) |
---|
1002 | { |
---|
1003 | |
---|
1004 | getnanouptime_fromclock(tsp, sysclock_active); |
---|
1005 | } |
---|
1006 | |
---|
1007 | void |
---|
1008 | getmicrouptime(struct timeval *tvp) |
---|
1009 | { |
---|
1010 | |
---|
1011 | getmicrouptime_fromclock(tvp, sysclock_active); |
---|
1012 | } |
---|
1013 | |
---|
1014 | void |
---|
1015 | getbintime(struct bintime *bt) |
---|
1016 | { |
---|
1017 | |
---|
1018 | getbintime_fromclock(bt, sysclock_active); |
---|
1019 | } |
---|
1020 | |
---|
1021 | void |
---|
1022 | getnanotime(struct timespec *tsp) |
---|
1023 | { |
---|
1024 | |
---|
1025 | getnanotime_fromclock(tsp, sysclock_active); |
---|
1026 | } |
---|
1027 | |
---|
1028 | void |
---|
1029 | getmicrotime(struct timeval *tvp) |
---|
1030 | { |
---|
1031 | |
---|
1032 | getmicrouptime_fromclock(tvp, sysclock_active); |
---|
1033 | } |
---|
1034 | |
---|
1035 | #endif /* FFCLOCK */ |
---|
1036 | |
---|
1037 | #ifndef __rtems__ |
---|
1038 | /* |
---|
1039 | * This is a clone of getnanotime and used for walltimestamps. |
---|
1040 | * The dtrace_ prefix prevents fbt from creating probes for |
---|
1041 | * it so walltimestamp can be safely used in all fbt probes. |
---|
1042 | */ |
---|
1043 | void |
---|
1044 | dtrace_getnanotime(struct timespec *tsp) |
---|
1045 | { |
---|
1046 | struct timehands *th; |
---|
1047 | uint32_t gen; |
---|
1048 | |
---|
1049 | do { |
---|
1050 | th = timehands; |
---|
1051 | gen = th->th_generation; |
---|
1052 | *tsp = th->th_nanotime; |
---|
1053 | } while (gen == 0 || gen != th->th_generation); |
---|
1054 | } |
---|
1055 | #endif /* __rtems__ */ |
---|
1056 | |
---|
1057 | #ifdef FFCLOCK |
---|
1058 | /* |
---|
1059 | * System clock currently providing time to the system. Modifiable via sysctl |
---|
1060 | * when the FFCLOCK option is defined. |
---|
1061 | */ |
---|
1062 | int sysclock_active = SYSCLOCK_FBCK; |
---|
1063 | #endif |
---|
1064 | |
---|
1065 | /* Internal NTP status and error estimates. */ |
---|
1066 | extern int time_status; |
---|
1067 | extern long time_esterror; |
---|
1068 | |
---|
1069 | #ifndef __rtems__ |
---|
1070 | /* |
---|
1071 | * Take a snapshot of sysclock data which can be used to compare system clocks |
---|
1072 | * and generate timestamps after the fact. |
---|
1073 | */ |
---|
1074 | void |
---|
1075 | sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast) |
---|
1076 | { |
---|
1077 | struct fbclock_info *fbi; |
---|
1078 | struct timehands *th; |
---|
1079 | struct bintime bt; |
---|
1080 | unsigned int delta, gen; |
---|
1081 | #ifdef FFCLOCK |
---|
1082 | ffcounter ffcount; |
---|
1083 | struct fftimehands *ffth; |
---|
1084 | struct ffclock_info *ffi; |
---|
1085 | struct ffclock_estimate cest; |
---|
1086 | |
---|
1087 | ffi = &clock_snap->ff_info; |
---|
1088 | #endif |
---|
1089 | |
---|
1090 | fbi = &clock_snap->fb_info; |
---|
1091 | delta = 0; |
---|
1092 | |
---|
1093 | do { |
---|
1094 | th = timehands; |
---|
1095 | gen = th->th_generation; |
---|
1096 | fbi->th_scale = th->th_scale; |
---|
1097 | fbi->tick_time = th->th_offset; |
---|
1098 | #ifdef FFCLOCK |
---|
1099 | ffth = fftimehands; |
---|
1100 | ffi->tick_time = ffth->tick_time_lerp; |
---|
1101 | ffi->tick_time_lerp = ffth->tick_time_lerp; |
---|
1102 | ffi->period = ffth->cest.period; |
---|
1103 | ffi->period_lerp = ffth->period_lerp; |
---|
1104 | clock_snap->ffcount = ffth->tick_ffcount; |
---|
1105 | cest = ffth->cest; |
---|
1106 | #endif |
---|
1107 | if (!fast) |
---|
1108 | delta = tc_delta(th); |
---|
1109 | } while (gen == 0 || gen != th->th_generation); |
---|
1110 | |
---|
1111 | clock_snap->delta = delta; |
---|
1112 | #ifdef FFCLOCK |
---|
1113 | clock_snap->sysclock_active = sysclock_active; |
---|
1114 | #endif |
---|
1115 | |
---|
1116 | /* Record feedback clock status and error. */ |
---|
1117 | clock_snap->fb_info.status = time_status; |
---|
1118 | /* XXX: Very crude estimate of feedback clock error. */ |
---|
1119 | bt.sec = time_esterror / 1000000; |
---|
1120 | bt.frac = ((time_esterror - bt.sec) * 1000000) * |
---|
1121 | (uint64_t)18446744073709ULL; |
---|
1122 | clock_snap->fb_info.error = bt; |
---|
1123 | |
---|
1124 | #ifdef FFCLOCK |
---|
1125 | if (!fast) |
---|
1126 | clock_snap->ffcount += delta; |
---|
1127 | |
---|
1128 | /* Record feed-forward clock leap second adjustment. */ |
---|
1129 | ffi->leapsec_adjustment = cest.leapsec_total; |
---|
1130 | if (clock_snap->ffcount > cest.leapsec_next) |
---|
1131 | ffi->leapsec_adjustment -= cest.leapsec; |
---|
1132 | |
---|
1133 | /* Record feed-forward clock status and error. */ |
---|
1134 | clock_snap->ff_info.status = cest.status; |
---|
1135 | ffcount = clock_snap->ffcount - cest.update_ffcount; |
---|
1136 | ffclock_convert_delta(ffcount, cest.period, &bt); |
---|
1137 | /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */ |
---|
1138 | bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL); |
---|
1139 | /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */ |
---|
1140 | bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL); |
---|
1141 | clock_snap->ff_info.error = bt; |
---|
1142 | #endif |
---|
1143 | } |
---|
1144 | |
---|
1145 | /* |
---|
1146 | * Convert a sysclock snapshot into a struct bintime based on the specified |
---|
1147 | * clock source and flags. |
---|
1148 | */ |
---|
1149 | int |
---|
1150 | sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt, |
---|
1151 | int whichclock, uint32_t flags) |
---|
1152 | { |
---|
1153 | #ifdef FFCLOCK |
---|
1154 | struct bintime bt2; |
---|
1155 | uint64_t period; |
---|
1156 | #endif |
---|
1157 | |
---|
1158 | switch (whichclock) { |
---|
1159 | case SYSCLOCK_FBCK: |
---|
1160 | *bt = cs->fb_info.tick_time; |
---|
1161 | |
---|
1162 | /* If snapshot was created with !fast, delta will be >0. */ |
---|
1163 | if (cs->delta > 0) |
---|
1164 | bintime_addx(bt, cs->fb_info.th_scale * cs->delta); |
---|
1165 | |
---|
1166 | if ((flags & FBCLOCK_UPTIME) == 0) |
---|
1167 | bintime_add(bt, &boottimebin); |
---|
1168 | break; |
---|
1169 | #ifdef FFCLOCK |
---|
1170 | case SYSCLOCK_FFWD: |
---|
1171 | if (flags & FFCLOCK_LERP) { |
---|
1172 | *bt = cs->ff_info.tick_time_lerp; |
---|
1173 | period = cs->ff_info.period_lerp; |
---|
1174 | } else { |
---|
1175 | *bt = cs->ff_info.tick_time; |
---|
1176 | period = cs->ff_info.period; |
---|
1177 | } |
---|
1178 | |
---|
1179 | /* If snapshot was created with !fast, delta will be >0. */ |
---|
1180 | if (cs->delta > 0) { |
---|
1181 | ffclock_convert_delta(cs->delta, period, &bt2); |
---|
1182 | bintime_add(bt, &bt2); |
---|
1183 | } |
---|
1184 | |
---|
1185 | /* Leap second adjustment. */ |
---|
1186 | if (flags & FFCLOCK_LEAPSEC) |
---|
1187 | bt->sec -= cs->ff_info.leapsec_adjustment; |
---|
1188 | |
---|
1189 | /* Boot time adjustment, for uptime/monotonic clocks. */ |
---|
1190 | if (flags & FFCLOCK_UPTIME) |
---|
1191 | bintime_sub(bt, &ffclock_boottime); |
---|
1192 | break; |
---|
1193 | #endif |
---|
1194 | default: |
---|
1195 | return (EINVAL); |
---|
1196 | break; |
---|
1197 | } |
---|
1198 | |
---|
1199 | return (0); |
---|
1200 | } |
---|
1201 | #endif /* __rtems__ */ |
---|
1202 | |
---|
1203 | /* |
---|
1204 | * Initialize a new timecounter and possibly use it. |
---|
1205 | */ |
---|
1206 | void |
---|
1207 | tc_init(struct timecounter *tc) |
---|
1208 | { |
---|
1209 | #ifndef __rtems__ |
---|
1210 | uint32_t u; |
---|
1211 | struct sysctl_oid *tc_root; |
---|
1212 | |
---|
1213 | u = tc->tc_frequency / tc->tc_counter_mask; |
---|
1214 | /* XXX: We need some margin here, 10% is a guess */ |
---|
1215 | u *= 11; |
---|
1216 | u /= 10; |
---|
1217 | if (u > hz && tc->tc_quality >= 0) { |
---|
1218 | tc->tc_quality = -2000; |
---|
1219 | if (bootverbose) { |
---|
1220 | printf("Timecounter \"%s\" frequency %ju Hz", |
---|
1221 | tc->tc_name, (uintmax_t)tc->tc_frequency); |
---|
1222 | printf(" -- Insufficient hz, needs at least %u\n", u); |
---|
1223 | } |
---|
1224 | } else if (tc->tc_quality >= 0 || bootverbose) { |
---|
1225 | printf("Timecounter \"%s\" frequency %ju Hz quality %d\n", |
---|
1226 | tc->tc_name, (uintmax_t)tc->tc_frequency, |
---|
1227 | tc->tc_quality); |
---|
1228 | } |
---|
1229 | #endif /* __rtems__ */ |
---|
1230 | |
---|
1231 | tc->tc_next = timecounters; |
---|
1232 | timecounters = tc; |
---|
1233 | #ifndef __rtems__ |
---|
1234 | /* |
---|
1235 | * Set up sysctl tree for this counter. |
---|
1236 | */ |
---|
1237 | tc_root = SYSCTL_ADD_NODE(NULL, |
---|
1238 | SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name, |
---|
1239 | CTLFLAG_RW, 0, "timecounter description"); |
---|
1240 | SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO, |
---|
1241 | "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0, |
---|
1242 | "mask for implemented bits"); |
---|
1243 | SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO, |
---|
1244 | "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc), |
---|
1245 | sysctl_kern_timecounter_get, "IU", "current timecounter value"); |
---|
1246 | SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO, |
---|
1247 | "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc), |
---|
1248 | sysctl_kern_timecounter_freq, "QU", "timecounter frequency"); |
---|
1249 | SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO, |
---|
1250 | "quality", CTLFLAG_RD, &(tc->tc_quality), 0, |
---|
1251 | "goodness of time counter"); |
---|
1252 | /* |
---|
1253 | * Never automatically use a timecounter with negative quality. |
---|
1254 | * Even though we run on the dummy counter, switching here may be |
---|
1255 | * worse since this timecounter may not be monotonous. |
---|
1256 | */ |
---|
1257 | if (tc->tc_quality < 0) |
---|
1258 | return; |
---|
1259 | if (tc->tc_quality < timecounter->tc_quality) |
---|
1260 | return; |
---|
1261 | if (tc->tc_quality == timecounter->tc_quality && |
---|
1262 | tc->tc_frequency < timecounter->tc_frequency) |
---|
1263 | return; |
---|
1264 | #endif /* __rtems__ */ |
---|
1265 | (void)tc->tc_get_timecount(tc); |
---|
1266 | (void)tc->tc_get_timecount(tc); |
---|
1267 | timecounter = tc; |
---|
1268 | #ifdef __rtems__ |
---|
1269 | tc_windup(); |
---|
1270 | #endif /* __rtems__ */ |
---|
1271 | } |
---|
1272 | |
---|
1273 | #ifndef __rtems__ |
---|
1274 | /* Report the frequency of the current timecounter. */ |
---|
1275 | uint64_t |
---|
1276 | tc_getfrequency(void) |
---|
1277 | { |
---|
1278 | |
---|
1279 | return (timehands->th_counter->tc_frequency); |
---|
1280 | } |
---|
1281 | #endif /* __rtems__ */ |
---|
1282 | |
---|
1283 | /* |
---|
1284 | * Step our concept of UTC. This is done by modifying our estimate of |
---|
1285 | * when we booted. |
---|
1286 | * XXX: not locked. |
---|
1287 | */ |
---|
1288 | #ifndef __rtems__ |
---|
1289 | void |
---|
1290 | tc_setclock(struct timespec *ts) |
---|
1291 | #else /* __rtems__ */ |
---|
1292 | void |
---|
1293 | _Timecounter_Set_clock(const struct timespec *ts) |
---|
1294 | #endif /* __rtems__ */ |
---|
1295 | { |
---|
1296 | #ifndef __rtems__ |
---|
1297 | struct timespec tbef, taft; |
---|
1298 | #endif /* __rtems__ */ |
---|
1299 | struct bintime bt, bt2; |
---|
1300 | |
---|
1301 | #ifndef __rtems__ |
---|
1302 | cpu_tick_calibrate(1); |
---|
1303 | nanotime(&tbef); |
---|
1304 | #endif /* __rtems__ */ |
---|
1305 | timespec2bintime(ts, &bt); |
---|
1306 | binuptime(&bt2); |
---|
1307 | bintime_sub(&bt, &bt2); |
---|
1308 | bintime_add(&bt2, &boottimebin); |
---|
1309 | boottimebin = bt; |
---|
1310 | #ifndef __rtems__ |
---|
1311 | bintime2timeval(&bt, &boottime); |
---|
1312 | #endif /* __rtems__ */ |
---|
1313 | |
---|
1314 | /* XXX fiddle all the little crinkly bits around the fiords... */ |
---|
1315 | tc_windup(); |
---|
1316 | #ifndef __rtems__ |
---|
1317 | nanotime(&taft); |
---|
1318 | if (timestepwarnings) { |
---|
1319 | log(LOG_INFO, |
---|
1320 | "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n", |
---|
1321 | (intmax_t)tbef.tv_sec, tbef.tv_nsec, |
---|
1322 | (intmax_t)taft.tv_sec, taft.tv_nsec, |
---|
1323 | (intmax_t)ts->tv_sec, ts->tv_nsec); |
---|
1324 | } |
---|
1325 | cpu_tick_calibrate(1); |
---|
1326 | #endif /* __rtems__ */ |
---|
1327 | } |
---|
1328 | |
---|
1329 | /* |
---|
1330 | * Initialize the next struct timehands in the ring and make |
---|
1331 | * it the active timehands. Along the way we might switch to a different |
---|
1332 | * timecounter and/or do seconds processing in NTP. Slightly magic. |
---|
1333 | */ |
---|
1334 | static void |
---|
1335 | tc_windup(void) |
---|
1336 | { |
---|
1337 | struct bintime bt; |
---|
1338 | struct timehands *th, *tho; |
---|
1339 | uint64_t scale; |
---|
1340 | uint32_t delta, ncount, ogen; |
---|
1341 | int i; |
---|
1342 | time_t t; |
---|
1343 | #ifdef __rtems__ |
---|
1344 | ISR_lock_Context lock_context; |
---|
1345 | |
---|
1346 | _ISR_lock_ISR_disable_and_acquire(&_Timecounter_Lock, &lock_context); |
---|
1347 | #endif /* __rtems__ */ |
---|
1348 | |
---|
1349 | /* |
---|
1350 | * Make the next timehands a copy of the current one, but do not |
---|
1351 | * overwrite the generation or next pointer. While we update |
---|
1352 | * the contents, the generation must be zero. |
---|
1353 | */ |
---|
1354 | tho = timehands; |
---|
1355 | th = tho->th_next; |
---|
1356 | ogen = th->th_generation; |
---|
1357 | th->th_generation = 0; |
---|
1358 | bcopy(tho, th, offsetof(struct timehands, th_generation)); |
---|
1359 | |
---|
1360 | /* |
---|
1361 | * Capture a timecounter delta on the current timecounter and if |
---|
1362 | * changing timecounters, a counter value from the new timecounter. |
---|
1363 | * Update the offset fields accordingly. |
---|
1364 | */ |
---|
1365 | delta = tc_delta(th); |
---|
1366 | if (th->th_counter != timecounter) |
---|
1367 | ncount = timecounter->tc_get_timecount(timecounter); |
---|
1368 | else |
---|
1369 | ncount = 0; |
---|
1370 | #ifdef FFCLOCK |
---|
1371 | ffclock_windup(delta); |
---|
1372 | #endif |
---|
1373 | th->th_offset_count += delta; |
---|
1374 | th->th_offset_count &= th->th_counter->tc_counter_mask; |
---|
1375 | while (delta > th->th_counter->tc_frequency) { |
---|
1376 | /* Eat complete unadjusted seconds. */ |
---|
1377 | delta -= th->th_counter->tc_frequency; |
---|
1378 | th->th_offset.sec++; |
---|
1379 | } |
---|
1380 | if ((delta > th->th_counter->tc_frequency / 2) && |
---|
1381 | (th->th_scale * delta < ((uint64_t)1 << 63))) { |
---|
1382 | /* The product th_scale * delta just barely overflows. */ |
---|
1383 | th->th_offset.sec++; |
---|
1384 | } |
---|
1385 | bintime_addx(&th->th_offset, th->th_scale * delta); |
---|
1386 | |
---|
1387 | /* |
---|
1388 | * Hardware latching timecounters may not generate interrupts on |
---|
1389 | * PPS events, so instead we poll them. There is a finite risk that |
---|
1390 | * the hardware might capture a count which is later than the one we |
---|
1391 | * got above, and therefore possibly in the next NTP second which might |
---|
1392 | * have a different rate than the current NTP second. It doesn't |
---|
1393 | * matter in practice. |
---|
1394 | */ |
---|
1395 | if (tho->th_counter->tc_poll_pps) |
---|
1396 | tho->th_counter->tc_poll_pps(tho->th_counter); |
---|
1397 | |
---|
1398 | /* |
---|
1399 | * Deal with NTP second processing. The for loop normally |
---|
1400 | * iterates at most once, but in extreme situations it might |
---|
1401 | * keep NTP sane if timeouts are not run for several seconds. |
---|
1402 | * At boot, the time step can be large when the TOD hardware |
---|
1403 | * has been read, so on really large steps, we call |
---|
1404 | * ntp_update_second only twice. We need to call it twice in |
---|
1405 | * case we missed a leap second. |
---|
1406 | */ |
---|
1407 | bt = th->th_offset; |
---|
1408 | bintime_add(&bt, &boottimebin); |
---|
1409 | i = bt.sec - tho->th_microtime.tv_sec; |
---|
1410 | if (i > LARGE_STEP) |
---|
1411 | i = 2; |
---|
1412 | for (; i > 0; i--) { |
---|
1413 | t = bt.sec; |
---|
1414 | ntp_update_second(&th->th_adjustment, &bt.sec); |
---|
1415 | if (bt.sec != t) |
---|
1416 | boottimebin.sec += bt.sec - t; |
---|
1417 | } |
---|
1418 | /* Update the UTC timestamps used by the get*() functions. */ |
---|
1419 | /* XXX shouldn't do this here. Should force non-`get' versions. */ |
---|
1420 | bintime2timeval(&bt, &th->th_microtime); |
---|
1421 | bintime2timespec(&bt, &th->th_nanotime); |
---|
1422 | |
---|
1423 | /* Now is a good time to change timecounters. */ |
---|
1424 | if (th->th_counter != timecounter) { |
---|
1425 | #ifndef __rtems__ |
---|
1426 | #ifndef __arm__ |
---|
1427 | if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0) |
---|
1428 | cpu_disable_c2_sleep++; |
---|
1429 | if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0) |
---|
1430 | cpu_disable_c2_sleep--; |
---|
1431 | #endif |
---|
1432 | #endif /* __rtems__ */ |
---|
1433 | th->th_counter = timecounter; |
---|
1434 | th->th_offset_count = ncount; |
---|
1435 | #ifndef __rtems__ |
---|
1436 | tc_min_ticktock_freq = max(1, timecounter->tc_frequency / |
---|
1437 | (((uint64_t)timecounter->tc_counter_mask + 1) / 3)); |
---|
1438 | #endif /* __rtems__ */ |
---|
1439 | #ifdef FFCLOCK |
---|
1440 | ffclock_change_tc(th); |
---|
1441 | #endif |
---|
1442 | } |
---|
1443 | |
---|
1444 | /*- |
---|
1445 | * Recalculate the scaling factor. We want the number of 1/2^64 |
---|
1446 | * fractions of a second per period of the hardware counter, taking |
---|
1447 | * into account the th_adjustment factor which the NTP PLL/adjtime(2) |
---|
1448 | * processing provides us with. |
---|
1449 | * |
---|
1450 | * The th_adjustment is nanoseconds per second with 32 bit binary |
---|
1451 | * fraction and we want 64 bit binary fraction of second: |
---|
1452 | * |
---|
1453 | * x = a * 2^32 / 10^9 = a * 4.294967296 |
---|
1454 | * |
---|
1455 | * The range of th_adjustment is +/- 5000PPM so inside a 64bit int |
---|
1456 | * we can only multiply by about 850 without overflowing, that |
---|
1457 | * leaves no suitably precise fractions for multiply before divide. |
---|
1458 | * |
---|
1459 | * Divide before multiply with a fraction of 2199/512 results in a |
---|
1460 | * systematic undercompensation of 10PPM of th_adjustment. On a |
---|
1461 | * 5000PPM adjustment this is a 0.05PPM error. This is acceptable. |
---|
1462 | * |
---|
1463 | * We happily sacrifice the lowest of the 64 bits of our result |
---|
1464 | * to the goddess of code clarity. |
---|
1465 | * |
---|
1466 | */ |
---|
1467 | scale = (uint64_t)1 << 63; |
---|
1468 | scale += (th->th_adjustment / 1024) * 2199; |
---|
1469 | scale /= th->th_counter->tc_frequency; |
---|
1470 | th->th_scale = scale * 2; |
---|
1471 | |
---|
1472 | /* |
---|
1473 | * Now that the struct timehands is again consistent, set the new |
---|
1474 | * generation number, making sure to not make it zero. |
---|
1475 | */ |
---|
1476 | if (++ogen == 0) |
---|
1477 | ogen = 1; |
---|
1478 | th->th_generation = ogen; |
---|
1479 | |
---|
1480 | /* Go live with the new struct timehands. */ |
---|
1481 | #ifdef FFCLOCK |
---|
1482 | switch (sysclock_active) { |
---|
1483 | case SYSCLOCK_FBCK: |
---|
1484 | #endif |
---|
1485 | time_second = th->th_microtime.tv_sec; |
---|
1486 | time_uptime = th->th_offset.sec; |
---|
1487 | #ifdef FFCLOCK |
---|
1488 | break; |
---|
1489 | case SYSCLOCK_FFWD: |
---|
1490 | time_second = fftimehands->tick_time_lerp.sec; |
---|
1491 | time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec; |
---|
1492 | break; |
---|
1493 | } |
---|
1494 | #endif |
---|
1495 | |
---|
1496 | timehands = th; |
---|
1497 | #ifndef __rtems__ |
---|
1498 | timekeep_push_vdso(); |
---|
1499 | #endif /* __rtems__ */ |
---|
1500 | #ifdef __rtems__ |
---|
1501 | _ISR_lock_Release_and_ISR_enable(&_Timecounter_Lock, &lock_context); |
---|
1502 | #endif /* __rtems__ */ |
---|
1503 | } |
---|
1504 | |
---|
1505 | #ifndef __rtems__ |
---|
1506 | /* Report or change the active timecounter hardware. */ |
---|
1507 | static int |
---|
1508 | sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS) |
---|
1509 | { |
---|
1510 | char newname[32]; |
---|
1511 | struct timecounter *newtc, *tc; |
---|
1512 | int error; |
---|
1513 | |
---|
1514 | tc = timecounter; |
---|
1515 | strlcpy(newname, tc->tc_name, sizeof(newname)); |
---|
1516 | |
---|
1517 | error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req); |
---|
1518 | if (error != 0 || req->newptr == NULL || |
---|
1519 | strcmp(newname, tc->tc_name) == 0) |
---|
1520 | return (error); |
---|
1521 | for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) { |
---|
1522 | if (strcmp(newname, newtc->tc_name) != 0) |
---|
1523 | continue; |
---|
1524 | |
---|
1525 | /* Warm up new timecounter. */ |
---|
1526 | (void)newtc->tc_get_timecount(newtc); |
---|
1527 | (void)newtc->tc_get_timecount(newtc); |
---|
1528 | |
---|
1529 | timecounter = newtc; |
---|
1530 | |
---|
1531 | /* |
---|
1532 | * The vdso timehands update is deferred until the next |
---|
1533 | * 'tc_windup()'. |
---|
1534 | * |
---|
1535 | * This is prudent given that 'timekeep_push_vdso()' does not |
---|
1536 | * use any locking and that it can be called in hard interrupt |
---|
1537 | * context via 'tc_windup()'. |
---|
1538 | */ |
---|
1539 | return (0); |
---|
1540 | } |
---|
1541 | return (EINVAL); |
---|
1542 | } |
---|
1543 | |
---|
1544 | SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW, |
---|
1545 | 0, 0, sysctl_kern_timecounter_hardware, "A", |
---|
1546 | "Timecounter hardware selected"); |
---|
1547 | |
---|
1548 | |
---|
1549 | /* Report or change the active timecounter hardware. */ |
---|
1550 | static int |
---|
1551 | sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS) |
---|
1552 | { |
---|
1553 | struct sbuf sb; |
---|
1554 | struct timecounter *tc; |
---|
1555 | int error; |
---|
1556 | |
---|
1557 | sbuf_new_for_sysctl(&sb, NULL, 0, req); |
---|
1558 | for (tc = timecounters; tc != NULL; tc = tc->tc_next) { |
---|
1559 | if (tc != timecounters) |
---|
1560 | sbuf_putc(&sb, ' '); |
---|
1561 | sbuf_printf(&sb, "%s(%d)", tc->tc_name, tc->tc_quality); |
---|
1562 | } |
---|
1563 | error = sbuf_finish(&sb); |
---|
1564 | sbuf_delete(&sb); |
---|
1565 | return (error); |
---|
1566 | } |
---|
1567 | |
---|
1568 | SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD, |
---|
1569 | 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected"); |
---|
1570 | #endif /* __rtems__ */ |
---|
1571 | |
---|
1572 | #ifndef __rtems__ |
---|
1573 | /* |
---|
1574 | * RFC 2783 PPS-API implementation. |
---|
1575 | */ |
---|
1576 | |
---|
1577 | /* |
---|
1578 | * Return true if the driver is aware of the abi version extensions in the |
---|
1579 | * pps_state structure, and it supports at least the given abi version number. |
---|
1580 | */ |
---|
1581 | static inline int |
---|
1582 | abi_aware(struct pps_state *pps, int vers) |
---|
1583 | { |
---|
1584 | |
---|
1585 | return ((pps->kcmode & KCMODE_ABIFLAG) && pps->driver_abi >= vers); |
---|
1586 | } |
---|
1587 | |
---|
1588 | static int |
---|
1589 | pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps) |
---|
1590 | { |
---|
1591 | int err, timo; |
---|
1592 | pps_seq_t aseq, cseq; |
---|
1593 | struct timeval tv; |
---|
1594 | |
---|
1595 | if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC) |
---|
1596 | return (EINVAL); |
---|
1597 | |
---|
1598 | /* |
---|
1599 | * If no timeout is requested, immediately return whatever values were |
---|
1600 | * most recently captured. If timeout seconds is -1, that's a request |
---|
1601 | * to block without a timeout. WITNESS won't let us sleep forever |
---|
1602 | * without a lock (we really don't need a lock), so just repeatedly |
---|
1603 | * sleep a long time. |
---|
1604 | */ |
---|
1605 | if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) { |
---|
1606 | if (fapi->timeout.tv_sec == -1) |
---|
1607 | timo = 0x7fffffff; |
---|
1608 | else { |
---|
1609 | tv.tv_sec = fapi->timeout.tv_sec; |
---|
1610 | tv.tv_usec = fapi->timeout.tv_nsec / 1000; |
---|
1611 | timo = tvtohz(&tv); |
---|
1612 | } |
---|
1613 | aseq = pps->ppsinfo.assert_sequence; |
---|
1614 | cseq = pps->ppsinfo.clear_sequence; |
---|
1615 | while (aseq == pps->ppsinfo.assert_sequence && |
---|
1616 | cseq == pps->ppsinfo.clear_sequence) { |
---|
1617 | if (abi_aware(pps, 1) && pps->driver_mtx != NULL) { |
---|
1618 | if (pps->flags & PPSFLAG_MTX_SPIN) { |
---|
1619 | err = msleep_spin(pps, pps->driver_mtx, |
---|
1620 | "ppsfch", timo); |
---|
1621 | } else { |
---|
1622 | err = msleep(pps, pps->driver_mtx, PCATCH, |
---|
1623 | "ppsfch", timo); |
---|
1624 | } |
---|
1625 | } else { |
---|
1626 | err = tsleep(pps, PCATCH, "ppsfch", timo); |
---|
1627 | } |
---|
1628 | if (err == EWOULDBLOCK && fapi->timeout.tv_sec == -1) { |
---|
1629 | continue; |
---|
1630 | } else if (err != 0) { |
---|
1631 | return (err); |
---|
1632 | } |
---|
1633 | } |
---|
1634 | } |
---|
1635 | |
---|
1636 | pps->ppsinfo.current_mode = pps->ppsparam.mode; |
---|
1637 | fapi->pps_info_buf = pps->ppsinfo; |
---|
1638 | |
---|
1639 | return (0); |
---|
1640 | } |
---|
1641 | |
---|
1642 | int |
---|
1643 | pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps) |
---|
1644 | { |
---|
1645 | pps_params_t *app; |
---|
1646 | struct pps_fetch_args *fapi; |
---|
1647 | #ifdef FFCLOCK |
---|
1648 | struct pps_fetch_ffc_args *fapi_ffc; |
---|
1649 | #endif |
---|
1650 | #ifdef PPS_SYNC |
---|
1651 | struct pps_kcbind_args *kapi; |
---|
1652 | #endif |
---|
1653 | |
---|
1654 | KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl")); |
---|
1655 | switch (cmd) { |
---|
1656 | case PPS_IOC_CREATE: |
---|
1657 | return (0); |
---|
1658 | case PPS_IOC_DESTROY: |
---|
1659 | return (0); |
---|
1660 | case PPS_IOC_SETPARAMS: |
---|
1661 | app = (pps_params_t *)data; |
---|
1662 | if (app->mode & ~pps->ppscap) |
---|
1663 | return (EINVAL); |
---|
1664 | #ifdef FFCLOCK |
---|
1665 | /* Ensure only a single clock is selected for ffc timestamp. */ |
---|
1666 | if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK) |
---|
1667 | return (EINVAL); |
---|
1668 | #endif |
---|
1669 | pps->ppsparam = *app; |
---|
1670 | return (0); |
---|
1671 | case PPS_IOC_GETPARAMS: |
---|
1672 | app = (pps_params_t *)data; |
---|
1673 | *app = pps->ppsparam; |
---|
1674 | app->api_version = PPS_API_VERS_1; |
---|
1675 | return (0); |
---|
1676 | case PPS_IOC_GETCAP: |
---|
1677 | *(int*)data = pps->ppscap; |
---|
1678 | return (0); |
---|
1679 | case PPS_IOC_FETCH: |
---|
1680 | fapi = (struct pps_fetch_args *)data; |
---|
1681 | return (pps_fetch(fapi, pps)); |
---|
1682 | #ifdef FFCLOCK |
---|
1683 | case PPS_IOC_FETCH_FFCOUNTER: |
---|
1684 | fapi_ffc = (struct pps_fetch_ffc_args *)data; |
---|
1685 | if (fapi_ffc->tsformat && fapi_ffc->tsformat != |
---|
1686 | PPS_TSFMT_TSPEC) |
---|
1687 | return (EINVAL); |
---|
1688 | if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec) |
---|
1689 | return (EOPNOTSUPP); |
---|
1690 | pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode; |
---|
1691 | fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc; |
---|
1692 | /* Overwrite timestamps if feedback clock selected. */ |
---|
1693 | switch (pps->ppsparam.mode & PPS_TSCLK_MASK) { |
---|
1694 | case PPS_TSCLK_FBCK: |
---|
1695 | fapi_ffc->pps_info_buf_ffc.assert_timestamp = |
---|
1696 | pps->ppsinfo.assert_timestamp; |
---|
1697 | fapi_ffc->pps_info_buf_ffc.clear_timestamp = |
---|
1698 | pps->ppsinfo.clear_timestamp; |
---|
1699 | break; |
---|
1700 | case PPS_TSCLK_FFWD: |
---|
1701 | break; |
---|
1702 | default: |
---|
1703 | break; |
---|
1704 | } |
---|
1705 | return (0); |
---|
1706 | #endif /* FFCLOCK */ |
---|
1707 | case PPS_IOC_KCBIND: |
---|
1708 | #ifdef PPS_SYNC |
---|
1709 | kapi = (struct pps_kcbind_args *)data; |
---|
1710 | /* XXX Only root should be able to do this */ |
---|
1711 | if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC) |
---|
1712 | return (EINVAL); |
---|
1713 | if (kapi->kernel_consumer != PPS_KC_HARDPPS) |
---|
1714 | return (EINVAL); |
---|
1715 | if (kapi->edge & ~pps->ppscap) |
---|
1716 | return (EINVAL); |
---|
1717 | pps->kcmode = (kapi->edge & KCMODE_EDGEMASK) | |
---|
1718 | (pps->kcmode & KCMODE_ABIFLAG); |
---|
1719 | return (0); |
---|
1720 | #else |
---|
1721 | return (EOPNOTSUPP); |
---|
1722 | #endif |
---|
1723 | default: |
---|
1724 | return (ENOIOCTL); |
---|
1725 | } |
---|
1726 | } |
---|
1727 | |
---|
1728 | void |
---|
1729 | pps_init(struct pps_state *pps) |
---|
1730 | { |
---|
1731 | pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT; |
---|
1732 | if (pps->ppscap & PPS_CAPTUREASSERT) |
---|
1733 | pps->ppscap |= PPS_OFFSETASSERT; |
---|
1734 | if (pps->ppscap & PPS_CAPTURECLEAR) |
---|
1735 | pps->ppscap |= PPS_OFFSETCLEAR; |
---|
1736 | #ifdef FFCLOCK |
---|
1737 | pps->ppscap |= PPS_TSCLK_MASK; |
---|
1738 | #endif |
---|
1739 | pps->kcmode &= ~KCMODE_ABIFLAG; |
---|
1740 | } |
---|
1741 | |
---|
1742 | void |
---|
1743 | pps_init_abi(struct pps_state *pps) |
---|
1744 | { |
---|
1745 | |
---|
1746 | pps_init(pps); |
---|
1747 | if (pps->driver_abi > 0) { |
---|
1748 | pps->kcmode |= KCMODE_ABIFLAG; |
---|
1749 | pps->kernel_abi = PPS_ABI_VERSION; |
---|
1750 | } |
---|
1751 | } |
---|
1752 | |
---|
1753 | void |
---|
1754 | pps_capture(struct pps_state *pps) |
---|
1755 | { |
---|
1756 | struct timehands *th; |
---|
1757 | |
---|
1758 | KASSERT(pps != NULL, ("NULL pps pointer in pps_capture")); |
---|
1759 | th = timehands; |
---|
1760 | pps->capgen = th->th_generation; |
---|
1761 | pps->capth = th; |
---|
1762 | #ifdef FFCLOCK |
---|
1763 | pps->capffth = fftimehands; |
---|
1764 | #endif |
---|
1765 | pps->capcount = th->th_counter->tc_get_timecount(th->th_counter); |
---|
1766 | if (pps->capgen != th->th_generation) |
---|
1767 | pps->capgen = 0; |
---|
1768 | } |
---|
1769 | |
---|
1770 | void |
---|
1771 | pps_event(struct pps_state *pps, int event) |
---|
1772 | { |
---|
1773 | struct bintime bt; |
---|
1774 | struct timespec ts, *tsp, *osp; |
---|
1775 | uint32_t tcount, *pcount; |
---|
1776 | int foff, fhard; |
---|
1777 | pps_seq_t *pseq; |
---|
1778 | #ifdef FFCLOCK |
---|
1779 | struct timespec *tsp_ffc; |
---|
1780 | pps_seq_t *pseq_ffc; |
---|
1781 | ffcounter *ffcount; |
---|
1782 | #endif |
---|
1783 | |
---|
1784 | KASSERT(pps != NULL, ("NULL pps pointer in pps_event")); |
---|
1785 | /* If the timecounter was wound up underneath us, bail out. */ |
---|
1786 | if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation) |
---|
1787 | return; |
---|
1788 | |
---|
1789 | /* Things would be easier with arrays. */ |
---|
1790 | if (event == PPS_CAPTUREASSERT) { |
---|
1791 | tsp = &pps->ppsinfo.assert_timestamp; |
---|
1792 | osp = &pps->ppsparam.assert_offset; |
---|
1793 | foff = pps->ppsparam.mode & PPS_OFFSETASSERT; |
---|
1794 | fhard = pps->kcmode & PPS_CAPTUREASSERT; |
---|
1795 | pcount = &pps->ppscount[0]; |
---|
1796 | pseq = &pps->ppsinfo.assert_sequence; |
---|
1797 | #ifdef FFCLOCK |
---|
1798 | ffcount = &pps->ppsinfo_ffc.assert_ffcount; |
---|
1799 | tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp; |
---|
1800 | pseq_ffc = &pps->ppsinfo_ffc.assert_sequence; |
---|
1801 | #endif |
---|
1802 | } else { |
---|
1803 | tsp = &pps->ppsinfo.clear_timestamp; |
---|
1804 | osp = &pps->ppsparam.clear_offset; |
---|
1805 | foff = pps->ppsparam.mode & PPS_OFFSETCLEAR; |
---|
1806 | fhard = pps->kcmode & PPS_CAPTURECLEAR; |
---|
1807 | pcount = &pps->ppscount[1]; |
---|
1808 | pseq = &pps->ppsinfo.clear_sequence; |
---|
1809 | #ifdef FFCLOCK |
---|
1810 | ffcount = &pps->ppsinfo_ffc.clear_ffcount; |
---|
1811 | tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp; |
---|
1812 | pseq_ffc = &pps->ppsinfo_ffc.clear_sequence; |
---|
1813 | #endif |
---|
1814 | } |
---|
1815 | |
---|
1816 | /* |
---|
1817 | * If the timecounter changed, we cannot compare the count values, so |
---|
1818 | * we have to drop the rest of the PPS-stuff until the next event. |
---|
1819 | */ |
---|
1820 | if (pps->ppstc != pps->capth->th_counter) { |
---|
1821 | pps->ppstc = pps->capth->th_counter; |
---|
1822 | *pcount = pps->capcount; |
---|
1823 | pps->ppscount[2] = pps->capcount; |
---|
1824 | return; |
---|
1825 | } |
---|
1826 | |
---|
1827 | /* Convert the count to a timespec. */ |
---|
1828 | tcount = pps->capcount - pps->capth->th_offset_count; |
---|
1829 | tcount &= pps->capth->th_counter->tc_counter_mask; |
---|
1830 | bt = pps->capth->th_offset; |
---|
1831 | bintime_addx(&bt, pps->capth->th_scale * tcount); |
---|
1832 | bintime_add(&bt, &boottimebin); |
---|
1833 | bintime2timespec(&bt, &ts); |
---|
1834 | |
---|
1835 | /* If the timecounter was wound up underneath us, bail out. */ |
---|
1836 | if (pps->capgen != pps->capth->th_generation) |
---|
1837 | return; |
---|
1838 | |
---|
1839 | *pcount = pps->capcount; |
---|
1840 | (*pseq)++; |
---|
1841 | *tsp = ts; |
---|
1842 | |
---|
1843 | if (foff) { |
---|
1844 | timespecadd(tsp, osp); |
---|
1845 | if (tsp->tv_nsec < 0) { |
---|
1846 | tsp->tv_nsec += 1000000000; |
---|
1847 | tsp->tv_sec -= 1; |
---|
1848 | } |
---|
1849 | } |
---|
1850 | |
---|
1851 | #ifdef FFCLOCK |
---|
1852 | *ffcount = pps->capffth->tick_ffcount + tcount; |
---|
1853 | bt = pps->capffth->tick_time; |
---|
1854 | ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt); |
---|
1855 | bintime_add(&bt, &pps->capffth->tick_time); |
---|
1856 | bintime2timespec(&bt, &ts); |
---|
1857 | (*pseq_ffc)++; |
---|
1858 | *tsp_ffc = ts; |
---|
1859 | #endif |
---|
1860 | |
---|
1861 | #ifdef PPS_SYNC |
---|
1862 | if (fhard) { |
---|
1863 | uint64_t scale; |
---|
1864 | |
---|
1865 | /* |
---|
1866 | * Feed the NTP PLL/FLL. |
---|
1867 | * The FLL wants to know how many (hardware) nanoseconds |
---|
1868 | * elapsed since the previous event. |
---|
1869 | */ |
---|
1870 | tcount = pps->capcount - pps->ppscount[2]; |
---|
1871 | pps->ppscount[2] = pps->capcount; |
---|
1872 | tcount &= pps->capth->th_counter->tc_counter_mask; |
---|
1873 | scale = (uint64_t)1 << 63; |
---|
1874 | scale /= pps->capth->th_counter->tc_frequency; |
---|
1875 | scale *= 2; |
---|
1876 | bt.sec = 0; |
---|
1877 | bt.frac = 0; |
---|
1878 | bintime_addx(&bt, scale * tcount); |
---|
1879 | bintime2timespec(&bt, &ts); |
---|
1880 | hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec); |
---|
1881 | } |
---|
1882 | #endif |
---|
1883 | |
---|
1884 | /* Wakeup anyone sleeping in pps_fetch(). */ |
---|
1885 | wakeup(pps); |
---|
1886 | } |
---|
1887 | #else /* __rtems__ */ |
---|
1888 | /* FIXME: https://devel.rtems.org/ticket/2349 */ |
---|
1889 | #endif /* __rtems__ */ |
---|
1890 | |
---|
1891 | /* |
---|
1892 | * Timecounters need to be updated every so often to prevent the hardware |
---|
1893 | * counter from overflowing. Updating also recalculates the cached values |
---|
1894 | * used by the get*() family of functions, so their precision depends on |
---|
1895 | * the update frequency. |
---|
1896 | */ |
---|
1897 | |
---|
1898 | #ifndef __rtems__ |
---|
1899 | static int tc_tick; |
---|
1900 | SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, |
---|
1901 | "Approximate number of hardclock ticks in a millisecond"); |
---|
1902 | #endif /* __rtems__ */ |
---|
1903 | |
---|
1904 | #ifndef __rtems__ |
---|
1905 | void |
---|
1906 | tc_ticktock(int cnt) |
---|
1907 | { |
---|
1908 | static int count; |
---|
1909 | |
---|
1910 | count += cnt; |
---|
1911 | if (count < tc_tick) |
---|
1912 | return; |
---|
1913 | count = 0; |
---|
1914 | #else /* __rtems__ */ |
---|
1915 | void |
---|
1916 | _Timecounter_Tick(void) |
---|
1917 | { |
---|
1918 | #endif /* __rtems__ */ |
---|
1919 | tc_windup(); |
---|
1920 | #ifdef __rtems__ |
---|
1921 | _Watchdog_Tick(); |
---|
1922 | #endif /* __rtems__ */ |
---|
1923 | } |
---|
1924 | #ifdef __rtems__ |
---|
1925 | void |
---|
1926 | _Timecounter_Tick_simple(uint32_t delta, uint32_t offset) |
---|
1927 | { |
---|
1928 | struct bintime bt; |
---|
1929 | struct timehands *th; |
---|
1930 | uint32_t ogen; |
---|
1931 | ISR_lock_Context lock_context; |
---|
1932 | |
---|
1933 | _ISR_lock_ISR_disable_and_acquire(&_Timecounter_Lock, &lock_context); |
---|
1934 | |
---|
1935 | th = timehands; |
---|
1936 | ogen = th->th_generation; |
---|
1937 | th->th_offset_count = offset; |
---|
1938 | bintime_addx(&th->th_offset, th->th_scale * delta); |
---|
1939 | |
---|
1940 | bt = th->th_offset; |
---|
1941 | bintime_add(&bt, &boottimebin); |
---|
1942 | |
---|
1943 | /* Update the UTC timestamps used by the get*() functions. */ |
---|
1944 | /* XXX shouldn't do this here. Should force non-`get' versions. */ |
---|
1945 | bintime2timeval(&bt, &th->th_microtime); |
---|
1946 | bintime2timespec(&bt, &th->th_nanotime); |
---|
1947 | |
---|
1948 | /* |
---|
1949 | * Now that the struct timehands is again consistent, set the new |
---|
1950 | * generation number, making sure to not make it zero. |
---|
1951 | */ |
---|
1952 | if (++ogen == 0) |
---|
1953 | ogen = 1; |
---|
1954 | th->th_generation = ogen; |
---|
1955 | |
---|
1956 | /* Go live with the new struct timehands. */ |
---|
1957 | time_second = th->th_microtime.tv_sec; |
---|
1958 | time_uptime = th->th_offset.sec; |
---|
1959 | |
---|
1960 | _ISR_lock_Release_and_ISR_enable(&_Timecounter_Lock, &lock_context); |
---|
1961 | |
---|
1962 | _Watchdog_Tick(); |
---|
1963 | } |
---|
1964 | #endif /* __rtems__ */ |
---|
1965 | |
---|
1966 | #ifndef __rtems__ |
---|
1967 | static void __inline |
---|
1968 | tc_adjprecision(void) |
---|
1969 | { |
---|
1970 | int t; |
---|
1971 | |
---|
1972 | if (tc_timepercentage > 0) { |
---|
1973 | t = (99 + tc_timepercentage) / tc_timepercentage; |
---|
1974 | tc_precexp = fls(t + (t >> 1)) - 1; |
---|
1975 | FREQ2BT(hz / tc_tick, &bt_timethreshold); |
---|
1976 | FREQ2BT(hz, &bt_tickthreshold); |
---|
1977 | bintime_shift(&bt_timethreshold, tc_precexp); |
---|
1978 | bintime_shift(&bt_tickthreshold, tc_precexp); |
---|
1979 | } else { |
---|
1980 | tc_precexp = 31; |
---|
1981 | bt_timethreshold.sec = INT_MAX; |
---|
1982 | bt_timethreshold.frac = ~(uint64_t)0; |
---|
1983 | bt_tickthreshold = bt_timethreshold; |
---|
1984 | } |
---|
1985 | sbt_timethreshold = bttosbt(bt_timethreshold); |
---|
1986 | sbt_tickthreshold = bttosbt(bt_tickthreshold); |
---|
1987 | } |
---|
1988 | #endif /* __rtems__ */ |
---|
1989 | |
---|
1990 | #ifndef __rtems__ |
---|
1991 | static int |
---|
1992 | sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS) |
---|
1993 | { |
---|
1994 | int error, val; |
---|
1995 | |
---|
1996 | val = tc_timepercentage; |
---|
1997 | error = sysctl_handle_int(oidp, &val, 0, req); |
---|
1998 | if (error != 0 || req->newptr == NULL) |
---|
1999 | return (error); |
---|
2000 | tc_timepercentage = val; |
---|
2001 | if (cold) |
---|
2002 | goto done; |
---|
2003 | tc_adjprecision(); |
---|
2004 | done: |
---|
2005 | return (0); |
---|
2006 | } |
---|
2007 | #endif /* __rtems__ */ |
---|
2008 | |
---|
2009 | #ifndef __rtems__ |
---|
2010 | static void |
---|
2011 | inittimecounter(void *dummy) |
---|
2012 | #else /* __rtems__ */ |
---|
2013 | void |
---|
2014 | _Timecounter_Initialize(void) |
---|
2015 | #endif /* __rtems__ */ |
---|
2016 | { |
---|
2017 | #ifndef __rtems__ |
---|
2018 | u_int p; |
---|
2019 | int tick_rate; |
---|
2020 | |
---|
2021 | /* |
---|
2022 | * Set the initial timeout to |
---|
2023 | * max(1, <approx. number of hardclock ticks in a millisecond>). |
---|
2024 | * People should probably not use the sysctl to set the timeout |
---|
2025 | * to smaller than its inital value, since that value is the |
---|
2026 | * smallest reasonable one. If they want better timestamps they |
---|
2027 | * should use the non-"get"* functions. |
---|
2028 | */ |
---|
2029 | if (hz > 1000) |
---|
2030 | tc_tick = (hz + 500) / 1000; |
---|
2031 | else |
---|
2032 | tc_tick = 1; |
---|
2033 | tc_adjprecision(); |
---|
2034 | FREQ2BT(hz, &tick_bt); |
---|
2035 | tick_sbt = bttosbt(tick_bt); |
---|
2036 | tick_rate = hz / tc_tick; |
---|
2037 | FREQ2BT(tick_rate, &tc_tick_bt); |
---|
2038 | tc_tick_sbt = bttosbt(tc_tick_bt); |
---|
2039 | p = (tc_tick * 1000000) / hz; |
---|
2040 | printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000); |
---|
2041 | #endif /* __rtems__ */ |
---|
2042 | |
---|
2043 | #ifdef FFCLOCK |
---|
2044 | ffclock_init(); |
---|
2045 | #endif |
---|
2046 | /* warm up new timecounter (again) and get rolling. */ |
---|
2047 | (void)timecounter->tc_get_timecount(timecounter); |
---|
2048 | (void)timecounter->tc_get_timecount(timecounter); |
---|
2049 | tc_windup(); |
---|
2050 | } |
---|
2051 | |
---|
2052 | #ifndef __rtems__ |
---|
2053 | SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL); |
---|
2054 | #endif /* __rtems__ */ |
---|
2055 | |
---|
2056 | #ifndef __rtems__ |
---|
2057 | /* Cpu tick handling -------------------------------------------------*/ |
---|
2058 | |
---|
2059 | static int cpu_tick_variable; |
---|
2060 | static uint64_t cpu_tick_frequency; |
---|
2061 | |
---|
2062 | static uint64_t |
---|
2063 | tc_cpu_ticks(void) |
---|
2064 | { |
---|
2065 | static uint64_t base; |
---|
2066 | static unsigned last; |
---|
2067 | unsigned u; |
---|
2068 | struct timecounter *tc; |
---|
2069 | |
---|
2070 | tc = timehands->th_counter; |
---|
2071 | u = tc->tc_get_timecount(tc) & tc->tc_counter_mask; |
---|
2072 | if (u < last) |
---|
2073 | base += (uint64_t)tc->tc_counter_mask + 1; |
---|
2074 | last = u; |
---|
2075 | return (u + base); |
---|
2076 | } |
---|
2077 | |
---|
2078 | void |
---|
2079 | cpu_tick_calibration(void) |
---|
2080 | { |
---|
2081 | static time_t last_calib; |
---|
2082 | |
---|
2083 | if (time_uptime != last_calib && !(time_uptime & 0xf)) { |
---|
2084 | cpu_tick_calibrate(0); |
---|
2085 | last_calib = time_uptime; |
---|
2086 | } |
---|
2087 | } |
---|
2088 | |
---|
2089 | /* |
---|
2090 | * This function gets called every 16 seconds on only one designated |
---|
2091 | * CPU in the system from hardclock() via cpu_tick_calibration()(). |
---|
2092 | * |
---|
2093 | * Whenever the real time clock is stepped we get called with reset=1 |
---|
2094 | * to make sure we handle suspend/resume and similar events correctly. |
---|
2095 | */ |
---|
2096 | |
---|
2097 | static void |
---|
2098 | cpu_tick_calibrate(int reset) |
---|
2099 | { |
---|
2100 | static uint64_t c_last; |
---|
2101 | uint64_t c_this, c_delta; |
---|
2102 | static struct bintime t_last; |
---|
2103 | struct bintime t_this, t_delta; |
---|
2104 | uint32_t divi; |
---|
2105 | |
---|
2106 | if (reset) { |
---|
2107 | /* The clock was stepped, abort & reset */ |
---|
2108 | t_last.sec = 0; |
---|
2109 | return; |
---|
2110 | } |
---|
2111 | |
---|
2112 | /* we don't calibrate fixed rate cputicks */ |
---|
2113 | if (!cpu_tick_variable) |
---|
2114 | return; |
---|
2115 | |
---|
2116 | getbinuptime(&t_this); |
---|
2117 | c_this = cpu_ticks(); |
---|
2118 | if (t_last.sec != 0) { |
---|
2119 | c_delta = c_this - c_last; |
---|
2120 | t_delta = t_this; |
---|
2121 | bintime_sub(&t_delta, &t_last); |
---|
2122 | /* |
---|
2123 | * Headroom: |
---|
2124 | * 2^(64-20) / 16[s] = |
---|
2125 | * 2^(44) / 16[s] = |
---|
2126 | * 17.592.186.044.416 / 16 = |
---|
2127 | * 1.099.511.627.776 [Hz] |
---|
2128 | */ |
---|
2129 | divi = t_delta.sec << 20; |
---|
2130 | divi |= t_delta.frac >> (64 - 20); |
---|
2131 | c_delta <<= 20; |
---|
2132 | c_delta /= divi; |
---|
2133 | if (c_delta > cpu_tick_frequency) { |
---|
2134 | if (0 && bootverbose) |
---|
2135 | printf("cpu_tick increased to %ju Hz\n", |
---|
2136 | c_delta); |
---|
2137 | cpu_tick_frequency = c_delta; |
---|
2138 | } |
---|
2139 | } |
---|
2140 | c_last = c_this; |
---|
2141 | t_last = t_this; |
---|
2142 | } |
---|
2143 | |
---|
2144 | void |
---|
2145 | set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var) |
---|
2146 | { |
---|
2147 | |
---|
2148 | if (func == NULL) { |
---|
2149 | cpu_ticks = tc_cpu_ticks; |
---|
2150 | } else { |
---|
2151 | cpu_tick_frequency = freq; |
---|
2152 | cpu_tick_variable = var; |
---|
2153 | cpu_ticks = func; |
---|
2154 | } |
---|
2155 | } |
---|
2156 | |
---|
2157 | uint64_t |
---|
2158 | cpu_tickrate(void) |
---|
2159 | { |
---|
2160 | |
---|
2161 | if (cpu_ticks == tc_cpu_ticks) |
---|
2162 | return (tc_getfrequency()); |
---|
2163 | return (cpu_tick_frequency); |
---|
2164 | } |
---|
2165 | |
---|
2166 | /* |
---|
2167 | * We need to be slightly careful converting cputicks to microseconds. |
---|
2168 | * There is plenty of margin in 64 bits of microseconds (half a million |
---|
2169 | * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply |
---|
2170 | * before divide conversion (to retain precision) we find that the |
---|
2171 | * margin shrinks to 1.5 hours (one millionth of 146y). |
---|
2172 | * With a three prong approach we never lose significant bits, no |
---|
2173 | * matter what the cputick rate and length of timeinterval is. |
---|
2174 | */ |
---|
2175 | |
---|
2176 | uint64_t |
---|
2177 | cputick2usec(uint64_t tick) |
---|
2178 | { |
---|
2179 | |
---|
2180 | if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */ |
---|
2181 | return (tick / (cpu_tickrate() / 1000000LL)); |
---|
2182 | else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */ |
---|
2183 | return ((tick * 1000LL) / (cpu_tickrate() / 1000LL)); |
---|
2184 | else |
---|
2185 | return ((tick * 1000000LL) / cpu_tickrate()); |
---|
2186 | } |
---|
2187 | |
---|
2188 | cpu_tick_f *cpu_ticks = tc_cpu_ticks; |
---|
2189 | #endif /* __rtems__ */ |
---|
2190 | |
---|
2191 | #ifndef __rtems__ |
---|
2192 | static int vdso_th_enable = 1; |
---|
2193 | static int |
---|
2194 | sysctl_fast_gettime(SYSCTL_HANDLER_ARGS) |
---|
2195 | { |
---|
2196 | int old_vdso_th_enable, error; |
---|
2197 | |
---|
2198 | old_vdso_th_enable = vdso_th_enable; |
---|
2199 | error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req); |
---|
2200 | if (error != 0) |
---|
2201 | return (error); |
---|
2202 | vdso_th_enable = old_vdso_th_enable; |
---|
2203 | return (0); |
---|
2204 | } |
---|
2205 | SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime, |
---|
2206 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, |
---|
2207 | NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day"); |
---|
2208 | |
---|
2209 | uint32_t |
---|
2210 | tc_fill_vdso_timehands(struct vdso_timehands *vdso_th) |
---|
2211 | { |
---|
2212 | struct timehands *th; |
---|
2213 | uint32_t enabled; |
---|
2214 | |
---|
2215 | th = timehands; |
---|
2216 | vdso_th->th_algo = VDSO_TH_ALGO_1; |
---|
2217 | vdso_th->th_scale = th->th_scale; |
---|
2218 | vdso_th->th_offset_count = th->th_offset_count; |
---|
2219 | vdso_th->th_counter_mask = th->th_counter->tc_counter_mask; |
---|
2220 | vdso_th->th_offset = th->th_offset; |
---|
2221 | vdso_th->th_boottime = boottimebin; |
---|
2222 | enabled = cpu_fill_vdso_timehands(vdso_th, th->th_counter); |
---|
2223 | if (!vdso_th_enable) |
---|
2224 | enabled = 0; |
---|
2225 | return (enabled); |
---|
2226 | } |
---|
2227 | #endif /* __rtems__ */ |
---|
2228 | |
---|
2229 | #ifdef COMPAT_FREEBSD32 |
---|
2230 | uint32_t |
---|
2231 | tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32) |
---|
2232 | { |
---|
2233 | struct timehands *th; |
---|
2234 | uint32_t enabled; |
---|
2235 | |
---|
2236 | th = timehands; |
---|
2237 | vdso_th32->th_algo = VDSO_TH_ALGO_1; |
---|
2238 | *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale; |
---|
2239 | vdso_th32->th_offset_count = th->th_offset_count; |
---|
2240 | vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask; |
---|
2241 | vdso_th32->th_offset.sec = th->th_offset.sec; |
---|
2242 | *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac; |
---|
2243 | vdso_th32->th_boottime.sec = boottimebin.sec; |
---|
2244 | *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac; |
---|
2245 | enabled = cpu_fill_vdso_timehands32(vdso_th32, th->th_counter); |
---|
2246 | if (!vdso_th_enable) |
---|
2247 | enabled = 0; |
---|
2248 | return (enabled); |
---|
2249 | } |
---|
2250 | #endif |
---|