timekeeping.c 36 KB

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