timekeeping.c 39 KB

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