timekeeping.c 35 KB

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