timekeeping.c 36 KB

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