1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/percpu.h>
14#include <linux/init.h>
15#include <linux/mm.h>
16#include <linux/sysdev.h>
17#include <linux/clocksource.h>
18#include <linux/jiffies.h>
19#include <linux/time.h>
20#include <linux/tick.h>
21
22
23
24
25
26
27__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45struct timespec xtime __attribute__ ((aligned (16)));
46struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
47static unsigned long total_sleep_time;
48
49
50int __read_mostly timekeeping_suspended;
51
52static struct timespec xtime_cache __attribute__ ((aligned (16)));
53void update_xtime_cache(u64 nsec)
54{
55 xtime_cache = xtime;
56 timespec_add_ns(&xtime_cache, nsec);
57}
58
59struct clocksource *clock;
60
61
62#ifdef CONFIG_GENERIC_TIME
63
64
65
66
67
68
69
70static void clocksource_forward_now(void)
71{
72 cycle_t cycle_now, cycle_delta;
73 s64 nsec;
74
75 cycle_now = clocksource_read(clock);
76 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
77 clock->cycle_last = cycle_now;
78
79 nsec = cyc2ns(clock, cycle_delta);
80 timespec_add_ns(&xtime, nsec);
81
82 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
83 clock->raw_time.tv_nsec += nsec;
84}
85
86
87
88
89
90
91
92void getnstimeofday(struct timespec *ts)
93{
94 cycle_t cycle_now, cycle_delta;
95 unsigned long seq;
96 s64 nsecs;
97
98 WARN_ON(timekeeping_suspended);
99
100 do {
101 seq = read_seqbegin(&xtime_lock);
102
103 *ts = xtime;
104
105
106 cycle_now = clocksource_read(clock);
107
108
109 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
110
111
112 nsecs = cyc2ns(clock, cycle_delta);
113
114 } while (read_seqretry(&xtime_lock, seq));
115
116 timespec_add_ns(ts, nsecs);
117}
118
119EXPORT_SYMBOL(getnstimeofday);
120
121
122
123
124
125
126
127void do_gettimeofday(struct timeval *tv)
128{
129 struct timespec now;
130
131 getnstimeofday(&now);
132 tv->tv_sec = now.tv_sec;
133 tv->tv_usec = now.tv_nsec/1000;
134}
135
136EXPORT_SYMBOL(do_gettimeofday);
137
138
139
140
141
142
143int do_settimeofday(struct timespec *tv)
144{
145 struct timespec ts_delta;
146 unsigned long flags;
147
148 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
149 return -EINVAL;
150
151 write_seqlock_irqsave(&xtime_lock, flags);
152
153 clocksource_forward_now();
154
155 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
156 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
157 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
158
159 xtime = *tv;
160
161 update_xtime_cache(0);
162
163 clock->error = 0;
164 ntp_clear();
165
166 update_vsyscall(&xtime, clock);
167
168 write_sequnlock_irqrestore(&xtime_lock, flags);
169
170
171 clock_was_set();
172
173 return 0;
174}
175
176EXPORT_SYMBOL(do_settimeofday);
177
178
179
180
181
182
183static void change_clocksource(void)
184{
185 struct clocksource *new;
186
187 new = clocksource_get_next();
188
189 if (clock == new)
190 return;
191
192 clocksource_forward_now();
193
194 new->raw_time = clock->raw_time;
195
196 clock = new;
197 clock->cycle_last = 0;
198 clock->cycle_last = clocksource_read(new);
199 clock->error = 0;
200 clock->xtime_nsec = 0;
201 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
202
203 tick_clock_notify();
204
205
206
207
208
209
210
211}
212#else
213static inline void clocksource_forward_now(void) { }
214static inline void change_clocksource(void) { }
215#endif
216
217
218
219
220
221
222
223void getrawmonotonic(struct timespec *ts)
224{
225 unsigned long seq;
226 s64 nsecs;
227 cycle_t cycle_now, cycle_delta;
228
229 do {
230 seq = read_seqbegin(&xtime_lock);
231
232
233 cycle_now = clocksource_read(clock);
234
235
236 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
237
238
239 nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
240
241 *ts = clock->raw_time;
242
243 } while (read_seqretry(&xtime_lock, seq));
244
245 timespec_add_ns(ts, nsecs);
246}
247EXPORT_SYMBOL(getrawmonotonic);
248
249
250
251
252
253int timekeeping_valid_for_hres(void)
254{
255 unsigned long seq;
256 int ret;
257
258 do {
259 seq = read_seqbegin(&xtime_lock);
260
261 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
262
263 } while (read_seqretry(&xtime_lock, seq));
264
265 return ret;
266}
267
268
269
270
271
272
273
274
275
276
277unsigned long __attribute__((weak)) read_persistent_clock(void)
278{
279 return 0;
280}
281
282
283
284
285void __init timekeeping_init(void)
286{
287 unsigned long flags;
288 unsigned long sec = read_persistent_clock();
289
290 write_seqlock_irqsave(&xtime_lock, flags);
291
292 ntp_init();
293
294 clock = clocksource_get_next();
295 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
296 clock->cycle_last = clocksource_read(clock);
297
298 xtime.tv_sec = sec;
299 xtime.tv_nsec = 0;
300 set_normalized_timespec(&wall_to_monotonic,
301 -xtime.tv_sec, -xtime.tv_nsec);
302 update_xtime_cache(0);
303 total_sleep_time = 0;
304 write_sequnlock_irqrestore(&xtime_lock, flags);
305}
306
307
308static unsigned long timekeeping_suspend_time;
309
310
311
312
313
314
315
316
317
318static int timekeeping_resume(struct sys_device *dev)
319{
320 unsigned long flags;
321 unsigned long now = read_persistent_clock();
322
323 clocksource_resume();
324
325 write_seqlock_irqsave(&xtime_lock, flags);
326
327 if (now && (now > timekeeping_suspend_time)) {
328 unsigned long sleep_length = now - timekeeping_suspend_time;
329
330 xtime.tv_sec += sleep_length;
331 wall_to_monotonic.tv_sec -= sleep_length;
332 total_sleep_time += sleep_length;
333 }
334 update_xtime_cache(0);
335
336 clock->cycle_last = 0;
337 clock->cycle_last = clocksource_read(clock);
338 clock->error = 0;
339 timekeeping_suspended = 0;
340 write_sequnlock_irqrestore(&xtime_lock, flags);
341
342 touch_softlockup_watchdog();
343
344 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
345
346
347 hres_timers_resume();
348
349 return 0;
350}
351
352static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
353{
354 unsigned long flags;
355
356 timekeeping_suspend_time = read_persistent_clock();
357
358 write_seqlock_irqsave(&xtime_lock, flags);
359 clocksource_forward_now();
360 timekeeping_suspended = 1;
361 write_sequnlock_irqrestore(&xtime_lock, flags);
362
363 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
364
365 return 0;
366}
367
368
369static struct sysdev_class timekeeping_sysclass = {
370 .name = "timekeeping",
371 .resume = timekeeping_resume,
372 .suspend = timekeeping_suspend,
373};
374
375static struct sys_device device_timer = {
376 .id = 0,
377 .cls = &timekeeping_sysclass,
378};
379
380static int __init timekeeping_init_device(void)
381{
382 int error = sysdev_class_register(&timekeeping_sysclass);
383 if (!error)
384 error = sysdev_register(&device_timer);
385 return error;
386}
387
388device_initcall(timekeeping_init_device);
389
390
391
392
393
394static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
395 s64 *offset)
396{
397 s64 tick_error, i;
398 u32 look_ahead, adj;
399 s32 error2, mult;
400
401
402
403
404
405
406
407
408
409
410 error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
411 error2 = abs(error2);
412 for (look_ahead = 0; error2 > 0; look_ahead++)
413 error2 >>= 2;
414
415
416
417
418
419 tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1);
420 tick_error -= clock->xtime_interval >> 1;
421 error = ((error - tick_error) >> look_ahead) + tick_error;
422
423
424 i = *interval;
425 mult = 1;
426 if (error < 0) {
427 error = -error;
428 *interval = -*interval;
429 *offset = -*offset;
430 mult = -1;
431 }
432 for (adj = 0; error > i; adj++)
433 error >>= 1;
434
435 *interval <<= adj;
436 *offset <<= adj;
437 return mult << adj;
438}
439
440
441
442
443
444
445static void clocksource_adjust(s64 offset)
446{
447 s64 error, interval = clock->cycle_interval;
448 int adj;
449
450 error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1);
451 if (error > interval) {
452 error >>= 2;
453 if (likely(error <= interval))
454 adj = 1;
455 else
456 adj = clocksource_bigadjust(error, &interval, &offset);
457 } else if (error < -interval) {
458 error >>= 2;
459 if (likely(error >= -interval)) {
460 adj = -1;
461 interval = -interval;
462 offset = -offset;
463 } else
464 adj = clocksource_bigadjust(error, &interval, &offset);
465 } else
466 return;
467
468 clock->mult += adj;
469 clock->xtime_interval += interval;
470 clock->xtime_nsec -= offset;
471 clock->error -= (interval - offset) <<
472 (NTP_SCALE_SHIFT - clock->shift);
473}
474
475
476
477
478
479
480void update_wall_time(void)
481{
482 cycle_t offset;
483
484
485 if (unlikely(timekeeping_suspended))
486 return;
487
488#ifdef CONFIG_GENERIC_TIME
489 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
490#else
491 offset = clock->cycle_interval;
492#endif
493 clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift;
494
495
496
497
498 while (offset >= clock->cycle_interval) {
499
500 offset -= clock->cycle_interval;
501 clock->cycle_last += clock->cycle_interval;
502
503 clock->xtime_nsec += clock->xtime_interval;
504 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
505 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
506 xtime.tv_sec++;
507 second_overflow();
508 }
509
510 clock->raw_time.tv_nsec += clock->raw_interval;
511 if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
512 clock->raw_time.tv_nsec -= NSEC_PER_SEC;
513 clock->raw_time.tv_sec++;
514 }
515
516
517 clock->error += tick_length;
518 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
519 }
520
521
522 clocksource_adjust(offset);
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 if (unlikely((s64)clock->xtime_nsec < 0)) {
541 s64 neg = -(s64)clock->xtime_nsec;
542 clock->xtime_nsec = 0;
543 clock->error += neg << (NTP_SCALE_SHIFT - clock->shift);
544 }
545
546
547
548
549 xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1;
550 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
551 clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift);
552
553 update_xtime_cache(cyc2ns(clock, offset));
554
555
556 change_clocksource();
557 update_vsyscall(&xtime, clock);
558}
559
560
561
562
563
564
565
566
567
568
569
570
571void getboottime(struct timespec *ts)
572{
573 set_normalized_timespec(ts,
574 - (wall_to_monotonic.tv_sec + total_sleep_time),
575 - wall_to_monotonic.tv_nsec);
576}
577
578
579
580
581
582void monotonic_to_bootbased(struct timespec *ts)
583{
584 ts->tv_sec += total_sleep_time;
585}
586
587unsigned long get_seconds(void)
588{
589 return xtime_cache.tv_sec;
590}
591EXPORT_SYMBOL(get_seconds);
592
593
594struct timespec current_kernel_time(void)
595{
596 struct timespec now;
597 unsigned long seq;
598
599 do {
600 seq = read_seqbegin(&xtime_lock);
601
602 now = xtime_cache;
603 } while (read_seqretry(&xtime_lock, seq));
604
605 return now;
606}
607EXPORT_SYMBOL(current_kernel_time);
608