timekeeping.c 37 KB

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