timekeeping.c 37 KB

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