timekeeping.c 37 KB

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