timekeeping.c 37 KB

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