timekeeping.c 43 KB

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