timekeeping.c 37 KB

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