timekeeping.c 37 KB

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