timekeeping.c 37 KB

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