timekeeping.c 15 KB

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