timekeeping.c 39 KB

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