xref: /openbmc/linux/kernel/time/timekeeping.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*
2  *  linux/kernel/time/timekeeping.c
3  *
4  *  Kernel timekeeping code and accessor functions
5  *
6  *  This code was moved from linux/kernel/timer.c.
7  *  Please see that file for copyright and history logs.
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  * This read-write spinlock protects us from races in SMP while
25  * playing with xtime and avenrun.
26  */
27 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
28 
29 
30 /*
31  * The current time
32  * wall_to_monotonic is what we need to add to xtime (or xtime corrected
33  * for sub jiffie times) to get to monotonic time.  Monotonic is pegged
34  * at zero at system boot time, so wall_to_monotonic will be negative,
35  * however, we will ALWAYS keep the tv_nsec part positive so we can use
36  * the usual normalization.
37  *
38  * wall_to_monotonic is moved after resume from suspend for the monotonic
39  * time not to jump. We need to add total_sleep_time to wall_to_monotonic
40  * to get the real boot based time offset.
41  *
42  * - wall_to_monotonic is no longer the boot time, getboottime must be
43  * used instead.
44  */
45 struct timespec xtime __attribute__ ((aligned (16)));
46 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
47 static unsigned long total_sleep_time;		/* seconds */
48 
49 static struct timespec xtime_cache __attribute__ ((aligned (16)));
50 void update_xtime_cache(u64 nsec)
51 {
52 	xtime_cache = xtime;
53 	timespec_add_ns(&xtime_cache, nsec);
54 }
55 
56 struct clocksource *clock;
57 
58 
59 #ifdef CONFIG_GENERIC_TIME
60 /**
61  * clocksource_forward_now - update clock to the current time
62  *
63  * Forward the current clock to update its state since the last call to
64  * update_wall_time(). This is useful before significant clock changes,
65  * as it avoids having to deal with this time offset explicitly.
66  */
67 static void clocksource_forward_now(void)
68 {
69 	cycle_t cycle_now, cycle_delta;
70 	s64 nsec;
71 
72 	cycle_now = clocksource_read(clock);
73 	cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
74 	clock->cycle_last = cycle_now;
75 
76 	nsec = cyc2ns(clock, cycle_delta);
77 	timespec_add_ns(&xtime, nsec);
78 
79 	nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
80 	clock->raw_time.tv_nsec += nsec;
81 }
82 
83 /**
84  * getnstimeofday - Returns the time of day in a timespec
85  * @ts:		pointer to the timespec to be set
86  *
87  * Returns the time of day in a timespec.
88  */
89 void getnstimeofday(struct timespec *ts)
90 {
91 	cycle_t cycle_now, cycle_delta;
92 	unsigned long seq;
93 	s64 nsecs;
94 
95 	do {
96 		seq = read_seqbegin(&xtime_lock);
97 
98 		*ts = xtime;
99 
100 		/* read clocksource: */
101 		cycle_now = clocksource_read(clock);
102 
103 		/* calculate the delta since the last update_wall_time: */
104 		cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
105 
106 		/* convert to nanoseconds: */
107 		nsecs = cyc2ns(clock, cycle_delta);
108 
109 	} while (read_seqretry(&xtime_lock, seq));
110 
111 	timespec_add_ns(ts, nsecs);
112 }
113 
114 EXPORT_SYMBOL(getnstimeofday);
115 
116 /**
117  * do_gettimeofday - Returns the time of day in a timeval
118  * @tv:		pointer to the timeval to be set
119  *
120  * NOTE: Users should be converted to using getnstimeofday()
121  */
122 void do_gettimeofday(struct timeval *tv)
123 {
124 	struct timespec now;
125 
126 	getnstimeofday(&now);
127 	tv->tv_sec = now.tv_sec;
128 	tv->tv_usec = now.tv_nsec/1000;
129 }
130 
131 EXPORT_SYMBOL(do_gettimeofday);
132 /**
133  * do_settimeofday - Sets the time of day
134  * @tv:		pointer to the timespec variable containing the new time
135  *
136  * Sets the time of day to the new time and update NTP and notify hrtimers
137  */
138 int do_settimeofday(struct timespec *tv)
139 {
140 	struct timespec ts_delta;
141 	unsigned long flags;
142 
143 	if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
144 		return -EINVAL;
145 
146 	write_seqlock_irqsave(&xtime_lock, flags);
147 
148 	clocksource_forward_now();
149 
150 	ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
151 	ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
152 	wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
153 
154 	xtime = *tv;
155 
156 	update_xtime_cache(0);
157 
158 	clock->error = 0;
159 	ntp_clear();
160 
161 	update_vsyscall(&xtime, clock);
162 
163 	write_sequnlock_irqrestore(&xtime_lock, flags);
164 
165 	/* signal hrtimers about time change */
166 	clock_was_set();
167 
168 	return 0;
169 }
170 
171 EXPORT_SYMBOL(do_settimeofday);
172 
173 /**
174  * change_clocksource - Swaps clocksources if a new one is available
175  *
176  * Accumulates current time interval and initializes new clocksource
177  */
178 static void change_clocksource(void)
179 {
180 	struct clocksource *new;
181 
182 	new = clocksource_get_next();
183 
184 	if (clock == new)
185 		return;
186 
187 	clocksource_forward_now();
188 
189 	new->raw_time = clock->raw_time;
190 
191 	clock = new;
192 	clock->cycle_last = 0;
193 	clock->cycle_last = clocksource_read(new);
194 	clock->error = 0;
195 	clock->xtime_nsec = 0;
196 	clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
197 
198 	tick_clock_notify();
199 
200 	/*
201 	 * We're holding xtime lock and waking up klogd would deadlock
202 	 * us on enqueue.  So no printing!
203 	printk(KERN_INFO "Time: %s clocksource has been installed.\n",
204 	       clock->name);
205 	 */
206 }
207 #else
208 static inline void clocksource_forward_now(void) { }
209 static inline void change_clocksource(void) { }
210 #endif
211 
212 /**
213  * getrawmonotonic - Returns the raw monotonic time in a timespec
214  * @ts:		pointer to the timespec to be set
215  *
216  * Returns the raw monotonic time (completely un-modified by ntp)
217  */
218 void getrawmonotonic(struct timespec *ts)
219 {
220 	unsigned long seq;
221 	s64 nsecs;
222 	cycle_t cycle_now, cycle_delta;
223 
224 	do {
225 		seq = read_seqbegin(&xtime_lock);
226 
227 		/* read clocksource: */
228 		cycle_now = clocksource_read(clock);
229 
230 		/* calculate the delta since the last update_wall_time: */
231 		cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
232 
233 		/* convert to nanoseconds: */
234 		nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
235 
236 		*ts = clock->raw_time;
237 
238 	} while (read_seqretry(&xtime_lock, seq));
239 
240 	timespec_add_ns(ts, nsecs);
241 }
242 EXPORT_SYMBOL(getrawmonotonic);
243 
244 
245 /**
246  * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
247  */
248 int timekeeping_valid_for_hres(void)
249 {
250 	unsigned long seq;
251 	int ret;
252 
253 	do {
254 		seq = read_seqbegin(&xtime_lock);
255 
256 		ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
257 
258 	} while (read_seqretry(&xtime_lock, seq));
259 
260 	return ret;
261 }
262 
263 /**
264  * read_persistent_clock -  Return time in seconds from the persistent clock.
265  *
266  * Weak dummy function for arches that do not yet support it.
267  * Returns seconds from epoch using the battery backed persistent clock.
268  * Returns zero if unsupported.
269  *
270  *  XXX - Do be sure to remove it once all arches implement it.
271  */
272 unsigned long __attribute__((weak)) read_persistent_clock(void)
273 {
274 	return 0;
275 }
276 
277 /*
278  * timekeeping_init - Initializes the clocksource and common timekeeping values
279  */
280 void __init timekeeping_init(void)
281 {
282 	unsigned long flags;
283 	unsigned long sec = read_persistent_clock();
284 
285 	write_seqlock_irqsave(&xtime_lock, flags);
286 
287 	ntp_init();
288 
289 	clock = clocksource_get_next();
290 	clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
291 	clock->cycle_last = clocksource_read(clock);
292 
293 	xtime.tv_sec = sec;
294 	xtime.tv_nsec = 0;
295 	set_normalized_timespec(&wall_to_monotonic,
296 		-xtime.tv_sec, -xtime.tv_nsec);
297 	update_xtime_cache(0);
298 	total_sleep_time = 0;
299 	write_sequnlock_irqrestore(&xtime_lock, flags);
300 }
301 
302 /* flag for if timekeeping is suspended */
303 static int timekeeping_suspended;
304 /* time in seconds when suspend began */
305 static unsigned long timekeeping_suspend_time;
306 
307 /**
308  * timekeeping_resume - Resumes the generic timekeeping subsystem.
309  * @dev:	unused
310  *
311  * This is for the generic clocksource timekeeping.
312  * xtime/wall_to_monotonic/jiffies/etc are
313  * still managed by arch specific suspend/resume code.
314  */
315 static int timekeeping_resume(struct sys_device *dev)
316 {
317 	unsigned long flags;
318 	unsigned long now = read_persistent_clock();
319 
320 	clocksource_resume();
321 
322 	write_seqlock_irqsave(&xtime_lock, flags);
323 
324 	if (now && (now > timekeeping_suspend_time)) {
325 		unsigned long sleep_length = now - timekeeping_suspend_time;
326 
327 		xtime.tv_sec += sleep_length;
328 		wall_to_monotonic.tv_sec -= sleep_length;
329 		total_sleep_time += sleep_length;
330 	}
331 	update_xtime_cache(0);
332 	/* re-base the last cycle value */
333 	clock->cycle_last = 0;
334 	clock->cycle_last = clocksource_read(clock);
335 	clock->error = 0;
336 	timekeeping_suspended = 0;
337 	write_sequnlock_irqrestore(&xtime_lock, flags);
338 
339 	touch_softlockup_watchdog();
340 
341 	clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
342 
343 	/* Resume hrtimers */
344 	hres_timers_resume();
345 
346 	return 0;
347 }
348 
349 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
350 {
351 	unsigned long flags;
352 
353 	timekeeping_suspend_time = read_persistent_clock();
354 
355 	write_seqlock_irqsave(&xtime_lock, flags);
356 	clocksource_forward_now();
357 	timekeeping_suspended = 1;
358 	write_sequnlock_irqrestore(&xtime_lock, flags);
359 
360 	clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
361 
362 	return 0;
363 }
364 
365 /* sysfs resume/suspend bits for timekeeping */
366 static struct sysdev_class timekeeping_sysclass = {
367 	.name		= "timekeeping",
368 	.resume		= timekeeping_resume,
369 	.suspend	= timekeeping_suspend,
370 };
371 
372 static struct sys_device device_timer = {
373 	.id		= 0,
374 	.cls		= &timekeeping_sysclass,
375 };
376 
377 static int __init timekeeping_init_device(void)
378 {
379 	int error = sysdev_class_register(&timekeeping_sysclass);
380 	if (!error)
381 		error = sysdev_register(&device_timer);
382 	return error;
383 }
384 
385 device_initcall(timekeeping_init_device);
386 
387 /*
388  * If the error is already larger, we look ahead even further
389  * to compensate for late or lost adjustments.
390  */
391 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
392 						 s64 *offset)
393 {
394 	s64 tick_error, i;
395 	u32 look_ahead, adj;
396 	s32 error2, mult;
397 
398 	/*
399 	 * Use the current error value to determine how much to look ahead.
400 	 * The larger the error the slower we adjust for it to avoid problems
401 	 * with losing too many ticks, otherwise we would overadjust and
402 	 * produce an even larger error.  The smaller the adjustment the
403 	 * faster we try to adjust for it, as lost ticks can do less harm
404 	 * here.  This is tuned so that an error of about 1 msec is adjusted
405 	 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
406 	 */
407 	error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
408 	error2 = abs(error2);
409 	for (look_ahead = 0; error2 > 0; look_ahead++)
410 		error2 >>= 2;
411 
412 	/*
413 	 * Now calculate the error in (1 << look_ahead) ticks, but first
414 	 * remove the single look ahead already included in the error.
415 	 */
416 	tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1);
417 	tick_error -= clock->xtime_interval >> 1;
418 	error = ((error - tick_error) >> look_ahead) + tick_error;
419 
420 	/* Finally calculate the adjustment shift value.  */
421 	i = *interval;
422 	mult = 1;
423 	if (error < 0) {
424 		error = -error;
425 		*interval = -*interval;
426 		*offset = -*offset;
427 		mult = -1;
428 	}
429 	for (adj = 0; error > i; adj++)
430 		error >>= 1;
431 
432 	*interval <<= adj;
433 	*offset <<= adj;
434 	return mult << adj;
435 }
436 
437 /*
438  * Adjust the multiplier to reduce the error value,
439  * this is optimized for the most common adjustments of -1,0,1,
440  * for other values we can do a bit more work.
441  */
442 static void clocksource_adjust(s64 offset)
443 {
444 	s64 error, interval = clock->cycle_interval;
445 	int adj;
446 
447 	error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1);
448 	if (error > interval) {
449 		error >>= 2;
450 		if (likely(error <= interval))
451 			adj = 1;
452 		else
453 			adj = clocksource_bigadjust(error, &interval, &offset);
454 	} else if (error < -interval) {
455 		error >>= 2;
456 		if (likely(error >= -interval)) {
457 			adj = -1;
458 			interval = -interval;
459 			offset = -offset;
460 		} else
461 			adj = clocksource_bigadjust(error, &interval, &offset);
462 	} else
463 		return;
464 
465 	clock->mult += adj;
466 	clock->xtime_interval += interval;
467 	clock->xtime_nsec -= offset;
468 	clock->error -= (interval - offset) <<
469 			(NTP_SCALE_SHIFT - clock->shift);
470 }
471 
472 /**
473  * update_wall_time - Uses the current clocksource to increment the wall time
474  *
475  * Called from the timer interrupt, must hold a write on xtime_lock.
476  */
477 void update_wall_time(void)
478 {
479 	cycle_t offset;
480 
481 	/* Make sure we're fully resumed: */
482 	if (unlikely(timekeeping_suspended))
483 		return;
484 
485 #ifdef CONFIG_GENERIC_TIME
486 	offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
487 #else
488 	offset = clock->cycle_interval;
489 #endif
490 	clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift;
491 
492 	/* normally this loop will run just once, however in the
493 	 * case of lost or late ticks, it will accumulate correctly.
494 	 */
495 	while (offset >= clock->cycle_interval) {
496 		/* accumulate one interval */
497 		offset -= clock->cycle_interval;
498 		clock->cycle_last += clock->cycle_interval;
499 
500 		clock->xtime_nsec += clock->xtime_interval;
501 		if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
502 			clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
503 			xtime.tv_sec++;
504 			second_overflow();
505 		}
506 
507 		clock->raw_time.tv_nsec += clock->raw_interval;
508 		if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
509 			clock->raw_time.tv_nsec -= NSEC_PER_SEC;
510 			clock->raw_time.tv_sec++;
511 		}
512 
513 		/* accumulate error between NTP and clock interval */
514 		clock->error += tick_length;
515 		clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
516 	}
517 
518 	/* correct the clock when NTP error is too big */
519 	clocksource_adjust(offset);
520 
521 	/* store full nanoseconds into xtime after rounding it up and
522 	 * add the remainder to the error difference.
523 	 */
524 	xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1;
525 	clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
526 	clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift);
527 
528 	update_xtime_cache(cyc2ns(clock, offset));
529 
530 	/* check to see if there is a new clocksource to use */
531 	change_clocksource();
532 	update_vsyscall(&xtime, clock);
533 }
534 
535 /**
536  * getboottime - Return the real time of system boot.
537  * @ts:		pointer to the timespec to be set
538  *
539  * Returns the time of day in a timespec.
540  *
541  * This is based on the wall_to_monotonic offset and the total suspend
542  * time. Calls to settimeofday will affect the value returned (which
543  * basically means that however wrong your real time clock is at boot time,
544  * you get the right time here).
545  */
546 void getboottime(struct timespec *ts)
547 {
548 	set_normalized_timespec(ts,
549 		- (wall_to_monotonic.tv_sec + total_sleep_time),
550 		- wall_to_monotonic.tv_nsec);
551 }
552 
553 /**
554  * monotonic_to_bootbased - Convert the monotonic time to boot based.
555  * @ts:		pointer to the timespec to be converted
556  */
557 void monotonic_to_bootbased(struct timespec *ts)
558 {
559 	ts->tv_sec += total_sleep_time;
560 }
561 
562 unsigned long get_seconds(void)
563 {
564 	return xtime_cache.tv_sec;
565 }
566 EXPORT_SYMBOL(get_seconds);
567 
568 
569 struct timespec current_kernel_time(void)
570 {
571 	struct timespec now;
572 	unsigned long seq;
573 
574 	do {
575 		seq = read_seqbegin(&xtime_lock);
576 
577 		now = xtime_cache;
578 	} while (read_seqretry(&xtime_lock, seq));
579 
580 	return now;
581 }
582 EXPORT_SYMBOL(current_kernel_time);
583