timekeeping.c 12 KB

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