timekeeping.c 37 KB

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