• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/kernel/time/
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/sched.h>
17#include <linux/sysdev.h>
18#include <linux/clocksource.h>
19#include <linux/jiffies.h>
20#include <linux/time.h>
21#include <linux/tick.h>
22#include <linux/stop_machine.h>
23
24/* Structure holding internal timekeeping values. */
25struct timekeeper {
26	/* Current clocksource used for timekeeping. */
27	struct clocksource *clock;
28	/* The shift value of the current clocksource. */
29	int	shift;
30
31	/* Number of clock cycles in one NTP interval. */
32	cycle_t cycle_interval;
33	/* Number of clock shifted nano seconds in one NTP interval. */
34	u64	xtime_interval;
35	/* Raw nano seconds accumulated per NTP interval. */
36	u32	raw_interval;
37
38	/* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
39	u64	xtime_nsec;
40	/* Difference between accumulated time and NTP time in ntp
41	 * shifted nano seconds. */
42	s64	ntp_error;
43	/* Shift conversion between clock shifted nano seconds and
44	 * ntp shifted nano seconds. */
45	int	ntp_error_shift;
46	/* NTP adjusted clock multiplier */
47	u32	mult;
48};
49
50struct timekeeper timekeeper;
51
52/**
53 * timekeeper_setup_internals - Set up internals to use clocksource clock.
54 *
55 * @clock:		Pointer to clocksource.
56 *
57 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
58 * pair and interval request.
59 *
60 * Unless you're the timekeeping code, you should not be using this!
61 */
62static void timekeeper_setup_internals(struct clocksource *clock)
63{
64	cycle_t interval;
65	u64 tmp;
66
67	timekeeper.clock = clock;
68	clock->cycle_last = clock->read(clock);
69
70	/* Do the ns -> cycle conversion first, using original mult */
71	tmp = NTP_INTERVAL_LENGTH;
72	tmp <<= clock->shift;
73	tmp += clock->mult/2;
74	do_div(tmp, clock->mult);
75	if (tmp == 0)
76		tmp = 1;
77
78	interval = (cycle_t) tmp;
79	timekeeper.cycle_interval = interval;
80
81	/* Go back from cycles -> shifted ns */
82	timekeeper.xtime_interval = (u64) interval * clock->mult;
83	timekeeper.raw_interval =
84		((u64) interval * clock->mult) >> clock->shift;
85
86	timekeeper.xtime_nsec = 0;
87	timekeeper.shift = clock->shift;
88
89	timekeeper.ntp_error = 0;
90	timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
91
92	/*
93	 * The timekeeper keeps its own mult values for the currently
94	 * active clocksource. These value will be adjusted via NTP
95	 * to counteract clock drifting.
96	 */
97	timekeeper.mult = clock->mult;
98}
99
100/* Timekeeper helper functions. */
101static inline s64 timekeeping_get_ns(void)
102{
103	cycle_t cycle_now, cycle_delta;
104	struct clocksource *clock;
105
106	/* read clocksource: */
107	clock = timekeeper.clock;
108	cycle_now = clock->read(clock);
109
110	/* calculate the delta since the last update_wall_time: */
111	cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
112
113	/* return delta convert to nanoseconds using ntp adjusted mult. */
114	return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
115				  timekeeper.shift);
116}
117
118static inline s64 timekeeping_get_ns_raw(void)
119{
120	cycle_t cycle_now, cycle_delta;
121	struct clocksource *clock;
122
123	/* read clocksource: */
124	clock = timekeeper.clock;
125	cycle_now = clock->read(clock);
126
127	/* calculate the delta since the last update_wall_time: */
128	cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
129
130	/* return delta convert to nanoseconds using ntp adjusted mult. */
131	return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
132}
133
134/*
135 * This read-write spinlock protects us from races in SMP while
136 * playing with xtime.
137 */
138__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
139
140
141/*
142 * The current time
143 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
144 * for sub jiffie times) to get to monotonic time.  Monotonic is pegged
145 * at zero at system boot time, so wall_to_monotonic will be negative,
146 * however, we will ALWAYS keep the tv_nsec part positive so we can use
147 * the usual normalization.
148 *
149 * wall_to_monotonic is moved after resume from suspend for the monotonic
150 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
151 * to get the real boot based time offset.
152 *
153 * - wall_to_monotonic is no longer the boot time, getboottime must be
154 * used instead.
155 */
156struct timespec xtime __attribute__ ((aligned (16)));
157static struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
158static struct timespec total_sleep_time;
159/*Foxconn modify start by Hank 08/10/2012 */
160EXPORT_SYMBOL(xtime);
161/*Foxconn modify end by Hank 08/10/2012 */
162
163/*
164 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
165 */
166struct timespec raw_time;
167
168/* flag for if timekeeping is suspended */
169int __read_mostly timekeeping_suspended;
170
171/* must hold xtime_lock */
172void timekeeping_leap_insert(int leapsecond)
173{
174	xtime.tv_sec += leapsecond;
175	wall_to_monotonic.tv_sec -= leapsecond;
176	update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
177			timekeeper.mult);
178}
179
180/**
181 * timekeeping_forward_now - update clock to the current time
182 *
183 * Forward the current clock to update its state since the last call to
184 * update_wall_time(). This is useful before significant clock changes,
185 * as it avoids having to deal with this time offset explicitly.
186 */
187static void timekeeping_forward_now(void)
188{
189	cycle_t cycle_now, cycle_delta;
190	struct clocksource *clock;
191	s64 nsec;
192
193	clock = timekeeper.clock;
194	cycle_now = clock->read(clock);
195	cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
196	clock->cycle_last = cycle_now;
197
198	nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
199				  timekeeper.shift);
200
201	/* If arch requires, add in gettimeoffset() */
202	nsec += arch_gettimeoffset();
203
204	timespec_add_ns(&xtime, nsec);
205
206	nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
207	timespec_add_ns(&raw_time, nsec);
208}
209
210/**
211 * getnstimeofday - Returns the time of day in a timespec
212 * @ts:		pointer to the timespec to be set
213 *
214 * Returns the time of day in a timespec.
215 */
216void getnstimeofday(struct timespec *ts)
217{
218	unsigned long seq;
219	s64 nsecs;
220
221	WARN_ON(timekeeping_suspended);
222
223	do {
224		seq = read_seqbegin(&xtime_lock);
225
226		*ts = xtime;
227		nsecs = timekeeping_get_ns();
228
229		/* If arch requires, add in gettimeoffset() */
230		nsecs += arch_gettimeoffset();
231
232	} while (read_seqretry(&xtime_lock, seq));
233
234	timespec_add_ns(ts, nsecs);
235}
236
237EXPORT_SYMBOL(getnstimeofday);
238
239ktime_t ktime_get(void)
240{
241	unsigned int seq;
242	s64 secs, nsecs;
243
244	WARN_ON(timekeeping_suspended);
245
246	do {
247		seq = read_seqbegin(&xtime_lock);
248		secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
249		nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
250		nsecs += timekeeping_get_ns();
251
252	} while (read_seqretry(&xtime_lock, seq));
253	/*
254	 * Use ktime_set/ktime_add_ns to create a proper ktime on
255	 * 32-bit architectures without CONFIG_KTIME_SCALAR.
256	 */
257	return ktime_add_ns(ktime_set(secs, 0), nsecs);
258}
259EXPORT_SYMBOL_GPL(ktime_get);
260
261/**
262 * ktime_get_ts - get the monotonic clock in timespec format
263 * @ts:		pointer to timespec variable
264 *
265 * The function calculates the monotonic clock from the realtime
266 * clock and the wall_to_monotonic offset and stores the result
267 * in normalized timespec format in the variable pointed to by @ts.
268 */
269void ktime_get_ts(struct timespec *ts)
270{
271	struct timespec tomono;
272	unsigned int seq;
273	s64 nsecs;
274
275	WARN_ON(timekeeping_suspended);
276
277	do {
278		seq = read_seqbegin(&xtime_lock);
279		*ts = xtime;
280		tomono = wall_to_monotonic;
281		nsecs = timekeeping_get_ns();
282
283	} while (read_seqretry(&xtime_lock, seq));
284
285	set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
286				ts->tv_nsec + tomono.tv_nsec + nsecs);
287}
288EXPORT_SYMBOL_GPL(ktime_get_ts);
289
290/**
291 * do_gettimeofday - Returns the time of day in a timeval
292 * @tv:		pointer to the timeval to be set
293 *
294 * NOTE: Users should be converted to using getnstimeofday()
295 */
296void do_gettimeofday(struct timeval *tv)
297{
298	struct timespec now;
299
300	getnstimeofday(&now);
301	tv->tv_sec = now.tv_sec;
302	tv->tv_usec = now.tv_nsec/1000;
303}
304
305EXPORT_SYMBOL(do_gettimeofday);
306/**
307 * do_settimeofday - Sets the time of day
308 * @tv:		pointer to the timespec variable containing the new time
309 *
310 * Sets the time of day to the new time and update NTP and notify hrtimers
311 */
312int do_settimeofday(struct timespec *tv)
313{
314	struct timespec ts_delta;
315	unsigned long flags;
316
317	if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
318		return -EINVAL;
319
320	write_seqlock_irqsave(&xtime_lock, flags);
321
322	timekeeping_forward_now();
323
324	ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
325	ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
326	wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
327
328	xtime = *tv;
329
330	timekeeper.ntp_error = 0;
331	ntp_clear();
332
333	update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
334				timekeeper.mult);
335
336	write_sequnlock_irqrestore(&xtime_lock, flags);
337
338	/* signal hrtimers about time change */
339	clock_was_set();
340
341	return 0;
342}
343
344EXPORT_SYMBOL(do_settimeofday);
345
346/**
347 * change_clocksource - Swaps clocksources if a new one is available
348 *
349 * Accumulates current time interval and initializes new clocksource
350 */
351static int change_clocksource(void *data)
352{
353	struct clocksource *new, *old;
354
355	new = (struct clocksource *) data;
356
357	timekeeping_forward_now();
358	if (!new->enable || new->enable(new) == 0) {
359		old = timekeeper.clock;
360		timekeeper_setup_internals(new);
361		if (old->disable)
362			old->disable(old);
363	}
364	return 0;
365}
366
367/**
368 * timekeeping_notify - Install a new clock source
369 * @clock:		pointer to the clock source
370 *
371 * This function is called from clocksource.c after a new, better clock
372 * source has been registered. The caller holds the clocksource_mutex.
373 */
374void timekeeping_notify(struct clocksource *clock)
375{
376	if (timekeeper.clock == clock)
377		return;
378	stop_machine(change_clocksource, clock, NULL);
379	tick_clock_notify();
380}
381
382/**
383 * ktime_get_real - get the real (wall-) time in ktime_t format
384 *
385 * returns the time in ktime_t format
386 */
387ktime_t ktime_get_real(void)
388{
389	struct timespec now;
390
391	getnstimeofday(&now);
392
393	return timespec_to_ktime(now);
394}
395EXPORT_SYMBOL_GPL(ktime_get_real);
396
397/**
398 * getrawmonotonic - Returns the raw monotonic time in a timespec
399 * @ts:		pointer to the timespec to be set
400 *
401 * Returns the raw monotonic time (completely un-modified by ntp)
402 */
403void getrawmonotonic(struct timespec *ts)
404{
405	unsigned long seq;
406	s64 nsecs;
407
408	do {
409		seq = read_seqbegin(&xtime_lock);
410		nsecs = timekeeping_get_ns_raw();
411		*ts = raw_time;
412
413	} while (read_seqretry(&xtime_lock, seq));
414
415	timespec_add_ns(ts, nsecs);
416}
417EXPORT_SYMBOL(getrawmonotonic);
418
419
420/**
421 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
422 */
423int timekeeping_valid_for_hres(void)
424{
425	unsigned long seq;
426	int ret;
427
428	do {
429		seq = read_seqbegin(&xtime_lock);
430
431		ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
432
433	} while (read_seqretry(&xtime_lock, seq));
434
435	return ret;
436}
437
438/**
439 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
440 *
441 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
442 * ensure that the clocksource does not change!
443 */
444u64 timekeeping_max_deferment(void)
445{
446	return timekeeper.clock->max_idle_ns;
447}
448
449void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
450{
451	ts->tv_sec = 0;
452	ts->tv_nsec = 0;
453}
454
455void __attribute__((weak)) read_boot_clock(struct timespec *ts)
456{
457	ts->tv_sec = 0;
458	ts->tv_nsec = 0;
459}
460
461/*
462 * timekeeping_init - Initializes the clocksource and common timekeeping values
463 */
464void __init timekeeping_init(void)
465{
466	struct clocksource *clock;
467	unsigned long flags;
468	struct timespec now, boot;
469
470	read_persistent_clock(&now);
471	read_boot_clock(&boot);
472
473	write_seqlock_irqsave(&xtime_lock, flags);
474
475	ntp_init();
476
477	clock = clocksource_default_clock();
478	if (clock->enable)
479		clock->enable(clock);
480	timekeeper_setup_internals(clock);
481
482	xtime.tv_sec = now.tv_sec;
483	xtime.tv_nsec = now.tv_nsec;
484	raw_time.tv_sec = 0;
485	raw_time.tv_nsec = 0;
486	if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
487		boot.tv_sec = xtime.tv_sec;
488		boot.tv_nsec = xtime.tv_nsec;
489	}
490	set_normalized_timespec(&wall_to_monotonic,
491				-boot.tv_sec, -boot.tv_nsec);
492	total_sleep_time.tv_sec = 0;
493	total_sleep_time.tv_nsec = 0;
494	write_sequnlock_irqrestore(&xtime_lock, flags);
495}
496
497/* time in seconds when suspend began */
498static struct timespec timekeeping_suspend_time;
499
500/**
501 * timekeeping_resume - Resumes the generic timekeeping subsystem.
502 * @dev:	unused
503 *
504 * This is for the generic clocksource timekeeping.
505 * xtime/wall_to_monotonic/jiffies/etc are
506 * still managed by arch specific suspend/resume code.
507 */
508static int timekeeping_resume(struct sys_device *dev)
509{
510	unsigned long flags;
511	struct timespec ts;
512
513	read_persistent_clock(&ts);
514
515	clocksource_resume();
516
517	write_seqlock_irqsave(&xtime_lock, flags);
518
519	if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
520		ts = timespec_sub(ts, timekeeping_suspend_time);
521		xtime = timespec_add(xtime, ts);
522		wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
523		total_sleep_time = timespec_add(total_sleep_time, ts);
524	}
525	/* re-base the last cycle value */
526	timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
527	timekeeper.ntp_error = 0;
528	timekeeping_suspended = 0;
529	write_sequnlock_irqrestore(&xtime_lock, flags);
530
531	touch_softlockup_watchdog();
532
533	clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
534
535	/* Resume hrtimers */
536	hres_timers_resume();
537
538	return 0;
539}
540
541static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
542{
543	unsigned long flags;
544
545	read_persistent_clock(&timekeeping_suspend_time);
546
547	write_seqlock_irqsave(&xtime_lock, flags);
548	timekeeping_forward_now();
549	timekeeping_suspended = 1;
550	write_sequnlock_irqrestore(&xtime_lock, flags);
551
552	clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
553	clocksource_suspend();
554
555	return 0;
556}
557
558/* sysfs resume/suspend bits for timekeeping */
559static struct sysdev_class timekeeping_sysclass = {
560	.name		= "timekeeping",
561	.resume		= timekeeping_resume,
562	.suspend	= timekeeping_suspend,
563};
564
565static struct sys_device device_timer = {
566	.id		= 0,
567	.cls		= &timekeeping_sysclass,
568};
569
570static int __init timekeeping_init_device(void)
571{
572	int error = sysdev_class_register(&timekeeping_sysclass);
573	if (!error)
574		error = sysdev_register(&device_timer);
575	return error;
576}
577
578device_initcall(timekeeping_init_device);
579
580/*
581 * If the error is already larger, we look ahead even further
582 * to compensate for late or lost adjustments.
583 */
584static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
585						 s64 *offset)
586{
587	s64 tick_error, i;
588	u32 look_ahead, adj;
589	s32 error2, mult;
590
591	/*
592	 * Use the current error value to determine how much to look ahead.
593	 * The larger the error the slower we adjust for it to avoid problems
594	 * with losing too many ticks, otherwise we would overadjust and
595	 * produce an even larger error.  The smaller the adjustment the
596	 * faster we try to adjust for it, as lost ticks can do less harm
597	 * here.  This is tuned so that an error of about 1 msec is adjusted
598	 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
599	 */
600	error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
601	error2 = abs(error2);
602	for (look_ahead = 0; error2 > 0; look_ahead++)
603		error2 >>= 2;
604
605	/*
606	 * Now calculate the error in (1 << look_ahead) ticks, but first
607	 * remove the single look ahead already included in the error.
608	 */
609	tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
610	tick_error -= timekeeper.xtime_interval >> 1;
611	error = ((error - tick_error) >> look_ahead) + tick_error;
612
613	/* Finally calculate the adjustment shift value.  */
614	i = *interval;
615	mult = 1;
616	if (error < 0) {
617		error = -error;
618		*interval = -*interval;
619		*offset = -*offset;
620		mult = -1;
621	}
622	for (adj = 0; error > i; adj++)
623		error >>= 1;
624
625	*interval <<= adj;
626	*offset <<= adj;
627	return mult << adj;
628}
629
630/*
631 * Adjust the multiplier to reduce the error value,
632 * this is optimized for the most common adjustments of -1,0,1,
633 * for other values we can do a bit more work.
634 */
635static void timekeeping_adjust(s64 offset)
636{
637	s64 error, interval = timekeeper.cycle_interval;
638	int adj;
639
640	error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
641	if (error > interval) {
642		error >>= 2;
643		if (likely(error <= interval))
644			adj = 1;
645		else
646			adj = timekeeping_bigadjust(error, &interval, &offset);
647	} else if (error < -interval) {
648		error >>= 2;
649		if (likely(error >= -interval)) {
650			adj = -1;
651			interval = -interval;
652			offset = -offset;
653		} else
654			adj = timekeeping_bigadjust(error, &interval, &offset);
655	} else
656		return;
657
658	timekeeper.mult += adj;
659	timekeeper.xtime_interval += interval;
660	timekeeper.xtime_nsec -= offset;
661	timekeeper.ntp_error -= (interval - offset) <<
662				timekeeper.ntp_error_shift;
663}
664
665
666/**
667 * logarithmic_accumulation - shifted accumulation of cycles
668 *
669 * This functions accumulates a shifted interval of cycles into
670 * into a shifted interval nanoseconds. Allows for O(log) accumulation
671 * loop.
672 *
673 * Returns the unconsumed cycles.
674 */
675static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
676{
677	u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
678	u64 raw_nsecs;
679
680	/* If the offset is smaller then a shifted interval, do nothing */
681	if (offset < timekeeper.cycle_interval<<shift)
682		return offset;
683
684	/* Accumulate one shifted interval */
685	offset -= timekeeper.cycle_interval << shift;
686	timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
687
688	timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
689	while (timekeeper.xtime_nsec >= nsecps) {
690		timekeeper.xtime_nsec -= nsecps;
691		xtime.tv_sec++;
692		second_overflow();
693	}
694
695	/* Accumulate raw time */
696	raw_nsecs = timekeeper.raw_interval << shift;
697	raw_nsecs += raw_time.tv_nsec;
698	if (raw_nsecs >= NSEC_PER_SEC) {
699		u64 raw_secs = raw_nsecs;
700		raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
701		raw_time.tv_sec += raw_secs;
702	}
703	raw_time.tv_nsec = raw_nsecs;
704
705	/* Accumulate error between NTP and clock interval */
706	timekeeper.ntp_error += tick_length << shift;
707	timekeeper.ntp_error -= timekeeper.xtime_interval <<
708				(timekeeper.ntp_error_shift + shift);
709
710	return offset;
711}
712
713
714/**
715 * update_wall_time - Uses the current clocksource to increment the wall time
716 *
717 * Called from the timer interrupt, must hold a write on xtime_lock.
718 */
719void update_wall_time(void)
720{
721	struct clocksource *clock;
722	cycle_t offset;
723	int shift = 0, maxshift;
724
725	/* Make sure we're fully resumed: */
726	if (unlikely(timekeeping_suspended))
727		return;
728
729	clock = timekeeper.clock;
730
731#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
732	offset = timekeeper.cycle_interval;
733#else
734	offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
735#endif
736	timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
737
738	/*
739	 * With NO_HZ we may have to accumulate many cycle_intervals
740	 * (think "ticks") worth of time at once. To do this efficiently,
741	 * we calculate the largest doubling multiple of cycle_intervals
742	 * that is smaller then the offset. We then accumulate that
743	 * chunk in one go, and then try to consume the next smaller
744	 * doubled multiple.
745	 */
746	shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
747	shift = max(0, shift);
748	/* Bound shift to one less then what overflows tick_length */
749	maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
750	shift = min(shift, maxshift);
751	while (offset >= timekeeper.cycle_interval) {
752		offset = logarithmic_accumulation(offset, shift);
753		if(offset < timekeeper.cycle_interval<<shift)
754			shift--;
755	}
756
757	/* correct the clock when NTP error is too big */
758	timekeeping_adjust(offset);
759
760	/*
761	 * Since in the loop above, we accumulate any amount of time
762	 * in xtime_nsec over a second into xtime.tv_sec, its possible for
763	 * xtime_nsec to be fairly small after the loop. Further, if we're
764	 * slightly speeding the clocksource up in timekeeping_adjust(),
765	 * its possible the required corrective factor to xtime_nsec could
766	 * cause it to underflow.
767	 *
768	 * Now, we cannot simply roll the accumulated second back, since
769	 * the NTP subsystem has been notified via second_overflow. So
770	 * instead we push xtime_nsec forward by the amount we underflowed,
771	 * and add that amount into the error.
772	 *
773	 * We'll correct this error next time through this function, when
774	 * xtime_nsec is not as small.
775	 */
776	if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
777		s64 neg = -(s64)timekeeper.xtime_nsec;
778		timekeeper.xtime_nsec = 0;
779		timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
780	}
781
782
783	/*
784	 * Store full nanoseconds into xtime after rounding it up and
785	 * add the remainder to the error difference.
786	 */
787	xtime.tv_nsec =	((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
788	timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
789	timekeeper.ntp_error +=	timekeeper.xtime_nsec <<
790				timekeeper.ntp_error_shift;
791
792	/*
793	 * Finally, make sure that after the rounding
794	 * xtime.tv_nsec isn't larger then NSEC_PER_SEC
795	 */
796	if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
797		xtime.tv_nsec -= NSEC_PER_SEC;
798		xtime.tv_sec++;
799		second_overflow();
800	}
801
802	/* check to see if there is a new clocksource to use */
803	update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
804				timekeeper.mult);
805}
806
807/**
808 * getboottime - Return the real time of system boot.
809 * @ts:		pointer to the timespec to be set
810 *
811 * Returns the time of day in a timespec.
812 *
813 * This is based on the wall_to_monotonic offset and the total suspend
814 * time. Calls to settimeofday will affect the value returned (which
815 * basically means that however wrong your real time clock is at boot time,
816 * you get the right time here).
817 */
818void getboottime(struct timespec *ts)
819{
820	struct timespec boottime = {
821		.tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
822		.tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
823	};
824
825	set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
826}
827EXPORT_SYMBOL_GPL(getboottime);
828
829/**
830 * monotonic_to_bootbased - Convert the monotonic time to boot based.
831 * @ts:		pointer to the timespec to be converted
832 */
833void monotonic_to_bootbased(struct timespec *ts)
834{
835	*ts = timespec_add(*ts, total_sleep_time);
836}
837EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
838
839unsigned long get_seconds(void)
840{
841	return xtime.tv_sec;
842}
843EXPORT_SYMBOL(get_seconds);
844
845struct timespec __current_kernel_time(void)
846{
847	return xtime;
848}
849
850struct timespec __get_wall_to_monotonic(void)
851{
852	return wall_to_monotonic;
853}
854
855struct timespec current_kernel_time(void)
856{
857	struct timespec now;
858	unsigned long seq;
859
860	do {
861		seq = read_seqbegin(&xtime_lock);
862
863		now = xtime;
864	} while (read_seqretry(&xtime_lock, seq));
865
866	return now;
867}
868EXPORT_SYMBOL(current_kernel_time);
869
870struct timespec get_monotonic_coarse(void)
871{
872	struct timespec now, mono;
873	unsigned long seq;
874
875	do {
876		seq = read_seqbegin(&xtime_lock);
877
878		now = xtime;
879		mono = wall_to_monotonic;
880	} while (read_seqretry(&xtime_lock, seq));
881
882	set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
883				now.tv_nsec + mono.tv_nsec);
884	return now;
885}
886