timekeeping.c 35 KB

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