timekeeping.c 13 KB

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