timekeeping.c 29 KB

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