timekeeping.c 36 KB

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