timekeeping.c 22 KB

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