timekeeping.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. /* The shift value of the current clocksource. */
  27. int shift;
  28. /* Number of clock cycles in one NTP interval. */
  29. cycle_t cycle_interval;
  30. /* Number of clock shifted nano seconds in one NTP interval. */
  31. u64 xtime_interval;
  32. /* shifted nano seconds left over when rounding cycle_interval */
  33. s64 xtime_remainder;
  34. /* Raw nano seconds accumulated per NTP interval. */
  35. u32 raw_interval;
  36. /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
  37. u64 xtime_nsec;
  38. /* Difference between accumulated time and NTP time in ntp
  39. * shifted nano seconds. */
  40. s64 ntp_error;
  41. /* Shift conversion between clock shifted nano seconds and
  42. * ntp shifted nano seconds. */
  43. int ntp_error_shift;
  44. /* NTP adjusted clock multiplier */
  45. u32 mult;
  46. };
  47. static struct timekeeper timekeeper;
  48. /**
  49. * timekeeper_setup_internals - Set up internals to use clocksource clock.
  50. *
  51. * @clock: Pointer to clocksource.
  52. *
  53. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  54. * pair and interval request.
  55. *
  56. * Unless you're the timekeeping code, you should not be using this!
  57. */
  58. static void timekeeper_setup_internals(struct clocksource *clock)
  59. {
  60. cycle_t interval;
  61. u64 tmp, ntpinterval;
  62. timekeeper.clock = clock;
  63. clock->cycle_last = clock->read(clock);
  64. /* Do the ns -> cycle conversion first, using original mult */
  65. tmp = NTP_INTERVAL_LENGTH;
  66. tmp <<= clock->shift;
  67. ntpinterval = tmp;
  68. tmp += clock->mult/2;
  69. do_div(tmp, clock->mult);
  70. if (tmp == 0)
  71. tmp = 1;
  72. interval = (cycle_t) tmp;
  73. timekeeper.cycle_interval = interval;
  74. /* Go back from cycles -> shifted ns */
  75. timekeeper.xtime_interval = (u64) interval * clock->mult;
  76. timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
  77. timekeeper.raw_interval =
  78. ((u64) interval * clock->mult) >> clock->shift;
  79. timekeeper.xtime_nsec = 0;
  80. timekeeper.shift = clock->shift;
  81. timekeeper.ntp_error = 0;
  82. timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  83. /*
  84. * The timekeeper keeps its own mult values for the currently
  85. * active clocksource. These value will be adjusted via NTP
  86. * to counteract clock drifting.
  87. */
  88. timekeeper.mult = clock->mult;
  89. }
  90. /* Timekeeper helper functions. */
  91. static inline s64 timekeeping_get_ns(void)
  92. {
  93. cycle_t cycle_now, cycle_delta;
  94. struct clocksource *clock;
  95. /* read clocksource: */
  96. clock = timekeeper.clock;
  97. cycle_now = clock->read(clock);
  98. /* calculate the delta since the last update_wall_time: */
  99. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  100. /* return delta convert to nanoseconds using ntp adjusted mult. */
  101. return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  102. timekeeper.shift);
  103. }
  104. static inline s64 timekeeping_get_ns_raw(void)
  105. {
  106. cycle_t cycle_now, cycle_delta;
  107. struct clocksource *clock;
  108. /* read clocksource: */
  109. clock = timekeeper.clock;
  110. cycle_now = clock->read(clock);
  111. /* calculate the delta since the last update_wall_time: */
  112. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  113. /* return delta convert to nanoseconds using ntp adjusted mult. */
  114. return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  115. }
  116. /*
  117. * This read-write spinlock protects us from races in SMP while
  118. * playing with xtime.
  119. */
  120. __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  121. /*
  122. * The current time
  123. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  124. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  125. * at zero at system boot time, so wall_to_monotonic will be negative,
  126. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  127. * the usual normalization.
  128. *
  129. * wall_to_monotonic is moved after resume from suspend for the monotonic
  130. * time not to jump. We need to add total_sleep_time to wall_to_monotonic
  131. * to get the real boot based time offset.
  132. *
  133. * - wall_to_monotonic is no longer the boot time, getboottime must be
  134. * used instead.
  135. */
  136. static struct timespec xtime __attribute__ ((aligned (16)));
  137. static struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  138. static struct timespec total_sleep_time;
  139. /*
  140. * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
  141. */
  142. static struct timespec raw_time;
  143. /* flag for if timekeeping is suspended */
  144. int __read_mostly timekeeping_suspended;
  145. /* must hold xtime_lock */
  146. void timekeeping_leap_insert(int leapsecond)
  147. {
  148. xtime.tv_sec += leapsecond;
  149. wall_to_monotonic.tv_sec -= leapsecond;
  150. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  151. timekeeper.mult);
  152. }
  153. /**
  154. * timekeeping_forward_now - update clock to the current time
  155. *
  156. * Forward the current clock to update its state since the last call to
  157. * update_wall_time(). This is useful before significant clock changes,
  158. * as it avoids having to deal with this time offset explicitly.
  159. */
  160. static void timekeeping_forward_now(void)
  161. {
  162. cycle_t cycle_now, cycle_delta;
  163. struct clocksource *clock;
  164. s64 nsec;
  165. clock = timekeeper.clock;
  166. cycle_now = clock->read(clock);
  167. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  168. clock->cycle_last = cycle_now;
  169. nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  170. timekeeper.shift);
  171. /* If arch requires, add in gettimeoffset() */
  172. nsec += arch_gettimeoffset();
  173. timespec_add_ns(&xtime, nsec);
  174. nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  175. timespec_add_ns(&raw_time, nsec);
  176. }
  177. /**
  178. * getnstimeofday - Returns the time of day in a timespec
  179. * @ts: pointer to the timespec to be set
  180. *
  181. * Returns the time of day in a timespec.
  182. */
  183. void getnstimeofday(struct timespec *ts)
  184. {
  185. unsigned long seq;
  186. s64 nsecs;
  187. WARN_ON(timekeeping_suspended);
  188. do {
  189. seq = read_seqbegin(&xtime_lock);
  190. *ts = xtime;
  191. nsecs = timekeeping_get_ns();
  192. /* If arch requires, add in gettimeoffset() */
  193. nsecs += arch_gettimeoffset();
  194. } while (read_seqretry(&xtime_lock, seq));
  195. timespec_add_ns(ts, nsecs);
  196. }
  197. EXPORT_SYMBOL(getnstimeofday);
  198. ktime_t ktime_get(void)
  199. {
  200. unsigned int seq;
  201. s64 secs, nsecs;
  202. WARN_ON(timekeeping_suspended);
  203. do {
  204. seq = read_seqbegin(&xtime_lock);
  205. secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
  206. nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
  207. nsecs += timekeeping_get_ns();
  208. } while (read_seqretry(&xtime_lock, seq));
  209. /*
  210. * Use ktime_set/ktime_add_ns to create a proper ktime on
  211. * 32-bit architectures without CONFIG_KTIME_SCALAR.
  212. */
  213. return ktime_add_ns(ktime_set(secs, 0), nsecs);
  214. }
  215. EXPORT_SYMBOL_GPL(ktime_get);
  216. /**
  217. * ktime_get_ts - get the monotonic clock in timespec format
  218. * @ts: pointer to timespec variable
  219. *
  220. * The function calculates the monotonic clock from the realtime
  221. * clock and the wall_to_monotonic offset and stores the result
  222. * in normalized timespec format in the variable pointed to by @ts.
  223. */
  224. void ktime_get_ts(struct timespec *ts)
  225. {
  226. struct timespec tomono;
  227. unsigned int seq;
  228. s64 nsecs;
  229. WARN_ON(timekeeping_suspended);
  230. do {
  231. seq = read_seqbegin(&xtime_lock);
  232. *ts = xtime;
  233. tomono = wall_to_monotonic;
  234. nsecs = timekeeping_get_ns();
  235. } while (read_seqretry(&xtime_lock, seq));
  236. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  237. ts->tv_nsec + tomono.tv_nsec + nsecs);
  238. }
  239. EXPORT_SYMBOL_GPL(ktime_get_ts);
  240. #ifdef CONFIG_NTP_PPS
  241. /**
  242. * getnstime_raw_and_real - get day and raw monotonic time in timespec format
  243. * @ts_raw: pointer to the timespec to be set to raw monotonic time
  244. * @ts_real: pointer to the timespec to be set to the time of day
  245. *
  246. * This function reads both the time of day and raw monotonic time at the
  247. * same time atomically and stores the resulting timestamps in timespec
  248. * format.
  249. */
  250. void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  251. {
  252. unsigned long seq;
  253. s64 nsecs_raw, nsecs_real;
  254. WARN_ON_ONCE(timekeeping_suspended);
  255. do {
  256. u32 arch_offset;
  257. seq = read_seqbegin(&xtime_lock);
  258. *ts_raw = raw_time;
  259. *ts_real = xtime;
  260. nsecs_raw = timekeeping_get_ns_raw();
  261. nsecs_real = timekeeping_get_ns();
  262. /* If arch requires, add in gettimeoffset() */
  263. arch_offset = arch_gettimeoffset();
  264. nsecs_raw += arch_offset;
  265. nsecs_real += arch_offset;
  266. } while (read_seqretry(&xtime_lock, seq));
  267. timespec_add_ns(ts_raw, nsecs_raw);
  268. timespec_add_ns(ts_real, nsecs_real);
  269. }
  270. EXPORT_SYMBOL(getnstime_raw_and_real);
  271. #endif /* CONFIG_NTP_PPS */
  272. /**
  273. * do_gettimeofday - Returns the time of day in a timeval
  274. * @tv: pointer to the timeval to be set
  275. *
  276. * NOTE: Users should be converted to using getnstimeofday()
  277. */
  278. void do_gettimeofday(struct timeval *tv)
  279. {
  280. struct timespec now;
  281. getnstimeofday(&now);
  282. tv->tv_sec = now.tv_sec;
  283. tv->tv_usec = now.tv_nsec/1000;
  284. }
  285. EXPORT_SYMBOL(do_gettimeofday);
  286. /**
  287. * do_settimeofday - Sets the time of day
  288. * @tv: pointer to the timespec variable containing the new time
  289. *
  290. * Sets the time of day to the new time and update NTP and notify hrtimers
  291. */
  292. int do_settimeofday(const struct timespec *tv)
  293. {
  294. struct timespec ts_delta;
  295. unsigned long flags;
  296. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  297. return -EINVAL;
  298. write_seqlock_irqsave(&xtime_lock, flags);
  299. timekeeping_forward_now();
  300. ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
  301. ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
  302. wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
  303. xtime = *tv;
  304. timekeeper.ntp_error = 0;
  305. ntp_clear();
  306. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  307. timekeeper.mult);
  308. write_sequnlock_irqrestore(&xtime_lock, flags);
  309. /* signal hrtimers about time change */
  310. clock_was_set();
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(do_settimeofday);
  314. /**
  315. * timekeeping_inject_offset - Adds or subtracts from the current time.
  316. * @tv: pointer to the timespec variable containing the offset
  317. *
  318. * Adds or subtracts an offset value from the current time.
  319. */
  320. int timekeeping_inject_offset(struct timespec *ts)
  321. {
  322. unsigned long flags;
  323. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  324. return -EINVAL;
  325. write_seqlock_irqsave(&xtime_lock, flags);
  326. timekeeping_forward_now();
  327. xtime = timespec_add(xtime, *ts);
  328. wall_to_monotonic = timespec_sub(wall_to_monotonic, *ts);
  329. timekeeper.ntp_error = 0;
  330. ntp_clear();
  331. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  332. timekeeper.mult);
  333. write_sequnlock_irqrestore(&xtime_lock, flags);
  334. /* signal hrtimers about time change */
  335. clock_was_set();
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(timekeeping_inject_offset);
  339. /**
  340. * change_clocksource - Swaps clocksources if a new one is available
  341. *
  342. * Accumulates current time interval and initializes new clocksource
  343. */
  344. static int change_clocksource(void *data)
  345. {
  346. struct clocksource *new, *old;
  347. new = (struct clocksource *) data;
  348. timekeeping_forward_now();
  349. if (!new->enable || new->enable(new) == 0) {
  350. old = timekeeper.clock;
  351. timekeeper_setup_internals(new);
  352. if (old->disable)
  353. old->disable(old);
  354. }
  355. return 0;
  356. }
  357. /**
  358. * timekeeping_notify - Install a new clock source
  359. * @clock: pointer to the clock source
  360. *
  361. * This function is called from clocksource.c after a new, better clock
  362. * source has been registered. The caller holds the clocksource_mutex.
  363. */
  364. void timekeeping_notify(struct clocksource *clock)
  365. {
  366. if (timekeeper.clock == clock)
  367. return;
  368. stop_machine(change_clocksource, clock, NULL);
  369. tick_clock_notify();
  370. }
  371. /**
  372. * ktime_get_real - get the real (wall-) time in ktime_t format
  373. *
  374. * returns the time in ktime_t format
  375. */
  376. ktime_t ktime_get_real(void)
  377. {
  378. struct timespec now;
  379. getnstimeofday(&now);
  380. return timespec_to_ktime(now);
  381. }
  382. EXPORT_SYMBOL_GPL(ktime_get_real);
  383. /**
  384. * getrawmonotonic - Returns the raw monotonic time in a timespec
  385. * @ts: pointer to the timespec to be set
  386. *
  387. * Returns the raw monotonic time (completely un-modified by ntp)
  388. */
  389. void getrawmonotonic(struct timespec *ts)
  390. {
  391. unsigned long seq;
  392. s64 nsecs;
  393. do {
  394. seq = read_seqbegin(&xtime_lock);
  395. nsecs = timekeeping_get_ns_raw();
  396. *ts = raw_time;
  397. } while (read_seqretry(&xtime_lock, seq));
  398. timespec_add_ns(ts, nsecs);
  399. }
  400. EXPORT_SYMBOL(getrawmonotonic);
  401. /**
  402. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  403. */
  404. int timekeeping_valid_for_hres(void)
  405. {
  406. unsigned long seq;
  407. int ret;
  408. do {
  409. seq = read_seqbegin(&xtime_lock);
  410. ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  411. } while (read_seqretry(&xtime_lock, seq));
  412. return ret;
  413. }
  414. /**
  415. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  416. *
  417. * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
  418. * ensure that the clocksource does not change!
  419. */
  420. u64 timekeeping_max_deferment(void)
  421. {
  422. return timekeeper.clock->max_idle_ns;
  423. }
  424. /**
  425. * read_persistent_clock - Return time from the persistent clock.
  426. *
  427. * Weak dummy function for arches that do not yet support it.
  428. * Reads the time from the battery backed persistent clock.
  429. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  430. *
  431. * XXX - Do be sure to remove it once all arches implement it.
  432. */
  433. void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
  434. {
  435. ts->tv_sec = 0;
  436. ts->tv_nsec = 0;
  437. }
  438. /**
  439. * read_boot_clock - Return time of the system start.
  440. *
  441. * Weak dummy function for arches that do not yet support it.
  442. * Function to read the exact time the system has been started.
  443. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  444. *
  445. * XXX - Do be sure to remove it once all arches implement it.
  446. */
  447. void __attribute__((weak)) read_boot_clock(struct timespec *ts)
  448. {
  449. ts->tv_sec = 0;
  450. ts->tv_nsec = 0;
  451. }
  452. /*
  453. * timekeeping_init - Initializes the clocksource and common timekeeping values
  454. */
  455. void __init timekeeping_init(void)
  456. {
  457. struct clocksource *clock;
  458. unsigned long flags;
  459. struct timespec now, boot;
  460. read_persistent_clock(&now);
  461. read_boot_clock(&boot);
  462. write_seqlock_irqsave(&xtime_lock, flags);
  463. ntp_init();
  464. clock = clocksource_default_clock();
  465. if (clock->enable)
  466. clock->enable(clock);
  467. timekeeper_setup_internals(clock);
  468. xtime.tv_sec = now.tv_sec;
  469. xtime.tv_nsec = now.tv_nsec;
  470. raw_time.tv_sec = 0;
  471. raw_time.tv_nsec = 0;
  472. if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
  473. boot.tv_sec = xtime.tv_sec;
  474. boot.tv_nsec = xtime.tv_nsec;
  475. }
  476. set_normalized_timespec(&wall_to_monotonic,
  477. -boot.tv_sec, -boot.tv_nsec);
  478. total_sleep_time.tv_sec = 0;
  479. total_sleep_time.tv_nsec = 0;
  480. write_sequnlock_irqrestore(&xtime_lock, flags);
  481. }
  482. /* time in seconds when suspend began */
  483. static struct timespec timekeeping_suspend_time;
  484. /**
  485. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  486. * @delta: pointer to a timespec delta value
  487. *
  488. * Takes a timespec offset measuring a suspend interval and properly
  489. * adds the sleep offset to the timekeeping variables.
  490. */
  491. static void __timekeeping_inject_sleeptime(struct timespec *delta)
  492. {
  493. xtime = timespec_add(xtime, *delta);
  494. wall_to_monotonic = timespec_sub(wall_to_monotonic, *delta);
  495. total_sleep_time = timespec_add(total_sleep_time, *delta);
  496. }
  497. /**
  498. * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
  499. * @delta: pointer to a timespec delta value
  500. *
  501. * This hook is for architectures that cannot support read_persistent_clock
  502. * because their RTC/persistent clock is only accessible when irqs are enabled.
  503. *
  504. * This function should only be called by rtc_resume(), and allows
  505. * a suspend offset to be injected into the timekeeping values.
  506. */
  507. void timekeeping_inject_sleeptime(struct timespec *delta)
  508. {
  509. unsigned long flags;
  510. struct timespec ts;
  511. /* Make sure we don't set the clock twice */
  512. read_persistent_clock(&ts);
  513. if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
  514. return;
  515. write_seqlock_irqsave(&xtime_lock, flags);
  516. timekeeping_forward_now();
  517. __timekeeping_inject_sleeptime(delta);
  518. timekeeper.ntp_error = 0;
  519. ntp_clear();
  520. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  521. timekeeper.mult);
  522. write_sequnlock_irqrestore(&xtime_lock, flags);
  523. /* signal hrtimers about time change */
  524. clock_was_set();
  525. }
  526. /**
  527. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  528. *
  529. * This is for the generic clocksource timekeeping.
  530. * xtime/wall_to_monotonic/jiffies/etc are
  531. * still managed by arch specific suspend/resume code.
  532. */
  533. static void timekeeping_resume(void)
  534. {
  535. unsigned long flags;
  536. struct timespec ts;
  537. read_persistent_clock(&ts);
  538. clocksource_resume();
  539. write_seqlock_irqsave(&xtime_lock, flags);
  540. if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
  541. ts = timespec_sub(ts, timekeeping_suspend_time);
  542. __timekeeping_inject_sleeptime(&ts);
  543. }
  544. /* re-base the last cycle value */
  545. timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
  546. timekeeper.ntp_error = 0;
  547. timekeeping_suspended = 0;
  548. write_sequnlock_irqrestore(&xtime_lock, flags);
  549. touch_softlockup_watchdog();
  550. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  551. /* Resume hrtimers */
  552. hrtimers_resume();
  553. }
  554. static int timekeeping_suspend(void)
  555. {
  556. unsigned long flags;
  557. read_persistent_clock(&timekeeping_suspend_time);
  558. write_seqlock_irqsave(&xtime_lock, flags);
  559. timekeeping_forward_now();
  560. timekeeping_suspended = 1;
  561. write_sequnlock_irqrestore(&xtime_lock, flags);
  562. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  563. clocksource_suspend();
  564. return 0;
  565. }
  566. /* sysfs resume/suspend bits for timekeeping */
  567. static struct syscore_ops timekeeping_syscore_ops = {
  568. .resume = timekeeping_resume,
  569. .suspend = timekeeping_suspend,
  570. };
  571. static int __init timekeeping_init_ops(void)
  572. {
  573. register_syscore_ops(&timekeeping_syscore_ops);
  574. return 0;
  575. }
  576. device_initcall(timekeeping_init_ops);
  577. /*
  578. * If the error is already larger, we look ahead even further
  579. * to compensate for late or lost adjustments.
  580. */
  581. static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  582. s64 *offset)
  583. {
  584. s64 tick_error, i;
  585. u32 look_ahead, adj;
  586. s32 error2, mult;
  587. /*
  588. * Use the current error value to determine how much to look ahead.
  589. * The larger the error the slower we adjust for it to avoid problems
  590. * with losing too many ticks, otherwise we would overadjust and
  591. * produce an even larger error. The smaller the adjustment the
  592. * faster we try to adjust for it, as lost ticks can do less harm
  593. * here. This is tuned so that an error of about 1 msec is adjusted
  594. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  595. */
  596. error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
  597. error2 = abs(error2);
  598. for (look_ahead = 0; error2 > 0; look_ahead++)
  599. error2 >>= 2;
  600. /*
  601. * Now calculate the error in (1 << look_ahead) ticks, but first
  602. * remove the single look ahead already included in the error.
  603. */
  604. tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
  605. tick_error -= timekeeper.xtime_interval >> 1;
  606. error = ((error - tick_error) >> look_ahead) + tick_error;
  607. /* Finally calculate the adjustment shift value. */
  608. i = *interval;
  609. mult = 1;
  610. if (error < 0) {
  611. error = -error;
  612. *interval = -*interval;
  613. *offset = -*offset;
  614. mult = -1;
  615. }
  616. for (adj = 0; error > i; adj++)
  617. error >>= 1;
  618. *interval <<= adj;
  619. *offset <<= adj;
  620. return mult << adj;
  621. }
  622. /*
  623. * Adjust the multiplier to reduce the error value,
  624. * this is optimized for the most common adjustments of -1,0,1,
  625. * for other values we can do a bit more work.
  626. */
  627. static void timekeeping_adjust(s64 offset)
  628. {
  629. s64 error, interval = timekeeper.cycle_interval;
  630. int adj;
  631. error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
  632. if (error > interval) {
  633. error >>= 2;
  634. if (likely(error <= interval))
  635. adj = 1;
  636. else
  637. adj = timekeeping_bigadjust(error, &interval, &offset);
  638. } else if (error < -interval) {
  639. error >>= 2;
  640. if (likely(error >= -interval)) {
  641. adj = -1;
  642. interval = -interval;
  643. offset = -offset;
  644. } else
  645. adj = timekeeping_bigadjust(error, &interval, &offset);
  646. } else
  647. return;
  648. timekeeper.mult += adj;
  649. timekeeper.xtime_interval += interval;
  650. timekeeper.xtime_nsec -= offset;
  651. timekeeper.ntp_error -= (interval - offset) <<
  652. timekeeper.ntp_error_shift;
  653. }
  654. /**
  655. * logarithmic_accumulation - shifted accumulation of cycles
  656. *
  657. * This functions accumulates a shifted interval of cycles into
  658. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  659. * loop.
  660. *
  661. * Returns the unconsumed cycles.
  662. */
  663. static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
  664. {
  665. u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
  666. u64 raw_nsecs;
  667. /* If the offset is smaller then a shifted interval, do nothing */
  668. if (offset < timekeeper.cycle_interval<<shift)
  669. return offset;
  670. /* Accumulate one shifted interval */
  671. offset -= timekeeper.cycle_interval << shift;
  672. timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
  673. timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
  674. while (timekeeper.xtime_nsec >= nsecps) {
  675. timekeeper.xtime_nsec -= nsecps;
  676. xtime.tv_sec++;
  677. second_overflow();
  678. }
  679. /* Accumulate raw time */
  680. raw_nsecs = timekeeper.raw_interval << shift;
  681. raw_nsecs += raw_time.tv_nsec;
  682. if (raw_nsecs >= NSEC_PER_SEC) {
  683. u64 raw_secs = raw_nsecs;
  684. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  685. raw_time.tv_sec += raw_secs;
  686. }
  687. raw_time.tv_nsec = raw_nsecs;
  688. /* Accumulate error between NTP and clock interval */
  689. timekeeper.ntp_error += tick_length << shift;
  690. timekeeper.ntp_error -=
  691. (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
  692. (timekeeper.ntp_error_shift + shift);
  693. return offset;
  694. }
  695. /**
  696. * update_wall_time - Uses the current clocksource to increment the wall time
  697. *
  698. * Called from the timer interrupt, must hold a write on xtime_lock.
  699. */
  700. static void update_wall_time(void)
  701. {
  702. struct clocksource *clock;
  703. cycle_t offset;
  704. int shift = 0, maxshift;
  705. /* Make sure we're fully resumed: */
  706. if (unlikely(timekeeping_suspended))
  707. return;
  708. clock = timekeeper.clock;
  709. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  710. offset = timekeeper.cycle_interval;
  711. #else
  712. offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
  713. #endif
  714. timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
  715. /*
  716. * With NO_HZ we may have to accumulate many cycle_intervals
  717. * (think "ticks") worth of time at once. To do this efficiently,
  718. * we calculate the largest doubling multiple of cycle_intervals
  719. * that is smaller then the offset. We then accumulate that
  720. * chunk in one go, and then try to consume the next smaller
  721. * doubled multiple.
  722. */
  723. shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
  724. shift = max(0, shift);
  725. /* Bound shift to one less then what overflows tick_length */
  726. maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
  727. shift = min(shift, maxshift);
  728. while (offset >= timekeeper.cycle_interval) {
  729. offset = logarithmic_accumulation(offset, shift);
  730. if(offset < timekeeper.cycle_interval<<shift)
  731. shift--;
  732. }
  733. /* correct the clock when NTP error is too big */
  734. timekeeping_adjust(offset);
  735. /*
  736. * Since in the loop above, we accumulate any amount of time
  737. * in xtime_nsec over a second into xtime.tv_sec, its possible for
  738. * xtime_nsec to be fairly small after the loop. Further, if we're
  739. * slightly speeding the clocksource up in timekeeping_adjust(),
  740. * its possible the required corrective factor to xtime_nsec could
  741. * cause it to underflow.
  742. *
  743. * Now, we cannot simply roll the accumulated second back, since
  744. * the NTP subsystem has been notified via second_overflow. So
  745. * instead we push xtime_nsec forward by the amount we underflowed,
  746. * and add that amount into the error.
  747. *
  748. * We'll correct this error next time through this function, when
  749. * xtime_nsec is not as small.
  750. */
  751. if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
  752. s64 neg = -(s64)timekeeper.xtime_nsec;
  753. timekeeper.xtime_nsec = 0;
  754. timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
  755. }
  756. /*
  757. * Store full nanoseconds into xtime after rounding it up and
  758. * add the remainder to the error difference.
  759. */
  760. xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
  761. timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
  762. timekeeper.ntp_error += timekeeper.xtime_nsec <<
  763. timekeeper.ntp_error_shift;
  764. /*
  765. * Finally, make sure that after the rounding
  766. * xtime.tv_nsec isn't larger then NSEC_PER_SEC
  767. */
  768. if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
  769. xtime.tv_nsec -= NSEC_PER_SEC;
  770. xtime.tv_sec++;
  771. second_overflow();
  772. }
  773. /* check to see if there is a new clocksource to use */
  774. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  775. timekeeper.mult);
  776. }
  777. /**
  778. * getboottime - Return the real time of system boot.
  779. * @ts: pointer to the timespec to be set
  780. *
  781. * Returns the wall-time of boot in a timespec.
  782. *
  783. * This is based on the wall_to_monotonic offset and the total suspend
  784. * time. Calls to settimeofday will affect the value returned (which
  785. * basically means that however wrong your real time clock is at boot time,
  786. * you get the right time here).
  787. */
  788. void getboottime(struct timespec *ts)
  789. {
  790. struct timespec boottime = {
  791. .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
  792. .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
  793. };
  794. set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
  795. }
  796. EXPORT_SYMBOL_GPL(getboottime);
  797. /**
  798. * get_monotonic_boottime - Returns monotonic time since boot
  799. * @ts: pointer to the timespec to be set
  800. *
  801. * Returns the monotonic time since boot in a timespec.
  802. *
  803. * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
  804. * includes the time spent in suspend.
  805. */
  806. void get_monotonic_boottime(struct timespec *ts)
  807. {
  808. struct timespec tomono, sleep;
  809. unsigned int seq;
  810. s64 nsecs;
  811. WARN_ON(timekeeping_suspended);
  812. do {
  813. seq = read_seqbegin(&xtime_lock);
  814. *ts = xtime;
  815. tomono = wall_to_monotonic;
  816. sleep = total_sleep_time;
  817. nsecs = timekeeping_get_ns();
  818. } while (read_seqretry(&xtime_lock, seq));
  819. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
  820. ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
  821. }
  822. EXPORT_SYMBOL_GPL(get_monotonic_boottime);
  823. /**
  824. * ktime_get_boottime - Returns monotonic time since boot in a ktime
  825. *
  826. * Returns the monotonic time since boot in a ktime
  827. *
  828. * This is similar to CLOCK_MONTONIC/ktime_get, but also
  829. * includes the time spent in suspend.
  830. */
  831. ktime_t ktime_get_boottime(void)
  832. {
  833. struct timespec ts;
  834. get_monotonic_boottime(&ts);
  835. return timespec_to_ktime(ts);
  836. }
  837. EXPORT_SYMBOL_GPL(ktime_get_boottime);
  838. /**
  839. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  840. * @ts: pointer to the timespec to be converted
  841. */
  842. void monotonic_to_bootbased(struct timespec *ts)
  843. {
  844. *ts = timespec_add(*ts, total_sleep_time);
  845. }
  846. EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
  847. unsigned long get_seconds(void)
  848. {
  849. return xtime.tv_sec;
  850. }
  851. EXPORT_SYMBOL(get_seconds);
  852. struct timespec __current_kernel_time(void)
  853. {
  854. return xtime;
  855. }
  856. struct timespec current_kernel_time(void)
  857. {
  858. struct timespec now;
  859. unsigned long seq;
  860. do {
  861. seq = read_seqbegin(&xtime_lock);
  862. now = xtime;
  863. } while (read_seqretry(&xtime_lock, seq));
  864. return now;
  865. }
  866. EXPORT_SYMBOL(current_kernel_time);
  867. struct timespec get_monotonic_coarse(void)
  868. {
  869. struct timespec now, mono;
  870. unsigned long seq;
  871. do {
  872. seq = read_seqbegin(&xtime_lock);
  873. now = xtime;
  874. mono = wall_to_monotonic;
  875. } while (read_seqretry(&xtime_lock, seq));
  876. set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
  877. now.tv_nsec + mono.tv_nsec);
  878. return now;
  879. }
  880. /*
  881. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  882. * without sampling the sequence number in xtime_lock.
  883. * jiffies is defined in the linker script...
  884. */
  885. void do_timer(unsigned long ticks)
  886. {
  887. jiffies_64 += ticks;
  888. update_wall_time();
  889. calc_global_load(ticks);
  890. }
  891. /**
  892. * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
  893. * and sleep offsets.
  894. * @xtim: pointer to timespec to be set with xtime
  895. * @wtom: pointer to timespec to be set with wall_to_monotonic
  896. * @sleep: pointer to timespec to be set with time in suspend
  897. */
  898. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  899. struct timespec *wtom, struct timespec *sleep)
  900. {
  901. unsigned long seq;
  902. do {
  903. seq = read_seqbegin(&xtime_lock);
  904. *xtim = xtime;
  905. *wtom = wall_to_monotonic;
  906. *sleep = total_sleep_time;
  907. } while (read_seqretry(&xtime_lock, seq));
  908. }
  909. /**
  910. * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
  911. */
  912. ktime_t ktime_get_monotonic_offset(void)
  913. {
  914. unsigned long seq;
  915. struct timespec wtom;
  916. do {
  917. seq = read_seqbegin(&xtime_lock);
  918. wtom = wall_to_monotonic;
  919. } while (read_seqretry(&xtime_lock, seq));
  920. return timespec_to_ktime(wtom);
  921. }
  922. /**
  923. * xtime_update() - advances the timekeeping infrastructure
  924. * @ticks: number of ticks, that have elapsed since the last call.
  925. *
  926. * Must be called with interrupts disabled.
  927. */
  928. void xtime_update(unsigned long ticks)
  929. {
  930. write_seqlock(&xtime_lock);
  931. do_timer(ticks);
  932. write_sequnlock(&xtime_lock);
  933. }