timekeeping.c 24 KB

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