timekeeping.c 38 KB

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