timekeeping.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. if (!timespec_valid(delta)) {
  494. printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
  495. "sleep delta value!\n");
  496. return;
  497. }
  498. xtime = timespec_add(xtime, *delta);
  499. wall_to_monotonic = timespec_sub(wall_to_monotonic, *delta);
  500. total_sleep_time = timespec_add(total_sleep_time, *delta);
  501. }
  502. /**
  503. * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
  504. * @delta: pointer to a timespec delta value
  505. *
  506. * This hook is for architectures that cannot support read_persistent_clock
  507. * because their RTC/persistent clock is only accessible when irqs are enabled.
  508. *
  509. * This function should only be called by rtc_resume(), and allows
  510. * a suspend offset to be injected into the timekeeping values.
  511. */
  512. void timekeeping_inject_sleeptime(struct timespec *delta)
  513. {
  514. unsigned long flags;
  515. struct timespec ts;
  516. /* Make sure we don't set the clock twice */
  517. read_persistent_clock(&ts);
  518. if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
  519. return;
  520. write_seqlock_irqsave(&xtime_lock, flags);
  521. timekeeping_forward_now();
  522. __timekeeping_inject_sleeptime(delta);
  523. timekeeper.ntp_error = 0;
  524. ntp_clear();
  525. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  526. timekeeper.mult);
  527. write_sequnlock_irqrestore(&xtime_lock, flags);
  528. /* signal hrtimers about time change */
  529. clock_was_set();
  530. }
  531. /**
  532. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  533. *
  534. * This is for the generic clocksource timekeeping.
  535. * xtime/wall_to_monotonic/jiffies/etc are
  536. * still managed by arch specific suspend/resume code.
  537. */
  538. static void timekeeping_resume(void)
  539. {
  540. unsigned long flags;
  541. struct timespec ts;
  542. read_persistent_clock(&ts);
  543. clocksource_resume();
  544. write_seqlock_irqsave(&xtime_lock, flags);
  545. if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
  546. ts = timespec_sub(ts, timekeeping_suspend_time);
  547. __timekeeping_inject_sleeptime(&ts);
  548. }
  549. /* re-base the last cycle value */
  550. timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
  551. timekeeper.ntp_error = 0;
  552. timekeeping_suspended = 0;
  553. write_sequnlock_irqrestore(&xtime_lock, flags);
  554. touch_softlockup_watchdog();
  555. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  556. /* Resume hrtimers */
  557. hrtimers_resume();
  558. }
  559. static int timekeeping_suspend(void)
  560. {
  561. unsigned long flags;
  562. struct timespec delta, delta_delta;
  563. static struct timespec old_delta;
  564. read_persistent_clock(&timekeeping_suspend_time);
  565. write_seqlock_irqsave(&xtime_lock, flags);
  566. timekeeping_forward_now();
  567. timekeeping_suspended = 1;
  568. /*
  569. * To avoid drift caused by repeated suspend/resumes,
  570. * which each can add ~1 second drift error,
  571. * try to compensate so the difference in system time
  572. * and persistent_clock time stays close to constant.
  573. */
  574. delta = timespec_sub(xtime, timekeeping_suspend_time);
  575. delta_delta = timespec_sub(delta, old_delta);
  576. if (abs(delta_delta.tv_sec) >= 2) {
  577. /*
  578. * if delta_delta is too large, assume time correction
  579. * has occured and set old_delta to the current delta.
  580. */
  581. old_delta = delta;
  582. } else {
  583. /* Otherwise try to adjust old_system to compensate */
  584. timekeeping_suspend_time =
  585. timespec_add(timekeeping_suspend_time, delta_delta);
  586. }
  587. write_sequnlock_irqrestore(&xtime_lock, flags);
  588. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  589. clocksource_suspend();
  590. return 0;
  591. }
  592. /* sysfs resume/suspend bits for timekeeping */
  593. static struct syscore_ops timekeeping_syscore_ops = {
  594. .resume = timekeeping_resume,
  595. .suspend = timekeeping_suspend,
  596. };
  597. static int __init timekeeping_init_ops(void)
  598. {
  599. register_syscore_ops(&timekeeping_syscore_ops);
  600. return 0;
  601. }
  602. device_initcall(timekeeping_init_ops);
  603. /*
  604. * If the error is already larger, we look ahead even further
  605. * to compensate for late or lost adjustments.
  606. */
  607. static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  608. s64 *offset)
  609. {
  610. s64 tick_error, i;
  611. u32 look_ahead, adj;
  612. s32 error2, mult;
  613. /*
  614. * Use the current error value to determine how much to look ahead.
  615. * The larger the error the slower we adjust for it to avoid problems
  616. * with losing too many ticks, otherwise we would overadjust and
  617. * produce an even larger error. The smaller the adjustment the
  618. * faster we try to adjust for it, as lost ticks can do less harm
  619. * here. This is tuned so that an error of about 1 msec is adjusted
  620. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  621. */
  622. error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
  623. error2 = abs(error2);
  624. for (look_ahead = 0; error2 > 0; look_ahead++)
  625. error2 >>= 2;
  626. /*
  627. * Now calculate the error in (1 << look_ahead) ticks, but first
  628. * remove the single look ahead already included in the error.
  629. */
  630. tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
  631. tick_error -= timekeeper.xtime_interval >> 1;
  632. error = ((error - tick_error) >> look_ahead) + tick_error;
  633. /* Finally calculate the adjustment shift value. */
  634. i = *interval;
  635. mult = 1;
  636. if (error < 0) {
  637. error = -error;
  638. *interval = -*interval;
  639. *offset = -*offset;
  640. mult = -1;
  641. }
  642. for (adj = 0; error > i; adj++)
  643. error >>= 1;
  644. *interval <<= adj;
  645. *offset <<= adj;
  646. return mult << adj;
  647. }
  648. /*
  649. * Adjust the multiplier to reduce the error value,
  650. * this is optimized for the most common adjustments of -1,0,1,
  651. * for other values we can do a bit more work.
  652. */
  653. static void timekeeping_adjust(s64 offset)
  654. {
  655. s64 error, interval = timekeeper.cycle_interval;
  656. int adj;
  657. error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
  658. if (error > interval) {
  659. error >>= 2;
  660. if (likely(error <= interval))
  661. adj = 1;
  662. else
  663. adj = timekeeping_bigadjust(error, &interval, &offset);
  664. } else if (error < -interval) {
  665. error >>= 2;
  666. if (likely(error >= -interval)) {
  667. adj = -1;
  668. interval = -interval;
  669. offset = -offset;
  670. } else
  671. adj = timekeeping_bigadjust(error, &interval, &offset);
  672. } else
  673. return;
  674. timekeeper.mult += adj;
  675. timekeeper.xtime_interval += interval;
  676. timekeeper.xtime_nsec -= offset;
  677. timekeeper.ntp_error -= (interval - offset) <<
  678. timekeeper.ntp_error_shift;
  679. }
  680. /**
  681. * logarithmic_accumulation - shifted accumulation of cycles
  682. *
  683. * This functions accumulates a shifted interval of cycles into
  684. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  685. * loop.
  686. *
  687. * Returns the unconsumed cycles.
  688. */
  689. static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
  690. {
  691. u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
  692. u64 raw_nsecs;
  693. /* If the offset is smaller then a shifted interval, do nothing */
  694. if (offset < timekeeper.cycle_interval<<shift)
  695. return offset;
  696. /* Accumulate one shifted interval */
  697. offset -= timekeeper.cycle_interval << shift;
  698. timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
  699. timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
  700. while (timekeeper.xtime_nsec >= nsecps) {
  701. timekeeper.xtime_nsec -= nsecps;
  702. xtime.tv_sec++;
  703. second_overflow();
  704. }
  705. /* Accumulate raw time */
  706. raw_nsecs = timekeeper.raw_interval << shift;
  707. raw_nsecs += raw_time.tv_nsec;
  708. if (raw_nsecs >= NSEC_PER_SEC) {
  709. u64 raw_secs = raw_nsecs;
  710. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  711. raw_time.tv_sec += raw_secs;
  712. }
  713. raw_time.tv_nsec = raw_nsecs;
  714. /* Accumulate error between NTP and clock interval */
  715. timekeeper.ntp_error += tick_length << shift;
  716. timekeeper.ntp_error -=
  717. (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
  718. (timekeeper.ntp_error_shift + shift);
  719. return offset;
  720. }
  721. /**
  722. * update_wall_time - Uses the current clocksource to increment the wall time
  723. *
  724. * Called from the timer interrupt, must hold a write on xtime_lock.
  725. */
  726. static void update_wall_time(void)
  727. {
  728. struct clocksource *clock;
  729. cycle_t offset;
  730. int shift = 0, maxshift;
  731. /* Make sure we're fully resumed: */
  732. if (unlikely(timekeeping_suspended))
  733. return;
  734. clock = timekeeper.clock;
  735. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  736. offset = timekeeper.cycle_interval;
  737. #else
  738. offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
  739. #endif
  740. timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
  741. /*
  742. * With NO_HZ we may have to accumulate many cycle_intervals
  743. * (think "ticks") worth of time at once. To do this efficiently,
  744. * we calculate the largest doubling multiple of cycle_intervals
  745. * that is smaller then the offset. We then accumulate that
  746. * chunk in one go, and then try to consume the next smaller
  747. * doubled multiple.
  748. */
  749. shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
  750. shift = max(0, shift);
  751. /* Bound shift to one less then what overflows tick_length */
  752. maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
  753. shift = min(shift, maxshift);
  754. while (offset >= timekeeper.cycle_interval) {
  755. offset = logarithmic_accumulation(offset, shift);
  756. if(offset < timekeeper.cycle_interval<<shift)
  757. shift--;
  758. }
  759. /* correct the clock when NTP error is too big */
  760. timekeeping_adjust(offset);
  761. /*
  762. * Since in the loop above, we accumulate any amount of time
  763. * in xtime_nsec over a second into xtime.tv_sec, its possible for
  764. * xtime_nsec to be fairly small after the loop. Further, if we're
  765. * slightly speeding the clocksource up in timekeeping_adjust(),
  766. * its possible the required corrective factor to xtime_nsec could
  767. * cause it to underflow.
  768. *
  769. * Now, we cannot simply roll the accumulated second back, since
  770. * the NTP subsystem has been notified via second_overflow. So
  771. * instead we push xtime_nsec forward by the amount we underflowed,
  772. * and add that amount into the error.
  773. *
  774. * We'll correct this error next time through this function, when
  775. * xtime_nsec is not as small.
  776. */
  777. if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
  778. s64 neg = -(s64)timekeeper.xtime_nsec;
  779. timekeeper.xtime_nsec = 0;
  780. timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
  781. }
  782. /*
  783. * Store full nanoseconds into xtime after rounding it up and
  784. * add the remainder to the error difference.
  785. */
  786. xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
  787. timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
  788. timekeeper.ntp_error += timekeeper.xtime_nsec <<
  789. timekeeper.ntp_error_shift;
  790. /*
  791. * Finally, make sure that after the rounding
  792. * xtime.tv_nsec isn't larger then NSEC_PER_SEC
  793. */
  794. if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
  795. xtime.tv_nsec -= NSEC_PER_SEC;
  796. xtime.tv_sec++;
  797. second_overflow();
  798. }
  799. /* check to see if there is a new clocksource to use */
  800. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  801. timekeeper.mult);
  802. }
  803. /**
  804. * getboottime - Return the real time of system boot.
  805. * @ts: pointer to the timespec to be set
  806. *
  807. * Returns the wall-time of boot in a timespec.
  808. *
  809. * This is based on the wall_to_monotonic offset and the total suspend
  810. * time. Calls to settimeofday will affect the value returned (which
  811. * basically means that however wrong your real time clock is at boot time,
  812. * you get the right time here).
  813. */
  814. void getboottime(struct timespec *ts)
  815. {
  816. struct timespec boottime = {
  817. .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
  818. .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
  819. };
  820. set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
  821. }
  822. EXPORT_SYMBOL_GPL(getboottime);
  823. /**
  824. * get_monotonic_boottime - Returns monotonic time since boot
  825. * @ts: pointer to the timespec to be set
  826. *
  827. * Returns the monotonic time since boot in a timespec.
  828. *
  829. * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
  830. * includes the time spent in suspend.
  831. */
  832. void get_monotonic_boottime(struct timespec *ts)
  833. {
  834. struct timespec tomono, sleep;
  835. unsigned int seq;
  836. s64 nsecs;
  837. WARN_ON(timekeeping_suspended);
  838. do {
  839. seq = read_seqbegin(&xtime_lock);
  840. *ts = xtime;
  841. tomono = wall_to_monotonic;
  842. sleep = total_sleep_time;
  843. nsecs = timekeeping_get_ns();
  844. } while (read_seqretry(&xtime_lock, seq));
  845. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
  846. ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
  847. }
  848. EXPORT_SYMBOL_GPL(get_monotonic_boottime);
  849. /**
  850. * ktime_get_boottime - Returns monotonic time since boot in a ktime
  851. *
  852. * Returns the monotonic time since boot in a ktime
  853. *
  854. * This is similar to CLOCK_MONTONIC/ktime_get, but also
  855. * includes the time spent in suspend.
  856. */
  857. ktime_t ktime_get_boottime(void)
  858. {
  859. struct timespec ts;
  860. get_monotonic_boottime(&ts);
  861. return timespec_to_ktime(ts);
  862. }
  863. EXPORT_SYMBOL_GPL(ktime_get_boottime);
  864. /**
  865. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  866. * @ts: pointer to the timespec to be converted
  867. */
  868. void monotonic_to_bootbased(struct timespec *ts)
  869. {
  870. *ts = timespec_add(*ts, total_sleep_time);
  871. }
  872. EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
  873. unsigned long get_seconds(void)
  874. {
  875. return xtime.tv_sec;
  876. }
  877. EXPORT_SYMBOL(get_seconds);
  878. struct timespec __current_kernel_time(void)
  879. {
  880. return xtime;
  881. }
  882. struct timespec current_kernel_time(void)
  883. {
  884. struct timespec now;
  885. unsigned long seq;
  886. do {
  887. seq = read_seqbegin(&xtime_lock);
  888. now = xtime;
  889. } while (read_seqretry(&xtime_lock, seq));
  890. return now;
  891. }
  892. EXPORT_SYMBOL(current_kernel_time);
  893. struct timespec get_monotonic_coarse(void)
  894. {
  895. struct timespec now, mono;
  896. unsigned long seq;
  897. do {
  898. seq = read_seqbegin(&xtime_lock);
  899. now = xtime;
  900. mono = wall_to_monotonic;
  901. } while (read_seqretry(&xtime_lock, seq));
  902. set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
  903. now.tv_nsec + mono.tv_nsec);
  904. return now;
  905. }
  906. /*
  907. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  908. * without sampling the sequence number in xtime_lock.
  909. * jiffies is defined in the linker script...
  910. */
  911. void do_timer(unsigned long ticks)
  912. {
  913. jiffies_64 += ticks;
  914. update_wall_time();
  915. calc_global_load(ticks);
  916. }
  917. /**
  918. * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
  919. * and sleep offsets.
  920. * @xtim: pointer to timespec to be set with xtime
  921. * @wtom: pointer to timespec to be set with wall_to_monotonic
  922. * @sleep: pointer to timespec to be set with time in suspend
  923. */
  924. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  925. struct timespec *wtom, struct timespec *sleep)
  926. {
  927. unsigned long seq;
  928. do {
  929. seq = read_seqbegin(&xtime_lock);
  930. *xtim = xtime;
  931. *wtom = wall_to_monotonic;
  932. *sleep = total_sleep_time;
  933. } while (read_seqretry(&xtime_lock, seq));
  934. }
  935. /**
  936. * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
  937. */
  938. ktime_t ktime_get_monotonic_offset(void)
  939. {
  940. unsigned long seq;
  941. struct timespec wtom;
  942. do {
  943. seq = read_seqbegin(&xtime_lock);
  944. wtom = wall_to_monotonic;
  945. } while (read_seqretry(&xtime_lock, seq));
  946. return timespec_to_ktime(wtom);
  947. }
  948. /**
  949. * xtime_update() - advances the timekeeping infrastructure
  950. * @ticks: number of ticks, that have elapsed since the last call.
  951. *
  952. * Must be called with interrupts disabled.
  953. */
  954. void xtime_update(unsigned long ticks)
  955. {
  956. write_seqlock(&xtime_lock);
  957. do_timer(ticks);
  958. write_sequnlock(&xtime_lock);
  959. }