timekeeping.c 36 KB

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