timekeeping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/percpu.h>
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/sysdev.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/time.h>
  19. #include <linux/tick.h>
  20. /*
  21. * This read-write spinlock protects us from races in SMP while
  22. * playing with xtime and avenrun.
  23. */
  24. __attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  25. EXPORT_SYMBOL(xtime_lock);
  26. /*
  27. * The current time
  28. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  29. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  30. * at zero at system boot time, so wall_to_monotonic will be negative,
  31. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  32. * the usual normalization.
  33. *
  34. * wall_to_monotonic is moved after resume from suspend for the monotonic
  35. * time not to jump. We need to add total_sleep_time to wall_to_monotonic
  36. * to get the real boot based time offset.
  37. *
  38. * - wall_to_monotonic is no longer the boot time, getboottime must be
  39. * used instead.
  40. */
  41. struct timespec xtime __attribute__ ((aligned (16)));
  42. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  43. static unsigned long total_sleep_time; /* seconds */
  44. EXPORT_SYMBOL(xtime);
  45. #ifdef CONFIG_NO_HZ
  46. static struct timespec xtime_cache __attribute__ ((aligned (16)));
  47. static inline void update_xtime_cache(u64 nsec)
  48. {
  49. xtime_cache = xtime;
  50. timespec_add_ns(&xtime_cache, nsec);
  51. }
  52. #else
  53. #define xtime_cache xtime
  54. /* We do *not* want to evaluate the argument for this case */
  55. #define update_xtime_cache(n) do { } while (0)
  56. #endif
  57. static struct clocksource *clock; /* pointer to current clocksource */
  58. #ifdef CONFIG_GENERIC_TIME
  59. /**
  60. * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
  61. *
  62. * private function, must hold xtime_lock lock when being
  63. * called. Returns the number of nanoseconds since the
  64. * last call to update_wall_time() (adjusted by NTP scaling)
  65. */
  66. static inline s64 __get_nsec_offset(void)
  67. {
  68. cycle_t cycle_now, cycle_delta;
  69. s64 ns_offset;
  70. /* read clocksource: */
  71. cycle_now = clocksource_read(clock);
  72. /* calculate the delta since the last update_wall_time: */
  73. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  74. /* convert to nanoseconds: */
  75. ns_offset = cyc2ns(clock, cycle_delta);
  76. return ns_offset;
  77. }
  78. /**
  79. * __get_realtime_clock_ts - Returns the time of day in a timespec
  80. * @ts: pointer to the timespec to be set
  81. *
  82. * Returns the time of day in a timespec. Used by
  83. * do_gettimeofday() and get_realtime_clock_ts().
  84. */
  85. static inline void __get_realtime_clock_ts(struct timespec *ts)
  86. {
  87. unsigned long seq;
  88. s64 nsecs;
  89. do {
  90. seq = read_seqbegin(&xtime_lock);
  91. *ts = xtime;
  92. nsecs = __get_nsec_offset();
  93. } while (read_seqretry(&xtime_lock, seq));
  94. timespec_add_ns(ts, nsecs);
  95. }
  96. /**
  97. * getnstimeofday - Returns the time of day in a timespec
  98. * @ts: pointer to the timespec to be set
  99. *
  100. * Returns the time of day in a timespec.
  101. */
  102. void getnstimeofday(struct timespec *ts)
  103. {
  104. __get_realtime_clock_ts(ts);
  105. }
  106. EXPORT_SYMBOL(getnstimeofday);
  107. /**
  108. * do_gettimeofday - Returns the time of day in a timeval
  109. * @tv: pointer to the timeval to be set
  110. *
  111. * NOTE: Users should be converted to using get_realtime_clock_ts()
  112. */
  113. void do_gettimeofday(struct timeval *tv)
  114. {
  115. struct timespec now;
  116. __get_realtime_clock_ts(&now);
  117. tv->tv_sec = now.tv_sec;
  118. tv->tv_usec = now.tv_nsec/1000;
  119. }
  120. EXPORT_SYMBOL(do_gettimeofday);
  121. /**
  122. * do_settimeofday - Sets the time of day
  123. * @tv: pointer to the timespec variable containing the new time
  124. *
  125. * Sets the time of day to the new time and update NTP and notify hrtimers
  126. */
  127. int do_settimeofday(struct timespec *tv)
  128. {
  129. unsigned long flags;
  130. time_t wtm_sec, sec = tv->tv_sec;
  131. long wtm_nsec, nsec = tv->tv_nsec;
  132. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  133. return -EINVAL;
  134. write_seqlock_irqsave(&xtime_lock, flags);
  135. nsec -= __get_nsec_offset();
  136. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  137. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  138. set_normalized_timespec(&xtime, sec, nsec);
  139. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  140. clock->error = 0;
  141. ntp_clear();
  142. update_vsyscall(&xtime, clock);
  143. write_sequnlock_irqrestore(&xtime_lock, flags);
  144. /* signal hrtimers about time change */
  145. clock_was_set();
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(do_settimeofday);
  149. /**
  150. * change_clocksource - Swaps clocksources if a new one is available
  151. *
  152. * Accumulates current time interval and initializes new clocksource
  153. */
  154. static void change_clocksource(void)
  155. {
  156. struct clocksource *new;
  157. cycle_t now;
  158. u64 nsec;
  159. new = clocksource_get_next();
  160. if (clock == new)
  161. return;
  162. now = clocksource_read(new);
  163. nsec = __get_nsec_offset();
  164. timespec_add_ns(&xtime, nsec);
  165. clock = new;
  166. clock->cycle_last = now;
  167. clock->error = 0;
  168. clock->xtime_nsec = 0;
  169. clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
  170. tick_clock_notify();
  171. printk(KERN_INFO "Time: %s clocksource has been installed.\n",
  172. clock->name);
  173. }
  174. #else
  175. static inline void change_clocksource(void) { }
  176. static inline s64 __get_nsec_offset(void) { return 0; }
  177. #endif
  178. /**
  179. * timekeeping_is_continuous - check to see if timekeeping is free running
  180. */
  181. int timekeeping_is_continuous(void)
  182. {
  183. unsigned long seq;
  184. int ret;
  185. do {
  186. seq = read_seqbegin(&xtime_lock);
  187. ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  188. } while (read_seqretry(&xtime_lock, seq));
  189. return ret;
  190. }
  191. /**
  192. * read_persistent_clock - Return time in seconds from the persistent clock.
  193. *
  194. * Weak dummy function for arches that do not yet support it.
  195. * Returns seconds from epoch using the battery backed persistent clock.
  196. * Returns zero if unsupported.
  197. *
  198. * XXX - Do be sure to remove it once all arches implement it.
  199. */
  200. unsigned long __attribute__((weak)) read_persistent_clock(void)
  201. {
  202. return 0;
  203. }
  204. /*
  205. * timekeeping_init - Initializes the clocksource and common timekeeping values
  206. */
  207. void __init timekeeping_init(void)
  208. {
  209. unsigned long flags;
  210. unsigned long sec = read_persistent_clock();
  211. write_seqlock_irqsave(&xtime_lock, flags);
  212. ntp_clear();
  213. clock = clocksource_get_next();
  214. clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
  215. clock->cycle_last = clocksource_read(clock);
  216. xtime.tv_sec = sec;
  217. xtime.tv_nsec = 0;
  218. set_normalized_timespec(&wall_to_monotonic,
  219. -xtime.tv_sec, -xtime.tv_nsec);
  220. total_sleep_time = 0;
  221. write_sequnlock_irqrestore(&xtime_lock, flags);
  222. }
  223. /* flag for if timekeeping is suspended */
  224. static int timekeeping_suspended;
  225. /* time in seconds when suspend began */
  226. static unsigned long timekeeping_suspend_time;
  227. /* xtime offset when we went into suspend */
  228. static s64 timekeeping_suspend_nsecs;
  229. /**
  230. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  231. * @dev: unused
  232. *
  233. * This is for the generic clocksource timekeeping.
  234. * xtime/wall_to_monotonic/jiffies/etc are
  235. * still managed by arch specific suspend/resume code.
  236. */
  237. static int timekeeping_resume(struct sys_device *dev)
  238. {
  239. unsigned long flags;
  240. unsigned long now = read_persistent_clock();
  241. clocksource_resume();
  242. write_seqlock_irqsave(&xtime_lock, flags);
  243. if (now && (now > timekeeping_suspend_time)) {
  244. unsigned long sleep_length = now - timekeeping_suspend_time;
  245. xtime.tv_sec += sleep_length;
  246. wall_to_monotonic.tv_sec -= sleep_length;
  247. total_sleep_time += sleep_length;
  248. }
  249. /* Make sure that we have the correct xtime reference */
  250. timespec_add_ns(&xtime, timekeeping_suspend_nsecs);
  251. /* re-base the last cycle value */
  252. clock->cycle_last = clocksource_read(clock);
  253. clock->error = 0;
  254. timekeeping_suspended = 0;
  255. write_sequnlock_irqrestore(&xtime_lock, flags);
  256. touch_softlockup_watchdog();
  257. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  258. /* Resume hrtimers */
  259. hres_timers_resume();
  260. return 0;
  261. }
  262. static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
  263. {
  264. unsigned long flags;
  265. timekeeping_suspend_time = read_persistent_clock();
  266. write_seqlock_irqsave(&xtime_lock, flags);
  267. /* Get the current xtime offset */
  268. timekeeping_suspend_nsecs = __get_nsec_offset();
  269. timekeeping_suspended = 1;
  270. write_sequnlock_irqrestore(&xtime_lock, flags);
  271. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  272. return 0;
  273. }
  274. /* sysfs resume/suspend bits for timekeeping */
  275. static struct sysdev_class timekeeping_sysclass = {
  276. .resume = timekeeping_resume,
  277. .suspend = timekeeping_suspend,
  278. set_kset_name("timekeeping"),
  279. };
  280. static struct sys_device device_timer = {
  281. .id = 0,
  282. .cls = &timekeeping_sysclass,
  283. };
  284. static int __init timekeeping_init_device(void)
  285. {
  286. int error = sysdev_class_register(&timekeeping_sysclass);
  287. if (!error)
  288. error = sysdev_register(&device_timer);
  289. return error;
  290. }
  291. device_initcall(timekeeping_init_device);
  292. /*
  293. * If the error is already larger, we look ahead even further
  294. * to compensate for late or lost adjustments.
  295. */
  296. static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
  297. s64 *offset)
  298. {
  299. s64 tick_error, i;
  300. u32 look_ahead, adj;
  301. s32 error2, mult;
  302. /*
  303. * Use the current error value to determine how much to look ahead.
  304. * The larger the error the slower we adjust for it to avoid problems
  305. * with losing too many ticks, otherwise we would overadjust and
  306. * produce an even larger error. The smaller the adjustment the
  307. * faster we try to adjust for it, as lost ticks can do less harm
  308. * here. This is tuned so that an error of about 1 msec is adusted
  309. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  310. */
  311. error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
  312. error2 = abs(error2);
  313. for (look_ahead = 0; error2 > 0; look_ahead++)
  314. error2 >>= 2;
  315. /*
  316. * Now calculate the error in (1 << look_ahead) ticks, but first
  317. * remove the single look ahead already included in the error.
  318. */
  319. tick_error = current_tick_length() >>
  320. (TICK_LENGTH_SHIFT - clock->shift + 1);
  321. tick_error -= clock->xtime_interval >> 1;
  322. error = ((error - tick_error) >> look_ahead) + tick_error;
  323. /* Finally calculate the adjustment shift value. */
  324. i = *interval;
  325. mult = 1;
  326. if (error < 0) {
  327. error = -error;
  328. *interval = -*interval;
  329. *offset = -*offset;
  330. mult = -1;
  331. }
  332. for (adj = 0; error > i; adj++)
  333. error >>= 1;
  334. *interval <<= adj;
  335. *offset <<= adj;
  336. return mult << adj;
  337. }
  338. /*
  339. * Adjust the multiplier to reduce the error value,
  340. * this is optimized for the most common adjustments of -1,0,1,
  341. * for other values we can do a bit more work.
  342. */
  343. static void clocksource_adjust(s64 offset)
  344. {
  345. s64 error, interval = clock->cycle_interval;
  346. int adj;
  347. error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
  348. if (error > interval) {
  349. error >>= 2;
  350. if (likely(error <= interval))
  351. adj = 1;
  352. else
  353. adj = clocksource_bigadjust(error, &interval, &offset);
  354. } else if (error < -interval) {
  355. error >>= 2;
  356. if (likely(error >= -interval)) {
  357. adj = -1;
  358. interval = -interval;
  359. offset = -offset;
  360. } else
  361. adj = clocksource_bigadjust(error, &interval, &offset);
  362. } else
  363. return;
  364. clock->mult += adj;
  365. clock->xtime_interval += interval;
  366. clock->xtime_nsec -= offset;
  367. clock->error -= (interval - offset) <<
  368. (TICK_LENGTH_SHIFT - clock->shift);
  369. }
  370. /**
  371. * update_wall_time - Uses the current clocksource to increment the wall time
  372. *
  373. * Called from the timer interrupt, must hold a write on xtime_lock.
  374. */
  375. void update_wall_time(void)
  376. {
  377. cycle_t offset;
  378. /* Make sure we're fully resumed: */
  379. if (unlikely(timekeeping_suspended))
  380. return;
  381. #ifdef CONFIG_GENERIC_TIME
  382. offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
  383. #else
  384. offset = clock->cycle_interval;
  385. #endif
  386. clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
  387. /* normally this loop will run just once, however in the
  388. * case of lost or late ticks, it will accumulate correctly.
  389. */
  390. while (offset >= clock->cycle_interval) {
  391. /* accumulate one interval */
  392. clock->xtime_nsec += clock->xtime_interval;
  393. clock->cycle_last += clock->cycle_interval;
  394. offset -= clock->cycle_interval;
  395. if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
  396. clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
  397. xtime.tv_sec++;
  398. second_overflow();
  399. }
  400. /* accumulate error between NTP and clock interval */
  401. clock->error += current_tick_length();
  402. clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
  403. }
  404. /* correct the clock when NTP error is too big */
  405. clocksource_adjust(offset);
  406. /* store full nanoseconds into xtime */
  407. xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
  408. clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
  409. update_xtime_cache(cyc2ns(clock, offset));
  410. /* check to see if there is a new clocksource to use */
  411. change_clocksource();
  412. update_vsyscall(&xtime, clock);
  413. }
  414. /**
  415. * getboottime - Return the real time of system boot.
  416. * @ts: pointer to the timespec to be set
  417. *
  418. * Returns the time of day in a timespec.
  419. *
  420. * This is based on the wall_to_monotonic offset and the total suspend
  421. * time. Calls to settimeofday will affect the value returned (which
  422. * basically means that however wrong your real time clock is at boot time,
  423. * you get the right time here).
  424. */
  425. void getboottime(struct timespec *ts)
  426. {
  427. set_normalized_timespec(ts,
  428. - (wall_to_monotonic.tv_sec + total_sleep_time),
  429. - wall_to_monotonic.tv_nsec);
  430. }
  431. /**
  432. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  433. * @ts: pointer to the timespec to be converted
  434. */
  435. void monotonic_to_bootbased(struct timespec *ts)
  436. {
  437. ts->tv_sec += total_sleep_time;
  438. }
  439. unsigned long get_seconds(void)
  440. {
  441. return xtime_cache.tv_sec;
  442. }
  443. EXPORT_SYMBOL(get_seconds);
  444. struct timespec current_kernel_time(void)
  445. {
  446. struct timespec now;
  447. unsigned long seq;
  448. do {
  449. seq = read_seqbegin(&xtime_lock);
  450. now = xtime_cache;
  451. } while (read_seqretry(&xtime_lock, seq));
  452. return now;
  453. }
  454. EXPORT_SYMBOL(current_kernel_time);