timekeeping.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. /* Structure holding internal timekeeping values. */
  21. struct timekeeper {
  22. /* Current clocksource used for timekeeping. */
  23. struct clocksource *clock;
  24. /* The shift value of the current clocksource. */
  25. int shift;
  26. /* Number of clock cycles in one NTP interval. */
  27. cycle_t cycle_interval;
  28. /* Number of clock shifted nano seconds in one NTP interval. */
  29. u64 xtime_interval;
  30. /* Raw nano seconds accumulated per NTP interval. */
  31. u32 raw_interval;
  32. /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
  33. u64 xtime_nsec;
  34. /* Difference between accumulated time and NTP time in ntp
  35. * shifted nano seconds. */
  36. s64 ntp_error;
  37. /* Shift conversion between clock shifted nano seconds and
  38. * ntp shifted nano seconds. */
  39. int ntp_error_shift;
  40. /* NTP adjusted clock multiplier */
  41. u32 mult;
  42. };
  43. struct timekeeper timekeeper;
  44. /**
  45. * timekeeper_setup_internals - Set up internals to use clocksource clock.
  46. *
  47. * @clock: Pointer to clocksource.
  48. *
  49. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  50. * pair and interval request.
  51. *
  52. * Unless you're the timekeeping code, you should not be using this!
  53. */
  54. static void timekeeper_setup_internals(struct clocksource *clock)
  55. {
  56. cycle_t interval;
  57. u64 tmp;
  58. timekeeper.clock = clock;
  59. clock->cycle_last = clock->read(clock);
  60. /* Do the ns -> cycle conversion first, using original mult */
  61. tmp = NTP_INTERVAL_LENGTH;
  62. tmp <<= clock->shift;
  63. tmp += clock->mult/2;
  64. do_div(tmp, clock->mult);
  65. if (tmp == 0)
  66. tmp = 1;
  67. interval = (cycle_t) tmp;
  68. timekeeper.cycle_interval = interval;
  69. /* Go back from cycles -> shifted ns */
  70. timekeeper.xtime_interval = (u64) interval * clock->mult;
  71. timekeeper.raw_interval =
  72. ((u64) interval * clock->mult) >> clock->shift;
  73. timekeeper.xtime_nsec = 0;
  74. timekeeper.shift = clock->shift;
  75. timekeeper.ntp_error = 0;
  76. timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  77. /*
  78. * The timekeeper keeps its own mult values for the currently
  79. * active clocksource. These value will be adjusted via NTP
  80. * to counteract clock drifting.
  81. */
  82. timekeeper.mult = clock->mult;
  83. }
  84. /* Timekeeper helper functions. */
  85. static inline s64 timekeeping_get_ns(void)
  86. {
  87. cycle_t cycle_now, cycle_delta;
  88. struct clocksource *clock;
  89. /* read clocksource: */
  90. clock = timekeeper.clock;
  91. cycle_now = clock->read(clock);
  92. /* calculate the delta since the last update_wall_time: */
  93. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  94. /* return delta convert to nanoseconds using ntp adjusted mult. */
  95. return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  96. timekeeper.shift);
  97. }
  98. static inline s64 timekeeping_get_ns_raw(void)
  99. {
  100. cycle_t cycle_now, cycle_delta;
  101. struct clocksource *clock;
  102. /* read clocksource: */
  103. clock = timekeeper.clock;
  104. cycle_now = clock->read(clock);
  105. /* calculate the delta since the last update_wall_time: */
  106. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  107. /* return delta convert to nanoseconds using ntp adjusted mult. */
  108. return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  109. }
  110. /*
  111. * This read-write spinlock protects us from races in SMP while
  112. * playing with xtime.
  113. */
  114. __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  115. /*
  116. * The current time
  117. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  118. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  119. * at zero at system boot time, so wall_to_monotonic will be negative,
  120. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  121. * the usual normalization.
  122. *
  123. * wall_to_monotonic is moved after resume from suspend for the monotonic
  124. * time not to jump. We need to add total_sleep_time to wall_to_monotonic
  125. * to get the real boot based time offset.
  126. *
  127. * - wall_to_monotonic is no longer the boot time, getboottime must be
  128. * used instead.
  129. */
  130. struct timespec xtime __attribute__ ((aligned (16)));
  131. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  132. static unsigned long total_sleep_time; /* seconds */
  133. /*
  134. * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
  135. */
  136. struct timespec raw_time;
  137. /* flag for if timekeeping is suspended */
  138. int __read_mostly timekeeping_suspended;
  139. static struct timespec xtime_cache __attribute__ ((aligned (16)));
  140. void update_xtime_cache(u64 nsec)
  141. {
  142. xtime_cache = xtime;
  143. timespec_add_ns(&xtime_cache, nsec);
  144. }
  145. /* must hold xtime_lock */
  146. void timekeeping_leap_insert(int leapsecond)
  147. {
  148. xtime.tv_sec += leapsecond;
  149. wall_to_monotonic.tv_sec -= leapsecond;
  150. update_vsyscall(&xtime, timekeeper.clock);
  151. }
  152. #ifdef CONFIG_GENERIC_TIME
  153. /**
  154. * timekeeping_forward_now - update clock to the current time
  155. *
  156. * Forward the current clock to update its state since the last call to
  157. * update_wall_time(). This is useful before significant clock changes,
  158. * as it avoids having to deal with this time offset explicitly.
  159. */
  160. static void timekeeping_forward_now(void)
  161. {
  162. cycle_t cycle_now, cycle_delta;
  163. struct clocksource *clock;
  164. s64 nsec;
  165. clock = timekeeper.clock;
  166. cycle_now = clock->read(clock);
  167. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  168. clock->cycle_last = cycle_now;
  169. nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  170. timekeeper.shift);
  171. /* If arch requires, add in gettimeoffset() */
  172. nsec += arch_gettimeoffset();
  173. timespec_add_ns(&xtime, nsec);
  174. nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  175. timespec_add_ns(&raw_time, nsec);
  176. }
  177. /**
  178. * getnstimeofday - Returns the time of day in a timespec
  179. * @ts: pointer to the timespec to be set
  180. *
  181. * Returns the time of day in a timespec.
  182. */
  183. void getnstimeofday(struct timespec *ts)
  184. {
  185. unsigned long seq;
  186. s64 nsecs;
  187. WARN_ON(timekeeping_suspended);
  188. do {
  189. seq = read_seqbegin(&xtime_lock);
  190. *ts = xtime;
  191. nsecs = timekeeping_get_ns();
  192. /* If arch requires, add in gettimeoffset() */
  193. nsecs += arch_gettimeoffset();
  194. } while (read_seqretry(&xtime_lock, seq));
  195. timespec_add_ns(ts, nsecs);
  196. }
  197. EXPORT_SYMBOL(getnstimeofday);
  198. ktime_t ktime_get(void)
  199. {
  200. unsigned int seq;
  201. s64 secs, nsecs;
  202. WARN_ON(timekeeping_suspended);
  203. do {
  204. seq = read_seqbegin(&xtime_lock);
  205. secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
  206. nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
  207. nsecs += timekeeping_get_ns();
  208. } while (read_seqretry(&xtime_lock, seq));
  209. /*
  210. * Use ktime_set/ktime_add_ns to create a proper ktime on
  211. * 32-bit architectures without CONFIG_KTIME_SCALAR.
  212. */
  213. return ktime_add_ns(ktime_set(secs, 0), nsecs);
  214. }
  215. EXPORT_SYMBOL_GPL(ktime_get);
  216. /**
  217. * ktime_get_ts - get the monotonic clock in timespec format
  218. * @ts: pointer to timespec variable
  219. *
  220. * The function calculates the monotonic clock from the realtime
  221. * clock and the wall_to_monotonic offset and stores the result
  222. * in normalized timespec format in the variable pointed to by @ts.
  223. */
  224. void ktime_get_ts(struct timespec *ts)
  225. {
  226. struct timespec tomono;
  227. unsigned int seq;
  228. s64 nsecs;
  229. WARN_ON(timekeeping_suspended);
  230. do {
  231. seq = read_seqbegin(&xtime_lock);
  232. *ts = xtime;
  233. tomono = wall_to_monotonic;
  234. nsecs = timekeeping_get_ns();
  235. } while (read_seqretry(&xtime_lock, seq));
  236. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  237. ts->tv_nsec + tomono.tv_nsec + nsecs);
  238. }
  239. EXPORT_SYMBOL_GPL(ktime_get_ts);
  240. /**
  241. * do_gettimeofday - Returns the time of day in a timeval
  242. * @tv: pointer to the timeval to be set
  243. *
  244. * NOTE: Users should be converted to using getnstimeofday()
  245. */
  246. void do_gettimeofday(struct timeval *tv)
  247. {
  248. struct timespec now;
  249. getnstimeofday(&now);
  250. tv->tv_sec = now.tv_sec;
  251. tv->tv_usec = now.tv_nsec/1000;
  252. }
  253. EXPORT_SYMBOL(do_gettimeofday);
  254. /**
  255. * do_settimeofday - Sets the time of day
  256. * @tv: pointer to the timespec variable containing the new time
  257. *
  258. * Sets the time of day to the new time and update NTP and notify hrtimers
  259. */
  260. int do_settimeofday(struct timespec *tv)
  261. {
  262. struct timespec ts_delta;
  263. unsigned long flags;
  264. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  265. return -EINVAL;
  266. write_seqlock_irqsave(&xtime_lock, flags);
  267. timekeeping_forward_now();
  268. ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
  269. ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
  270. wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
  271. xtime = *tv;
  272. update_xtime_cache(0);
  273. timekeeper.ntp_error = 0;
  274. ntp_clear();
  275. update_vsyscall(&xtime, timekeeper.clock);
  276. write_sequnlock_irqrestore(&xtime_lock, flags);
  277. /* signal hrtimers about time change */
  278. clock_was_set();
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(do_settimeofday);
  282. /**
  283. * change_clocksource - Swaps clocksources if a new one is available
  284. *
  285. * Accumulates current time interval and initializes new clocksource
  286. */
  287. static void change_clocksource(void)
  288. {
  289. struct clocksource *new, *old;
  290. new = clocksource_get_next();
  291. if (!new || timekeeper.clock == new)
  292. return;
  293. timekeeping_forward_now();
  294. if (new->enable && !new->enable(new))
  295. return;
  296. old = timekeeper.clock;
  297. timekeeper_setup_internals(new);
  298. if (old->disable)
  299. old->disable(old);
  300. tick_clock_notify();
  301. }
  302. #else /* GENERIC_TIME */
  303. static inline void timekeeping_forward_now(void) { }
  304. static inline void change_clocksource(void) { }
  305. /**
  306. * ktime_get - get the monotonic time in ktime_t format
  307. *
  308. * returns the time in ktime_t format
  309. */
  310. ktime_t ktime_get(void)
  311. {
  312. struct timespec now;
  313. ktime_get_ts(&now);
  314. return timespec_to_ktime(now);
  315. }
  316. EXPORT_SYMBOL_GPL(ktime_get);
  317. /**
  318. * ktime_get_ts - get the monotonic clock in timespec format
  319. * @ts: pointer to timespec variable
  320. *
  321. * The function calculates the monotonic clock from the realtime
  322. * clock and the wall_to_monotonic offset and stores the result
  323. * in normalized timespec format in the variable pointed to by @ts.
  324. */
  325. void ktime_get_ts(struct timespec *ts)
  326. {
  327. struct timespec tomono;
  328. unsigned long seq;
  329. do {
  330. seq = read_seqbegin(&xtime_lock);
  331. getnstimeofday(ts);
  332. tomono = wall_to_monotonic;
  333. } while (read_seqretry(&xtime_lock, seq));
  334. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  335. ts->tv_nsec + tomono.tv_nsec);
  336. }
  337. EXPORT_SYMBOL_GPL(ktime_get_ts);
  338. #endif /* !GENERIC_TIME */
  339. /**
  340. * ktime_get_real - get the real (wall-) time in ktime_t format
  341. *
  342. * returns the time in ktime_t format
  343. */
  344. ktime_t ktime_get_real(void)
  345. {
  346. struct timespec now;
  347. getnstimeofday(&now);
  348. return timespec_to_ktime(now);
  349. }
  350. EXPORT_SYMBOL_GPL(ktime_get_real);
  351. /**
  352. * getrawmonotonic - Returns the raw monotonic time in a timespec
  353. * @ts: pointer to the timespec to be set
  354. *
  355. * Returns the raw monotonic time (completely un-modified by ntp)
  356. */
  357. void getrawmonotonic(struct timespec *ts)
  358. {
  359. unsigned long seq;
  360. s64 nsecs;
  361. do {
  362. seq = read_seqbegin(&xtime_lock);
  363. nsecs = timekeeping_get_ns_raw();
  364. *ts = raw_time;
  365. } while (read_seqretry(&xtime_lock, seq));
  366. timespec_add_ns(ts, nsecs);
  367. }
  368. EXPORT_SYMBOL(getrawmonotonic);
  369. /**
  370. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  371. */
  372. int timekeeping_valid_for_hres(void)
  373. {
  374. unsigned long seq;
  375. int ret;
  376. do {
  377. seq = read_seqbegin(&xtime_lock);
  378. ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  379. } while (read_seqretry(&xtime_lock, seq));
  380. return ret;
  381. }
  382. /**
  383. * read_persistent_clock - Return time in seconds from the persistent clock.
  384. *
  385. * Weak dummy function for arches that do not yet support it.
  386. * Returns seconds from epoch using the battery backed persistent clock.
  387. * Returns zero if unsupported.
  388. *
  389. * XXX - Do be sure to remove it once all arches implement it.
  390. */
  391. unsigned long __attribute__((weak)) read_persistent_clock(void)
  392. {
  393. return 0;
  394. }
  395. /*
  396. * timekeeping_init - Initializes the clocksource and common timekeeping values
  397. */
  398. void __init timekeeping_init(void)
  399. {
  400. struct clocksource *clock;
  401. unsigned long flags;
  402. unsigned long sec = read_persistent_clock();
  403. write_seqlock_irqsave(&xtime_lock, flags);
  404. ntp_init();
  405. clock = clocksource_default_clock();
  406. if (clock->enable)
  407. clock->enable(clock);
  408. timekeeper_setup_internals(clock);
  409. xtime.tv_sec = sec;
  410. xtime.tv_nsec = 0;
  411. raw_time.tv_sec = 0;
  412. raw_time.tv_nsec = 0;
  413. set_normalized_timespec(&wall_to_monotonic,
  414. -xtime.tv_sec, -xtime.tv_nsec);
  415. update_xtime_cache(0);
  416. total_sleep_time = 0;
  417. write_sequnlock_irqrestore(&xtime_lock, flags);
  418. }
  419. /* time in seconds when suspend began */
  420. static unsigned long timekeeping_suspend_time;
  421. /**
  422. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  423. * @dev: unused
  424. *
  425. * This is for the generic clocksource timekeeping.
  426. * xtime/wall_to_monotonic/jiffies/etc are
  427. * still managed by arch specific suspend/resume code.
  428. */
  429. static int timekeeping_resume(struct sys_device *dev)
  430. {
  431. unsigned long flags;
  432. unsigned long now = read_persistent_clock();
  433. clocksource_resume();
  434. write_seqlock_irqsave(&xtime_lock, flags);
  435. if (now && (now > timekeeping_suspend_time)) {
  436. unsigned long sleep_length = now - timekeeping_suspend_time;
  437. xtime.tv_sec += sleep_length;
  438. wall_to_monotonic.tv_sec -= sleep_length;
  439. total_sleep_time += sleep_length;
  440. }
  441. update_xtime_cache(0);
  442. /* re-base the last cycle value */
  443. timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
  444. timekeeper.ntp_error = 0;
  445. timekeeping_suspended = 0;
  446. write_sequnlock_irqrestore(&xtime_lock, flags);
  447. touch_softlockup_watchdog();
  448. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  449. /* Resume hrtimers */
  450. hres_timers_resume();
  451. return 0;
  452. }
  453. static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
  454. {
  455. unsigned long flags;
  456. timekeeping_suspend_time = read_persistent_clock();
  457. write_seqlock_irqsave(&xtime_lock, flags);
  458. timekeeping_forward_now();
  459. timekeeping_suspended = 1;
  460. write_sequnlock_irqrestore(&xtime_lock, flags);
  461. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  462. return 0;
  463. }
  464. /* sysfs resume/suspend bits for timekeeping */
  465. static struct sysdev_class timekeeping_sysclass = {
  466. .name = "timekeeping",
  467. .resume = timekeeping_resume,
  468. .suspend = timekeeping_suspend,
  469. };
  470. static struct sys_device device_timer = {
  471. .id = 0,
  472. .cls = &timekeeping_sysclass,
  473. };
  474. static int __init timekeeping_init_device(void)
  475. {
  476. int error = sysdev_class_register(&timekeeping_sysclass);
  477. if (!error)
  478. error = sysdev_register(&device_timer);
  479. return error;
  480. }
  481. device_initcall(timekeeping_init_device);
  482. /*
  483. * If the error is already larger, we look ahead even further
  484. * to compensate for late or lost adjustments.
  485. */
  486. static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  487. s64 *offset)
  488. {
  489. s64 tick_error, i;
  490. u32 look_ahead, adj;
  491. s32 error2, mult;
  492. /*
  493. * Use the current error value to determine how much to look ahead.
  494. * The larger the error the slower we adjust for it to avoid problems
  495. * with losing too many ticks, otherwise we would overadjust and
  496. * produce an even larger error. The smaller the adjustment the
  497. * faster we try to adjust for it, as lost ticks can do less harm
  498. * here. This is tuned so that an error of about 1 msec is adjusted
  499. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  500. */
  501. error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
  502. error2 = abs(error2);
  503. for (look_ahead = 0; error2 > 0; look_ahead++)
  504. error2 >>= 2;
  505. /*
  506. * Now calculate the error in (1 << look_ahead) ticks, but first
  507. * remove the single look ahead already included in the error.
  508. */
  509. tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
  510. tick_error -= timekeeper.xtime_interval >> 1;
  511. error = ((error - tick_error) >> look_ahead) + tick_error;
  512. /* Finally calculate the adjustment shift value. */
  513. i = *interval;
  514. mult = 1;
  515. if (error < 0) {
  516. error = -error;
  517. *interval = -*interval;
  518. *offset = -*offset;
  519. mult = -1;
  520. }
  521. for (adj = 0; error > i; adj++)
  522. error >>= 1;
  523. *interval <<= adj;
  524. *offset <<= adj;
  525. return mult << adj;
  526. }
  527. /*
  528. * Adjust the multiplier to reduce the error value,
  529. * this is optimized for the most common adjustments of -1,0,1,
  530. * for other values we can do a bit more work.
  531. */
  532. static void timekeeping_adjust(s64 offset)
  533. {
  534. s64 error, interval = timekeeper.cycle_interval;
  535. int adj;
  536. error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
  537. if (error > interval) {
  538. error >>= 2;
  539. if (likely(error <= interval))
  540. adj = 1;
  541. else
  542. adj = timekeeping_bigadjust(error, &interval, &offset);
  543. } else if (error < -interval) {
  544. error >>= 2;
  545. if (likely(error >= -interval)) {
  546. adj = -1;
  547. interval = -interval;
  548. offset = -offset;
  549. } else
  550. adj = timekeeping_bigadjust(error, &interval, &offset);
  551. } else
  552. return;
  553. timekeeper.mult += adj;
  554. timekeeper.xtime_interval += interval;
  555. timekeeper.xtime_nsec -= offset;
  556. timekeeper.ntp_error -= (interval - offset) <<
  557. timekeeper.ntp_error_shift;
  558. }
  559. /**
  560. * update_wall_time - Uses the current clocksource to increment the wall time
  561. *
  562. * Called from the timer interrupt, must hold a write on xtime_lock.
  563. */
  564. void update_wall_time(void)
  565. {
  566. struct clocksource *clock;
  567. cycle_t offset;
  568. u64 nsecs;
  569. /* Make sure we're fully resumed: */
  570. if (unlikely(timekeeping_suspended))
  571. return;
  572. clock = timekeeper.clock;
  573. #ifdef CONFIG_GENERIC_TIME
  574. offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
  575. #else
  576. offset = timekeeper.cycle_interval;
  577. #endif
  578. timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
  579. /* normally this loop will run just once, however in the
  580. * case of lost or late ticks, it will accumulate correctly.
  581. */
  582. while (offset >= timekeeper.cycle_interval) {
  583. u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
  584. /* accumulate one interval */
  585. offset -= timekeeper.cycle_interval;
  586. clock->cycle_last += timekeeper.cycle_interval;
  587. timekeeper.xtime_nsec += timekeeper.xtime_interval;
  588. if (timekeeper.xtime_nsec >= nsecps) {
  589. timekeeper.xtime_nsec -= nsecps;
  590. xtime.tv_sec++;
  591. second_overflow();
  592. }
  593. raw_time.tv_nsec += timekeeper.raw_interval;
  594. if (raw_time.tv_nsec >= NSEC_PER_SEC) {
  595. raw_time.tv_nsec -= NSEC_PER_SEC;
  596. raw_time.tv_sec++;
  597. }
  598. /* accumulate error between NTP and clock interval */
  599. timekeeper.ntp_error += tick_length;
  600. timekeeper.ntp_error -= timekeeper.xtime_interval <<
  601. timekeeper.ntp_error_shift;
  602. }
  603. /* correct the clock when NTP error is too big */
  604. timekeeping_adjust(offset);
  605. /*
  606. * Since in the loop above, we accumulate any amount of time
  607. * in xtime_nsec over a second into xtime.tv_sec, its possible for
  608. * xtime_nsec to be fairly small after the loop. Further, if we're
  609. * slightly speeding the clocksource up in timekeeping_adjust(),
  610. * its possible the required corrective factor to xtime_nsec could
  611. * cause it to underflow.
  612. *
  613. * Now, we cannot simply roll the accumulated second back, since
  614. * the NTP subsystem has been notified via second_overflow. So
  615. * instead we push xtime_nsec forward by the amount we underflowed,
  616. * and add that amount into the error.
  617. *
  618. * We'll correct this error next time through this function, when
  619. * xtime_nsec is not as small.
  620. */
  621. if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
  622. s64 neg = -(s64)timekeeper.xtime_nsec;
  623. timekeeper.xtime_nsec = 0;
  624. timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
  625. }
  626. /* store full nanoseconds into xtime after rounding it up and
  627. * add the remainder to the error difference.
  628. */
  629. xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
  630. timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
  631. timekeeper.ntp_error += timekeeper.xtime_nsec <<
  632. timekeeper.ntp_error_shift;
  633. nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
  634. update_xtime_cache(nsecs);
  635. /* check to see if there is a new clocksource to use */
  636. change_clocksource();
  637. update_vsyscall(&xtime, timekeeper.clock);
  638. }
  639. /**
  640. * getboottime - Return the real time of system boot.
  641. * @ts: pointer to the timespec to be set
  642. *
  643. * Returns the time of day in a timespec.
  644. *
  645. * This is based on the wall_to_monotonic offset and the total suspend
  646. * time. Calls to settimeofday will affect the value returned (which
  647. * basically means that however wrong your real time clock is at boot time,
  648. * you get the right time here).
  649. */
  650. void getboottime(struct timespec *ts)
  651. {
  652. set_normalized_timespec(ts,
  653. - (wall_to_monotonic.tv_sec + total_sleep_time),
  654. - wall_to_monotonic.tv_nsec);
  655. }
  656. /**
  657. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  658. * @ts: pointer to the timespec to be converted
  659. */
  660. void monotonic_to_bootbased(struct timespec *ts)
  661. {
  662. ts->tv_sec += total_sleep_time;
  663. }
  664. unsigned long get_seconds(void)
  665. {
  666. return xtime_cache.tv_sec;
  667. }
  668. EXPORT_SYMBOL(get_seconds);
  669. struct timespec current_kernel_time(void)
  670. {
  671. struct timespec now;
  672. unsigned long seq;
  673. do {
  674. seq = read_seqbegin(&xtime_lock);
  675. now = xtime_cache;
  676. } while (read_seqretry(&xtime_lock, seq));
  677. return now;
  678. }
  679. EXPORT_SYMBOL(current_kernel_time);