timekeeping.c 41 KB

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