timekeeping.c 37 KB

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