timekeeping.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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/syscore_ops.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. /* shifted nano seconds left over when rounding cycle_interval */
  33. s64 xtime_remainder;
  34. /* Raw nano seconds accumulated per NTP interval. */
  35. u32 raw_interval;
  36. /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
  37. u64 xtime_nsec;
  38. /* Difference between accumulated time and NTP time in ntp
  39. * shifted nano seconds. */
  40. s64 ntp_error;
  41. /* Shift conversion between clock shifted nano seconds and
  42. * ntp shifted nano seconds. */
  43. int ntp_error_shift;
  44. /* NTP adjusted clock multiplier */
  45. u32 mult;
  46. /* The current time */
  47. struct timespec xtime;
  48. /*
  49. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  50. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  51. * at zero at system boot time, so wall_to_monotonic will be negative,
  52. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  53. * the usual normalization.
  54. *
  55. * wall_to_monotonic is moved after resume from suspend for the
  56. * monotonic time not to jump. We need to add total_sleep_time to
  57. * wall_to_monotonic to get the real boot based time offset.
  58. *
  59. * - wall_to_monotonic is no longer the boot time, getboottime must be
  60. * used instead.
  61. */
  62. struct timespec wall_to_monotonic;
  63. /* time spent in suspend */
  64. struct timespec total_sleep_time;
  65. /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
  66. struct timespec raw_time;
  67. /* Seqlock for all timekeeper values */
  68. seqlock_t lock;
  69. };
  70. static struct timekeeper timekeeper;
  71. /*
  72. * This read-write spinlock protects us from races in SMP while
  73. * playing with xtime.
  74. */
  75. __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  76. /* flag for if timekeeping is suspended */
  77. int __read_mostly timekeeping_suspended;
  78. /**
  79. * timekeeper_setup_internals - Set up internals to use clocksource clock.
  80. *
  81. * @clock: Pointer to clocksource.
  82. *
  83. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  84. * pair and interval request.
  85. *
  86. * Unless you're the timekeeping code, you should not be using this!
  87. */
  88. static void timekeeper_setup_internals(struct clocksource *clock)
  89. {
  90. cycle_t interval;
  91. u64 tmp, ntpinterval;
  92. timekeeper.clock = clock;
  93. clock->cycle_last = clock->read(clock);
  94. /* Do the ns -> cycle conversion first, using original mult */
  95. tmp = NTP_INTERVAL_LENGTH;
  96. tmp <<= clock->shift;
  97. ntpinterval = tmp;
  98. tmp += clock->mult/2;
  99. do_div(tmp, clock->mult);
  100. if (tmp == 0)
  101. tmp = 1;
  102. interval = (cycle_t) tmp;
  103. timekeeper.cycle_interval = interval;
  104. /* Go back from cycles -> shifted ns */
  105. timekeeper.xtime_interval = (u64) interval * clock->mult;
  106. timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
  107. timekeeper.raw_interval =
  108. ((u64) interval * clock->mult) >> clock->shift;
  109. timekeeper.xtime_nsec = 0;
  110. timekeeper.shift = clock->shift;
  111. timekeeper.ntp_error = 0;
  112. timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  113. /*
  114. * The timekeeper keeps its own mult values for the currently
  115. * active clocksource. These value will be adjusted via NTP
  116. * to counteract clock drifting.
  117. */
  118. timekeeper.mult = clock->mult;
  119. }
  120. /* Timekeeper helper functions. */
  121. static inline s64 timekeeping_get_ns(void)
  122. {
  123. cycle_t cycle_now, cycle_delta;
  124. struct clocksource *clock;
  125. /* read clocksource: */
  126. clock = timekeeper.clock;
  127. cycle_now = clock->read(clock);
  128. /* calculate the delta since the last update_wall_time: */
  129. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  130. /* return delta convert to nanoseconds using ntp adjusted mult. */
  131. return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  132. timekeeper.shift);
  133. }
  134. static inline s64 timekeeping_get_ns_raw(void)
  135. {
  136. cycle_t cycle_now, cycle_delta;
  137. struct clocksource *clock;
  138. /* read clocksource: */
  139. clock = timekeeper.clock;
  140. cycle_now = clock->read(clock);
  141. /* calculate the delta since the last update_wall_time: */
  142. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  143. /* return delta convert to nanoseconds. */
  144. return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  145. }
  146. /* must hold xtime_lock */
  147. void timekeeping_leap_insert(int leapsecond)
  148. {
  149. unsigned long flags;
  150. write_seqlock_irqsave(&timekeeper.lock, flags);
  151. timekeeper.xtime.tv_sec += leapsecond;
  152. timekeeper.wall_to_monotonic.tv_sec -= leapsecond;
  153. update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
  154. timekeeper.clock, timekeeper.mult);
  155. write_sequnlock_irqrestore(&timekeeper.lock, flags);
  156. }
  157. /**
  158. * timekeeping_forward_now - update clock to the current time
  159. *
  160. * Forward the current clock to update its state since the last call to
  161. * update_wall_time(). This is useful before significant clock changes,
  162. * as it avoids having to deal with this time offset explicitly.
  163. */
  164. static void timekeeping_forward_now(void)
  165. {
  166. cycle_t cycle_now, cycle_delta;
  167. struct clocksource *clock;
  168. s64 nsec;
  169. clock = timekeeper.clock;
  170. cycle_now = clock->read(clock);
  171. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  172. clock->cycle_last = cycle_now;
  173. nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  174. timekeeper.shift);
  175. /* If arch requires, add in gettimeoffset() */
  176. nsec += arch_gettimeoffset();
  177. timespec_add_ns(&timekeeper.xtime, nsec);
  178. nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  179. timespec_add_ns(&timekeeper.raw_time, nsec);
  180. }
  181. /**
  182. * getnstimeofday - Returns the time of day in a timespec
  183. * @ts: pointer to the timespec to be set
  184. *
  185. * Returns the time of day in a timespec.
  186. */
  187. void getnstimeofday(struct timespec *ts)
  188. {
  189. unsigned long seq;
  190. s64 nsecs;
  191. WARN_ON(timekeeping_suspended);
  192. do {
  193. seq = read_seqbegin(&timekeeper.lock);
  194. *ts = timekeeper.xtime;
  195. nsecs = timekeeping_get_ns();
  196. /* If arch requires, add in gettimeoffset() */
  197. nsecs += arch_gettimeoffset();
  198. } while (read_seqretry(&timekeeper.lock, seq));
  199. timespec_add_ns(ts, nsecs);
  200. }
  201. EXPORT_SYMBOL(getnstimeofday);
  202. ktime_t ktime_get(void)
  203. {
  204. unsigned int seq;
  205. s64 secs, nsecs;
  206. WARN_ON(timekeeping_suspended);
  207. do {
  208. seq = read_seqbegin(&timekeeper.lock);
  209. secs = timekeeper.xtime.tv_sec +
  210. timekeeper.wall_to_monotonic.tv_sec;
  211. nsecs = timekeeper.xtime.tv_nsec +
  212. timekeeper.wall_to_monotonic.tv_nsec;
  213. nsecs += timekeeping_get_ns();
  214. /* If arch requires, add in gettimeoffset() */
  215. nsecs += arch_gettimeoffset();
  216. } while (read_seqretry(&timekeeper.lock, seq));
  217. /*
  218. * Use ktime_set/ktime_add_ns to create a proper ktime on
  219. * 32-bit architectures without CONFIG_KTIME_SCALAR.
  220. */
  221. return ktime_add_ns(ktime_set(secs, 0), nsecs);
  222. }
  223. EXPORT_SYMBOL_GPL(ktime_get);
  224. /**
  225. * ktime_get_ts - get the monotonic clock in timespec format
  226. * @ts: pointer to timespec variable
  227. *
  228. * The function calculates the monotonic clock from the realtime
  229. * clock and the wall_to_monotonic offset and stores the result
  230. * in normalized timespec format in the variable pointed to by @ts.
  231. */
  232. void ktime_get_ts(struct timespec *ts)
  233. {
  234. struct timespec tomono;
  235. unsigned int seq;
  236. s64 nsecs;
  237. WARN_ON(timekeeping_suspended);
  238. do {
  239. seq = read_seqbegin(&timekeeper.lock);
  240. *ts = timekeeper.xtime;
  241. tomono = timekeeper.wall_to_monotonic;
  242. nsecs = timekeeping_get_ns();
  243. /* If arch requires, add in gettimeoffset() */
  244. nsecs += arch_gettimeoffset();
  245. } while (read_seqretry(&timekeeper.lock, seq));
  246. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  247. ts->tv_nsec + tomono.tv_nsec + nsecs);
  248. }
  249. EXPORT_SYMBOL_GPL(ktime_get_ts);
  250. #ifdef CONFIG_NTP_PPS
  251. /**
  252. * getnstime_raw_and_real - get day and raw monotonic time in timespec format
  253. * @ts_raw: pointer to the timespec to be set to raw monotonic time
  254. * @ts_real: pointer to the timespec to be set to the time of day
  255. *
  256. * This function reads both the time of day and raw monotonic time at the
  257. * same time atomically and stores the resulting timestamps in timespec
  258. * format.
  259. */
  260. void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  261. {
  262. unsigned long seq;
  263. s64 nsecs_raw, nsecs_real;
  264. WARN_ON_ONCE(timekeeping_suspended);
  265. do {
  266. u32 arch_offset;
  267. seq = read_seqbegin(&timekeeper.lock);
  268. *ts_raw = timekeeper.raw_time;
  269. *ts_real = timekeeper.xtime;
  270. nsecs_raw = timekeeping_get_ns_raw();
  271. nsecs_real = timekeeping_get_ns();
  272. /* If arch requires, add in gettimeoffset() */
  273. arch_offset = arch_gettimeoffset();
  274. nsecs_raw += arch_offset;
  275. nsecs_real += arch_offset;
  276. } while (read_seqretry(&timekeeper.lock, seq));
  277. timespec_add_ns(ts_raw, nsecs_raw);
  278. timespec_add_ns(ts_real, nsecs_real);
  279. }
  280. EXPORT_SYMBOL(getnstime_raw_and_real);
  281. #endif /* CONFIG_NTP_PPS */
  282. /**
  283. * do_gettimeofday - Returns the time of day in a timeval
  284. * @tv: pointer to the timeval to be set
  285. *
  286. * NOTE: Users should be converted to using getnstimeofday()
  287. */
  288. void do_gettimeofday(struct timeval *tv)
  289. {
  290. struct timespec now;
  291. getnstimeofday(&now);
  292. tv->tv_sec = now.tv_sec;
  293. tv->tv_usec = now.tv_nsec/1000;
  294. }
  295. EXPORT_SYMBOL(do_gettimeofday);
  296. /**
  297. * do_settimeofday - Sets the time of day
  298. * @tv: pointer to the timespec variable containing the new time
  299. *
  300. * Sets the time of day to the new time and update NTP and notify hrtimers
  301. */
  302. int do_settimeofday(const struct timespec *tv)
  303. {
  304. struct timespec ts_delta;
  305. unsigned long flags1,flags2;
  306. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  307. return -EINVAL;
  308. write_seqlock_irqsave(&xtime_lock, flags1);
  309. write_seqlock_irqsave(&timekeeper.lock, flags2);
  310. timekeeping_forward_now();
  311. ts_delta.tv_sec = tv->tv_sec - timekeeper.xtime.tv_sec;
  312. ts_delta.tv_nsec = tv->tv_nsec - timekeeper.xtime.tv_nsec;
  313. timekeeper.wall_to_monotonic =
  314. timespec_sub(timekeeper.wall_to_monotonic, ts_delta);
  315. timekeeper.xtime = *tv;
  316. timekeeper.ntp_error = 0;
  317. ntp_clear();
  318. update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
  319. timekeeper.clock, timekeeper.mult);
  320. write_sequnlock_irqrestore(&timekeeper.lock, flags2);
  321. write_sequnlock_irqrestore(&xtime_lock, flags1);
  322. /* signal hrtimers about time change */
  323. clock_was_set();
  324. return 0;
  325. }
  326. EXPORT_SYMBOL(do_settimeofday);
  327. /**
  328. * timekeeping_inject_offset - Adds or subtracts from the current time.
  329. * @tv: pointer to the timespec variable containing the offset
  330. *
  331. * Adds or subtracts an offset value from the current time.
  332. */
  333. int timekeeping_inject_offset(struct timespec *ts)
  334. {
  335. unsigned long flags1,flags2;
  336. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  337. return -EINVAL;
  338. write_seqlock_irqsave(&xtime_lock, flags1);
  339. write_seqlock_irqsave(&timekeeper.lock, flags2);
  340. timekeeping_forward_now();
  341. timekeeper.xtime = timespec_add(timekeeper.xtime, *ts);
  342. timekeeper.wall_to_monotonic =
  343. timespec_sub(timekeeper.wall_to_monotonic, *ts);
  344. timekeeper.ntp_error = 0;
  345. ntp_clear();
  346. update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
  347. timekeeper.clock, timekeeper.mult);
  348. write_sequnlock_irqrestore(&timekeeper.lock, flags2);
  349. write_sequnlock_irqrestore(&xtime_lock, flags1);
  350. /* signal hrtimers about time change */
  351. clock_was_set();
  352. return 0;
  353. }
  354. EXPORT_SYMBOL(timekeeping_inject_offset);
  355. /**
  356. * change_clocksource - Swaps clocksources if a new one is available
  357. *
  358. * Accumulates current time interval and initializes new clocksource
  359. */
  360. static int change_clocksource(void *data)
  361. {
  362. struct clocksource *new, *old;
  363. new = (struct clocksource *) data;
  364. timekeeping_forward_now();
  365. if (!new->enable || new->enable(new) == 0) {
  366. old = timekeeper.clock;
  367. timekeeper_setup_internals(new);
  368. if (old->disable)
  369. old->disable(old);
  370. }
  371. return 0;
  372. }
  373. /**
  374. * timekeeping_notify - Install a new clock source
  375. * @clock: pointer to the clock source
  376. *
  377. * This function is called from clocksource.c after a new, better clock
  378. * source has been registered. The caller holds the clocksource_mutex.
  379. */
  380. void timekeeping_notify(struct clocksource *clock)
  381. {
  382. if (timekeeper.clock == clock)
  383. return;
  384. stop_machine(change_clocksource, clock, NULL);
  385. tick_clock_notify();
  386. }
  387. /**
  388. * ktime_get_real - get the real (wall-) time in ktime_t format
  389. *
  390. * returns the time in ktime_t format
  391. */
  392. ktime_t ktime_get_real(void)
  393. {
  394. struct timespec now;
  395. getnstimeofday(&now);
  396. return timespec_to_ktime(now);
  397. }
  398. EXPORT_SYMBOL_GPL(ktime_get_real);
  399. /**
  400. * getrawmonotonic - Returns the raw monotonic time in a timespec
  401. * @ts: pointer to the timespec to be set
  402. *
  403. * Returns the raw monotonic time (completely un-modified by ntp)
  404. */
  405. void getrawmonotonic(struct timespec *ts)
  406. {
  407. unsigned long seq;
  408. s64 nsecs;
  409. do {
  410. seq = read_seqbegin(&timekeeper.lock);
  411. nsecs = timekeeping_get_ns_raw();
  412. *ts = timekeeper.raw_time;
  413. } while (read_seqretry(&timekeeper.lock, seq));
  414. timespec_add_ns(ts, nsecs);
  415. }
  416. EXPORT_SYMBOL(getrawmonotonic);
  417. /**
  418. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  419. */
  420. int timekeeping_valid_for_hres(void)
  421. {
  422. unsigned long seq;
  423. int ret;
  424. do {
  425. seq = read_seqbegin(&timekeeper.lock);
  426. ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  427. } while (read_seqretry(&timekeeper.lock, seq));
  428. return ret;
  429. }
  430. /**
  431. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  432. */
  433. u64 timekeeping_max_deferment(void)
  434. {
  435. unsigned long seq;
  436. u64 ret;
  437. do {
  438. seq = read_seqbegin(&timekeeper.lock);
  439. ret = timekeeper.clock->max_idle_ns;
  440. } while (read_seqretry(&timekeeper.lock, seq));
  441. return ret;
  442. }
  443. /**
  444. * read_persistent_clock - Return time from the persistent clock.
  445. *
  446. * Weak dummy function for arches that do not yet support it.
  447. * Reads the time from the battery backed persistent clock.
  448. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  449. *
  450. * XXX - Do be sure to remove it once all arches implement it.
  451. */
  452. void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
  453. {
  454. ts->tv_sec = 0;
  455. ts->tv_nsec = 0;
  456. }
  457. /**
  458. * read_boot_clock - Return time of the system start.
  459. *
  460. * Weak dummy function for arches that do not yet support it.
  461. * Function to read the exact time the system has been started.
  462. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  463. *
  464. * XXX - Do be sure to remove it once all arches implement it.
  465. */
  466. void __attribute__((weak)) read_boot_clock(struct timespec *ts)
  467. {
  468. ts->tv_sec = 0;
  469. ts->tv_nsec = 0;
  470. }
  471. /*
  472. * timekeeping_init - Initializes the clocksource and common timekeeping values
  473. */
  474. void __init timekeeping_init(void)
  475. {
  476. struct clocksource *clock;
  477. unsigned long flags;
  478. struct timespec now, boot;
  479. read_persistent_clock(&now);
  480. read_boot_clock(&boot);
  481. seqlock_init(&timekeeper.lock);
  482. write_seqlock_irqsave(&xtime_lock, flags);
  483. ntp_init();
  484. write_sequnlock_irqrestore(&xtime_lock, flags);
  485. write_seqlock_irqsave(&timekeeper.lock, flags);
  486. clock = clocksource_default_clock();
  487. if (clock->enable)
  488. clock->enable(clock);
  489. timekeeper_setup_internals(clock);
  490. timekeeper.xtime.tv_sec = now.tv_sec;
  491. timekeeper.xtime.tv_nsec = now.tv_nsec;
  492. timekeeper.raw_time.tv_sec = 0;
  493. timekeeper.raw_time.tv_nsec = 0;
  494. if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
  495. boot.tv_sec = timekeeper.xtime.tv_sec;
  496. boot.tv_nsec = timekeeper.xtime.tv_nsec;
  497. }
  498. set_normalized_timespec(&timekeeper.wall_to_monotonic,
  499. -boot.tv_sec, -boot.tv_nsec);
  500. timekeeper.total_sleep_time.tv_sec = 0;
  501. timekeeper.total_sleep_time.tv_nsec = 0;
  502. write_sequnlock_irqrestore(&timekeeper.lock, flags);
  503. }
  504. /* time in seconds when suspend began */
  505. static struct timespec timekeeping_suspend_time;
  506. /**
  507. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  508. * @delta: pointer to a timespec delta value
  509. *
  510. * Takes a timespec offset measuring a suspend interval and properly
  511. * adds the sleep offset to the timekeeping variables.
  512. */
  513. static void __timekeeping_inject_sleeptime(struct timespec *delta)
  514. {
  515. if (!timespec_valid(delta)) {
  516. printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
  517. "sleep delta value!\n");
  518. return;
  519. }
  520. timekeeper.xtime = timespec_add(timekeeper.xtime, *delta);
  521. timekeeper.wall_to_monotonic =
  522. timespec_sub(timekeeper.wall_to_monotonic, *delta);
  523. timekeeper.total_sleep_time = timespec_add(
  524. timekeeper.total_sleep_time, *delta);
  525. }
  526. /**
  527. * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
  528. * @delta: pointer to a timespec delta value
  529. *
  530. * This hook is for architectures that cannot support read_persistent_clock
  531. * because their RTC/persistent clock is only accessible when irqs are enabled.
  532. *
  533. * This function should only be called by rtc_resume(), and allows
  534. * a suspend offset to be injected into the timekeeping values.
  535. */
  536. void timekeeping_inject_sleeptime(struct timespec *delta)
  537. {
  538. unsigned long flags1,flags2;
  539. struct timespec ts;
  540. /* Make sure we don't set the clock twice */
  541. read_persistent_clock(&ts);
  542. if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
  543. return;
  544. write_seqlock_irqsave(&xtime_lock, flags1);
  545. write_seqlock_irqsave(&timekeeper.lock, flags2);
  546. timekeeping_forward_now();
  547. __timekeeping_inject_sleeptime(delta);
  548. timekeeper.ntp_error = 0;
  549. ntp_clear();
  550. update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
  551. timekeeper.clock, timekeeper.mult);
  552. write_sequnlock_irqrestore(&timekeeper.lock, flags2);
  553. write_sequnlock_irqrestore(&xtime_lock, flags1);
  554. /* signal hrtimers about time change */
  555. clock_was_set();
  556. }
  557. /**
  558. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  559. *
  560. * This is for the generic clocksource timekeeping.
  561. * xtime/wall_to_monotonic/jiffies/etc are
  562. * still managed by arch specific suspend/resume code.
  563. */
  564. static void timekeeping_resume(void)
  565. {
  566. unsigned long flags1,flags2;
  567. struct timespec ts;
  568. read_persistent_clock(&ts);
  569. clocksource_resume();
  570. write_seqlock_irqsave(&xtime_lock, flags1);
  571. write_seqlock_irqsave(&timekeeper.lock, flags2);
  572. if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
  573. ts = timespec_sub(ts, timekeeping_suspend_time);
  574. __timekeeping_inject_sleeptime(&ts);
  575. }
  576. /* re-base the last cycle value */
  577. timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
  578. timekeeper.ntp_error = 0;
  579. timekeeping_suspended = 0;
  580. write_sequnlock_irqrestore(&timekeeper.lock, flags2);
  581. write_sequnlock_irqrestore(&xtime_lock, flags1);
  582. touch_softlockup_watchdog();
  583. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  584. /* Resume hrtimers */
  585. hrtimers_resume();
  586. }
  587. static int timekeeping_suspend(void)
  588. {
  589. unsigned long flags1,flags2;
  590. struct timespec delta, delta_delta;
  591. static struct timespec old_delta;
  592. read_persistent_clock(&timekeeping_suspend_time);
  593. write_seqlock_irqsave(&xtime_lock, flags1);
  594. write_seqlock_irqsave(&timekeeper.lock, flags2);
  595. timekeeping_forward_now();
  596. timekeeping_suspended = 1;
  597. /*
  598. * To avoid drift caused by repeated suspend/resumes,
  599. * which each can add ~1 second drift error,
  600. * try to compensate so the difference in system time
  601. * and persistent_clock time stays close to constant.
  602. */
  603. delta = timespec_sub(timekeeper.xtime, timekeeping_suspend_time);
  604. delta_delta = timespec_sub(delta, old_delta);
  605. if (abs(delta_delta.tv_sec) >= 2) {
  606. /*
  607. * if delta_delta is too large, assume time correction
  608. * has occured and set old_delta to the current delta.
  609. */
  610. old_delta = delta;
  611. } else {
  612. /* Otherwise try to adjust old_system to compensate */
  613. timekeeping_suspend_time =
  614. timespec_add(timekeeping_suspend_time, delta_delta);
  615. }
  616. write_sequnlock_irqrestore(&timekeeper.lock, flags2);
  617. write_sequnlock_irqrestore(&xtime_lock, flags1);
  618. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  619. clocksource_suspend();
  620. return 0;
  621. }
  622. /* sysfs resume/suspend bits for timekeeping */
  623. static struct syscore_ops timekeeping_syscore_ops = {
  624. .resume = timekeeping_resume,
  625. .suspend = timekeeping_suspend,
  626. };
  627. static int __init timekeeping_init_ops(void)
  628. {
  629. register_syscore_ops(&timekeeping_syscore_ops);
  630. return 0;
  631. }
  632. device_initcall(timekeeping_init_ops);
  633. /*
  634. * If the error is already larger, we look ahead even further
  635. * to compensate for late or lost adjustments.
  636. */
  637. static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  638. s64 *offset)
  639. {
  640. s64 tick_error, i;
  641. u32 look_ahead, adj;
  642. s32 error2, mult;
  643. /*
  644. * Use the current error value to determine how much to look ahead.
  645. * The larger the error the slower we adjust for it to avoid problems
  646. * with losing too many ticks, otherwise we would overadjust and
  647. * produce an even larger error. The smaller the adjustment the
  648. * faster we try to adjust for it, as lost ticks can do less harm
  649. * here. This is tuned so that an error of about 1 msec is adjusted
  650. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  651. */
  652. error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
  653. error2 = abs(error2);
  654. for (look_ahead = 0; error2 > 0; look_ahead++)
  655. error2 >>= 2;
  656. /*
  657. * Now calculate the error in (1 << look_ahead) ticks, but first
  658. * remove the single look ahead already included in the error.
  659. */
  660. tick_error = ntp_tick_length() >> (timekeeper.ntp_error_shift + 1);
  661. tick_error -= timekeeper.xtime_interval >> 1;
  662. error = ((error - tick_error) >> look_ahead) + tick_error;
  663. /* Finally calculate the adjustment shift value. */
  664. i = *interval;
  665. mult = 1;
  666. if (error < 0) {
  667. error = -error;
  668. *interval = -*interval;
  669. *offset = -*offset;
  670. mult = -1;
  671. }
  672. for (adj = 0; error > i; adj++)
  673. error >>= 1;
  674. *interval <<= adj;
  675. *offset <<= adj;
  676. return mult << adj;
  677. }
  678. /*
  679. * Adjust the multiplier to reduce the error value,
  680. * this is optimized for the most common adjustments of -1,0,1,
  681. * for other values we can do a bit more work.
  682. */
  683. static void timekeeping_adjust(s64 offset)
  684. {
  685. s64 error, interval = timekeeper.cycle_interval;
  686. int adj;
  687. /*
  688. * The point of this is to check if the error is greater then half
  689. * an interval.
  690. *
  691. * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
  692. *
  693. * Note we subtract one in the shift, so that error is really error*2.
  694. * This "saves" dividing(shifting) interval twice, but keeps the
  695. * (error > interval) comparison as still measuring if error is
  696. * larger then half an interval.
  697. *
  698. * Note: It does not "save" on aggravation when reading the code.
  699. */
  700. error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
  701. if (error > interval) {
  702. /*
  703. * We now divide error by 4(via shift), which checks if
  704. * the error is greater then twice the interval.
  705. * If it is greater, we need a bigadjust, if its smaller,
  706. * we can adjust by 1.
  707. */
  708. error >>= 2;
  709. /*
  710. * XXX - In update_wall_time, we round up to the next
  711. * nanosecond, and store the amount rounded up into
  712. * the error. This causes the likely below to be unlikely.
  713. *
  714. * The proper fix is to avoid rounding up by using
  715. * the high precision timekeeper.xtime_nsec instead of
  716. * xtime.tv_nsec everywhere. Fixing this will take some
  717. * time.
  718. */
  719. if (likely(error <= interval))
  720. adj = 1;
  721. else
  722. adj = timekeeping_bigadjust(error, &interval, &offset);
  723. } else if (error < -interval) {
  724. /* See comment above, this is just switched for the negative */
  725. error >>= 2;
  726. if (likely(error >= -interval)) {
  727. adj = -1;
  728. interval = -interval;
  729. offset = -offset;
  730. } else
  731. adj = timekeeping_bigadjust(error, &interval, &offset);
  732. } else /* No adjustment needed */
  733. return;
  734. WARN_ONCE(timekeeper.clock->maxadj &&
  735. (timekeeper.mult + adj > timekeeper.clock->mult +
  736. timekeeper.clock->maxadj),
  737. "Adjusting %s more then 11%% (%ld vs %ld)\n",
  738. timekeeper.clock->name, (long)timekeeper.mult + adj,
  739. (long)timekeeper.clock->mult +
  740. timekeeper.clock->maxadj);
  741. /*
  742. * So the following can be confusing.
  743. *
  744. * To keep things simple, lets assume adj == 1 for now.
  745. *
  746. * When adj != 1, remember that the interval and offset values
  747. * have been appropriately scaled so the math is the same.
  748. *
  749. * The basic idea here is that we're increasing the multiplier
  750. * by one, this causes the xtime_interval to be incremented by
  751. * one cycle_interval. This is because:
  752. * xtime_interval = cycle_interval * mult
  753. * So if mult is being incremented by one:
  754. * xtime_interval = cycle_interval * (mult + 1)
  755. * Its the same as:
  756. * xtime_interval = (cycle_interval * mult) + cycle_interval
  757. * Which can be shortened to:
  758. * xtime_interval += cycle_interval
  759. *
  760. * So offset stores the non-accumulated cycles. Thus the current
  761. * time (in shifted nanoseconds) is:
  762. * now = (offset * adj) + xtime_nsec
  763. * Now, even though we're adjusting the clock frequency, we have
  764. * to keep time consistent. In other words, we can't jump back
  765. * in time, and we also want to avoid jumping forward in time.
  766. *
  767. * So given the same offset value, we need the time to be the same
  768. * both before and after the freq adjustment.
  769. * now = (offset * adj_1) + xtime_nsec_1
  770. * now = (offset * adj_2) + xtime_nsec_2
  771. * So:
  772. * (offset * adj_1) + xtime_nsec_1 =
  773. * (offset * adj_2) + xtime_nsec_2
  774. * And we know:
  775. * adj_2 = adj_1 + 1
  776. * So:
  777. * (offset * adj_1) + xtime_nsec_1 =
  778. * (offset * (adj_1+1)) + xtime_nsec_2
  779. * (offset * adj_1) + xtime_nsec_1 =
  780. * (offset * adj_1) + offset + xtime_nsec_2
  781. * Canceling the sides:
  782. * xtime_nsec_1 = offset + xtime_nsec_2
  783. * Which gives us:
  784. * xtime_nsec_2 = xtime_nsec_1 - offset
  785. * Which simplfies to:
  786. * xtime_nsec -= offset
  787. *
  788. * XXX - TODO: Doc ntp_error calculation.
  789. */
  790. timekeeper.mult += adj;
  791. timekeeper.xtime_interval += interval;
  792. timekeeper.xtime_nsec -= offset;
  793. timekeeper.ntp_error -= (interval - offset) <<
  794. timekeeper.ntp_error_shift;
  795. }
  796. /**
  797. * logarithmic_accumulation - shifted accumulation of cycles
  798. *
  799. * This functions accumulates a shifted interval of cycles into
  800. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  801. * loop.
  802. *
  803. * Returns the unconsumed cycles.
  804. */
  805. static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
  806. {
  807. u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
  808. u64 raw_nsecs;
  809. /* If the offset is smaller then a shifted interval, do nothing */
  810. if (offset < timekeeper.cycle_interval<<shift)
  811. return offset;
  812. /* Accumulate one shifted interval */
  813. offset -= timekeeper.cycle_interval << shift;
  814. timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
  815. timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
  816. while (timekeeper.xtime_nsec >= nsecps) {
  817. timekeeper.xtime_nsec -= nsecps;
  818. timekeeper.xtime.tv_sec++;
  819. second_overflow();
  820. }
  821. /* Accumulate raw time */
  822. raw_nsecs = timekeeper.raw_interval << shift;
  823. raw_nsecs += timekeeper.raw_time.tv_nsec;
  824. if (raw_nsecs >= NSEC_PER_SEC) {
  825. u64 raw_secs = raw_nsecs;
  826. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  827. timekeeper.raw_time.tv_sec += raw_secs;
  828. }
  829. timekeeper.raw_time.tv_nsec = raw_nsecs;
  830. /* Accumulate error between NTP and clock interval */
  831. timekeeper.ntp_error += ntp_tick_length() << shift;
  832. timekeeper.ntp_error -=
  833. (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
  834. (timekeeper.ntp_error_shift + shift);
  835. return offset;
  836. }
  837. /**
  838. * update_wall_time - Uses the current clocksource to increment the wall time
  839. *
  840. * Called from the timer interrupt, must hold a write on xtime_lock.
  841. */
  842. static void update_wall_time(void)
  843. {
  844. struct clocksource *clock;
  845. cycle_t offset;
  846. int shift = 0, maxshift;
  847. unsigned long flags;
  848. write_seqlock_irqsave(&timekeeper.lock, flags);
  849. /* Make sure we're fully resumed: */
  850. if (unlikely(timekeeping_suspended))
  851. goto out;
  852. clock = timekeeper.clock;
  853. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  854. offset = timekeeper.cycle_interval;
  855. #else
  856. offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
  857. #endif
  858. timekeeper.xtime_nsec = (s64)timekeeper.xtime.tv_nsec <<
  859. timekeeper.shift;
  860. /*
  861. * With NO_HZ we may have to accumulate many cycle_intervals
  862. * (think "ticks") worth of time at once. To do this efficiently,
  863. * we calculate the largest doubling multiple of cycle_intervals
  864. * that is smaller then the offset. We then accumulate that
  865. * chunk in one go, and then try to consume the next smaller
  866. * doubled multiple.
  867. */
  868. shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
  869. shift = max(0, shift);
  870. /* Bound shift to one less then what overflows tick_length */
  871. maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
  872. shift = min(shift, maxshift);
  873. while (offset >= timekeeper.cycle_interval) {
  874. offset = logarithmic_accumulation(offset, shift);
  875. if(offset < timekeeper.cycle_interval<<shift)
  876. shift--;
  877. }
  878. /* correct the clock when NTP error is too big */
  879. timekeeping_adjust(offset);
  880. /*
  881. * Since in the loop above, we accumulate any amount of time
  882. * in xtime_nsec over a second into xtime.tv_sec, its possible for
  883. * xtime_nsec to be fairly small after the loop. Further, if we're
  884. * slightly speeding the clocksource up in timekeeping_adjust(),
  885. * its possible the required corrective factor to xtime_nsec could
  886. * cause it to underflow.
  887. *
  888. * Now, we cannot simply roll the accumulated second back, since
  889. * the NTP subsystem has been notified via second_overflow. So
  890. * instead we push xtime_nsec forward by the amount we underflowed,
  891. * and add that amount into the error.
  892. *
  893. * We'll correct this error next time through this function, when
  894. * xtime_nsec is not as small.
  895. */
  896. if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
  897. s64 neg = -(s64)timekeeper.xtime_nsec;
  898. timekeeper.xtime_nsec = 0;
  899. timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
  900. }
  901. /*
  902. * Store full nanoseconds into xtime after rounding it up and
  903. * add the remainder to the error difference.
  904. */
  905. timekeeper.xtime.tv_nsec = ((s64)timekeeper.xtime_nsec >>
  906. timekeeper.shift) + 1;
  907. timekeeper.xtime_nsec -= (s64)timekeeper.xtime.tv_nsec <<
  908. timekeeper.shift;
  909. timekeeper.ntp_error += timekeeper.xtime_nsec <<
  910. timekeeper.ntp_error_shift;
  911. /*
  912. * Finally, make sure that after the rounding
  913. * xtime.tv_nsec isn't larger then NSEC_PER_SEC
  914. */
  915. if (unlikely(timekeeper.xtime.tv_nsec >= NSEC_PER_SEC)) {
  916. timekeeper.xtime.tv_nsec -= NSEC_PER_SEC;
  917. timekeeper.xtime.tv_sec++;
  918. second_overflow();
  919. }
  920. /* check to see if there is a new clocksource to use */
  921. update_vsyscall(&timekeeper.xtime, &timekeeper.wall_to_monotonic,
  922. timekeeper.clock, timekeeper.mult);
  923. out:
  924. write_sequnlock_irqrestore(&timekeeper.lock, flags);
  925. }
  926. /**
  927. * getboottime - Return the real time of system boot.
  928. * @ts: pointer to the timespec to be set
  929. *
  930. * Returns the wall-time of boot in a timespec.
  931. *
  932. * This is based on the wall_to_monotonic offset and the total suspend
  933. * time. Calls to settimeofday will affect the value returned (which
  934. * basically means that however wrong your real time clock is at boot time,
  935. * you get the right time here).
  936. */
  937. void getboottime(struct timespec *ts)
  938. {
  939. struct timespec boottime = {
  940. .tv_sec = timekeeper.wall_to_monotonic.tv_sec +
  941. timekeeper.total_sleep_time.tv_sec,
  942. .tv_nsec = timekeeper.wall_to_monotonic.tv_nsec +
  943. timekeeper.total_sleep_time.tv_nsec
  944. };
  945. set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
  946. }
  947. EXPORT_SYMBOL_GPL(getboottime);
  948. /**
  949. * get_monotonic_boottime - Returns monotonic time since boot
  950. * @ts: pointer to the timespec to be set
  951. *
  952. * Returns the monotonic time since boot in a timespec.
  953. *
  954. * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
  955. * includes the time spent in suspend.
  956. */
  957. void get_monotonic_boottime(struct timespec *ts)
  958. {
  959. struct timespec tomono, sleep;
  960. unsigned int seq;
  961. s64 nsecs;
  962. WARN_ON(timekeeping_suspended);
  963. do {
  964. seq = read_seqbegin(&timekeeper.lock);
  965. *ts = timekeeper.xtime;
  966. tomono = timekeeper.wall_to_monotonic;
  967. sleep = timekeeper.total_sleep_time;
  968. nsecs = timekeeping_get_ns();
  969. } while (read_seqretry(&timekeeper.lock, seq));
  970. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
  971. ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
  972. }
  973. EXPORT_SYMBOL_GPL(get_monotonic_boottime);
  974. /**
  975. * ktime_get_boottime - Returns monotonic time since boot in a ktime
  976. *
  977. * Returns the monotonic time since boot in a ktime
  978. *
  979. * This is similar to CLOCK_MONTONIC/ktime_get, but also
  980. * includes the time spent in suspend.
  981. */
  982. ktime_t ktime_get_boottime(void)
  983. {
  984. struct timespec ts;
  985. get_monotonic_boottime(&ts);
  986. return timespec_to_ktime(ts);
  987. }
  988. EXPORT_SYMBOL_GPL(ktime_get_boottime);
  989. /**
  990. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  991. * @ts: pointer to the timespec to be converted
  992. */
  993. void monotonic_to_bootbased(struct timespec *ts)
  994. {
  995. *ts = timespec_add(*ts, timekeeper.total_sleep_time);
  996. }
  997. EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
  998. unsigned long get_seconds(void)
  999. {
  1000. return timekeeper.xtime.tv_sec;
  1001. }
  1002. EXPORT_SYMBOL(get_seconds);
  1003. struct timespec __current_kernel_time(void)
  1004. {
  1005. return timekeeper.xtime;
  1006. }
  1007. struct timespec current_kernel_time(void)
  1008. {
  1009. struct timespec now;
  1010. unsigned long seq;
  1011. do {
  1012. seq = read_seqbegin(&timekeeper.lock);
  1013. now = timekeeper.xtime;
  1014. } while (read_seqretry(&timekeeper.lock, seq));
  1015. return now;
  1016. }
  1017. EXPORT_SYMBOL(current_kernel_time);
  1018. struct timespec get_monotonic_coarse(void)
  1019. {
  1020. struct timespec now, mono;
  1021. unsigned long seq;
  1022. do {
  1023. seq = read_seqbegin(&timekeeper.lock);
  1024. now = timekeeper.xtime;
  1025. mono = timekeeper.wall_to_monotonic;
  1026. } while (read_seqretry(&timekeeper.lock, seq));
  1027. set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
  1028. now.tv_nsec + mono.tv_nsec);
  1029. return now;
  1030. }
  1031. /*
  1032. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  1033. * without sampling the sequence number in xtime_lock.
  1034. * jiffies is defined in the linker script...
  1035. */
  1036. void do_timer(unsigned long ticks)
  1037. {
  1038. jiffies_64 += ticks;
  1039. update_wall_time();
  1040. calc_global_load(ticks);
  1041. }
  1042. /**
  1043. * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
  1044. * and sleep offsets.
  1045. * @xtim: pointer to timespec to be set with xtime
  1046. * @wtom: pointer to timespec to be set with wall_to_monotonic
  1047. * @sleep: pointer to timespec to be set with time in suspend
  1048. */
  1049. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  1050. struct timespec *wtom, struct timespec *sleep)
  1051. {
  1052. unsigned long seq;
  1053. do {
  1054. seq = read_seqbegin(&timekeeper.lock);
  1055. *xtim = timekeeper.xtime;
  1056. *wtom = timekeeper.wall_to_monotonic;
  1057. *sleep = timekeeper.total_sleep_time;
  1058. } while (read_seqretry(&timekeeper.lock, seq));
  1059. }
  1060. /**
  1061. * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
  1062. */
  1063. ktime_t ktime_get_monotonic_offset(void)
  1064. {
  1065. unsigned long seq;
  1066. struct timespec wtom;
  1067. do {
  1068. seq = read_seqbegin(&timekeeper.lock);
  1069. wtom = timekeeper.wall_to_monotonic;
  1070. } while (read_seqretry(&timekeeper.lock, seq));
  1071. return timespec_to_ktime(wtom);
  1072. }
  1073. /**
  1074. * xtime_update() - advances the timekeeping infrastructure
  1075. * @ticks: number of ticks, that have elapsed since the last call.
  1076. *
  1077. * Must be called with interrupts disabled.
  1078. */
  1079. void xtime_update(unsigned long ticks)
  1080. {
  1081. write_seqlock(&xtime_lock);
  1082. do_timer(ticks);
  1083. write_sequnlock(&xtime_lock);
  1084. }