timekeeping.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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 <linux/compiler.h>
  25. #include "tick-internal.h"
  26. #include "ntp_internal.h"
  27. #include "timekeeping_internal.h"
  28. #define TK_CLEAR_NTP (1 << 0)
  29. #define TK_MIRROR (1 << 1)
  30. #define TK_CLOCK_WAS_SET (1 << 2)
  31. /*
  32. * The most important data for readout fits into a single 64 byte
  33. * cache line.
  34. */
  35. static struct {
  36. seqcount_t seq;
  37. struct timekeeper timekeeper;
  38. } tk_core ____cacheline_aligned;
  39. static DEFINE_RAW_SPINLOCK(timekeeper_lock);
  40. static struct timekeeper shadow_timekeeper;
  41. /**
  42. * struct tk_fast - NMI safe timekeeper
  43. * @seq: Sequence counter for protecting updates. The lowest bit
  44. * is the index for the tk_read_base array
  45. * @base: tk_read_base array. Access is indexed by the lowest bit of
  46. * @seq.
  47. *
  48. * See @update_fast_timekeeper() below.
  49. */
  50. struct tk_fast {
  51. seqcount_t seq;
  52. struct tk_read_base base[2];
  53. };
  54. static struct tk_fast tk_fast_mono ____cacheline_aligned;
  55. /* flag for if timekeeping is suspended */
  56. int __read_mostly timekeeping_suspended;
  57. /* Flag for if there is a persistent clock on this platform */
  58. bool __read_mostly persistent_clock_exist = false;
  59. static inline void tk_normalize_xtime(struct timekeeper *tk)
  60. {
  61. while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) {
  62. tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift;
  63. tk->xtime_sec++;
  64. }
  65. }
  66. static inline struct timespec64 tk_xtime(struct timekeeper *tk)
  67. {
  68. struct timespec64 ts;
  69. ts.tv_sec = tk->xtime_sec;
  70. ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
  71. return ts;
  72. }
  73. static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
  74. {
  75. tk->xtime_sec = ts->tv_sec;
  76. tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift;
  77. }
  78. static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
  79. {
  80. tk->xtime_sec += ts->tv_sec;
  81. tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift;
  82. tk_normalize_xtime(tk);
  83. }
  84. static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
  85. {
  86. struct timespec64 tmp;
  87. /*
  88. * Verify consistency of: offset_real = -wall_to_monotonic
  89. * before modifying anything
  90. */
  91. set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
  92. -tk->wall_to_monotonic.tv_nsec);
  93. WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
  94. tk->wall_to_monotonic = wtm;
  95. set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
  96. tk->offs_real = timespec64_to_ktime(tmp);
  97. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
  98. }
  99. static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
  100. {
  101. tk->offs_boot = ktime_add(tk->offs_boot, delta);
  102. }
  103. /**
  104. * tk_setup_internals - Set up internals to use clocksource clock.
  105. *
  106. * @tk: The target timekeeper to setup.
  107. * @clock: Pointer to clocksource.
  108. *
  109. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  110. * pair and interval request.
  111. *
  112. * Unless you're the timekeeping code, you should not be using this!
  113. */
  114. static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
  115. {
  116. cycle_t interval;
  117. u64 tmp, ntpinterval;
  118. struct clocksource *old_clock;
  119. old_clock = tk->tkr.clock;
  120. tk->tkr.clock = clock;
  121. tk->tkr.read = clock->read;
  122. tk->tkr.mask = clock->mask;
  123. tk->tkr.cycle_last = tk->tkr.read(clock);
  124. /* Do the ns -> cycle conversion first, using original mult */
  125. tmp = NTP_INTERVAL_LENGTH;
  126. tmp <<= clock->shift;
  127. ntpinterval = tmp;
  128. tmp += clock->mult/2;
  129. do_div(tmp, clock->mult);
  130. if (tmp == 0)
  131. tmp = 1;
  132. interval = (cycle_t) tmp;
  133. tk->cycle_interval = interval;
  134. /* Go back from cycles -> shifted ns */
  135. tk->xtime_interval = (u64) interval * clock->mult;
  136. tk->xtime_remainder = ntpinterval - tk->xtime_interval;
  137. tk->raw_interval =
  138. ((u64) interval * clock->mult) >> clock->shift;
  139. /* if changing clocks, convert xtime_nsec shift units */
  140. if (old_clock) {
  141. int shift_change = clock->shift - old_clock->shift;
  142. if (shift_change < 0)
  143. tk->tkr.xtime_nsec >>= -shift_change;
  144. else
  145. tk->tkr.xtime_nsec <<= shift_change;
  146. }
  147. tk->tkr.shift = clock->shift;
  148. tk->ntp_error = 0;
  149. tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  150. tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
  151. /*
  152. * The timekeeper keeps its own mult values for the currently
  153. * active clocksource. These value will be adjusted via NTP
  154. * to counteract clock drifting.
  155. */
  156. tk->tkr.mult = clock->mult;
  157. tk->ntp_err_mult = 0;
  158. }
  159. /* Timekeeper helper functions. */
  160. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  161. static u32 default_arch_gettimeoffset(void) { return 0; }
  162. u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
  163. #else
  164. static inline u32 arch_gettimeoffset(void) { return 0; }
  165. #endif
  166. static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
  167. {
  168. cycle_t cycle_now, delta;
  169. s64 nsec;
  170. /* read clocksource: */
  171. cycle_now = tkr->read(tkr->clock);
  172. /* calculate the delta since the last update_wall_time: */
  173. delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
  174. nsec = delta * tkr->mult + tkr->xtime_nsec;
  175. nsec >>= tkr->shift;
  176. /* If arch requires, add in get_arch_timeoffset() */
  177. return nsec + arch_gettimeoffset();
  178. }
  179. static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
  180. {
  181. struct clocksource *clock = tk->tkr.clock;
  182. cycle_t cycle_now, delta;
  183. s64 nsec;
  184. /* read clocksource: */
  185. cycle_now = tk->tkr.read(clock);
  186. /* calculate the delta since the last update_wall_time: */
  187. delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
  188. /* convert delta to nanoseconds. */
  189. nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
  190. /* If arch requires, add in get_arch_timeoffset() */
  191. return nsec + arch_gettimeoffset();
  192. }
  193. /**
  194. * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
  195. * @tkr: Timekeeping readout base from which we take the update
  196. *
  197. * We want to use this from any context including NMI and tracing /
  198. * instrumenting the timekeeping code itself.
  199. *
  200. * So we handle this differently than the other timekeeping accessor
  201. * functions which retry when the sequence count has changed. The
  202. * update side does:
  203. *
  204. * smp_wmb(); <- Ensure that the last base[1] update is visible
  205. * tkf->seq++;
  206. * smp_wmb(); <- Ensure that the seqcount update is visible
  207. * update(tkf->base[0], tkr);
  208. * smp_wmb(); <- Ensure that the base[0] update is visible
  209. * tkf->seq++;
  210. * smp_wmb(); <- Ensure that the seqcount update is visible
  211. * update(tkf->base[1], tkr);
  212. *
  213. * The reader side does:
  214. *
  215. * do {
  216. * seq = tkf->seq;
  217. * smp_rmb();
  218. * idx = seq & 0x01;
  219. * now = now(tkf->base[idx]);
  220. * smp_rmb();
  221. * } while (seq != tkf->seq)
  222. *
  223. * As long as we update base[0] readers are forced off to
  224. * base[1]. Once base[0] is updated readers are redirected to base[0]
  225. * and the base[1] update takes place.
  226. *
  227. * So if a NMI hits the update of base[0] then it will use base[1]
  228. * which is still consistent. In the worst case this can result is a
  229. * slightly wrong timestamp (a few nanoseconds). See
  230. * @ktime_get_mono_fast_ns.
  231. */
  232. static void update_fast_timekeeper(struct tk_read_base *tkr)
  233. {
  234. struct tk_read_base *base = tk_fast_mono.base;
  235. /* Force readers off to base[1] */
  236. raw_write_seqcount_latch(&tk_fast_mono.seq);
  237. /* Update base[0] */
  238. memcpy(base, tkr, sizeof(*base));
  239. /* Force readers back to base[0] */
  240. raw_write_seqcount_latch(&tk_fast_mono.seq);
  241. /* Update base[1] */
  242. memcpy(base + 1, base, sizeof(*base));
  243. }
  244. /**
  245. * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
  246. *
  247. * This timestamp is not guaranteed to be monotonic across an update.
  248. * The timestamp is calculated by:
  249. *
  250. * now = base_mono + clock_delta * slope
  251. *
  252. * So if the update lowers the slope, readers who are forced to the
  253. * not yet updated second array are still using the old steeper slope.
  254. *
  255. * tmono
  256. * ^
  257. * | o n
  258. * | o n
  259. * | u
  260. * | o
  261. * |o
  262. * |12345678---> reader order
  263. *
  264. * o = old slope
  265. * u = update
  266. * n = new slope
  267. *
  268. * So reader 6 will observe time going backwards versus reader 5.
  269. *
  270. * While other CPUs are likely to be able observe that, the only way
  271. * for a CPU local observation is when an NMI hits in the middle of
  272. * the update. Timestamps taken from that NMI context might be ahead
  273. * of the following timestamps. Callers need to be aware of that and
  274. * deal with it.
  275. */
  276. u64 notrace ktime_get_mono_fast_ns(void)
  277. {
  278. struct tk_read_base *tkr;
  279. unsigned int seq;
  280. u64 now;
  281. do {
  282. seq = raw_read_seqcount(&tk_fast_mono.seq);
  283. tkr = tk_fast_mono.base + (seq & 0x01);
  284. now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr);
  285. } while (read_seqcount_retry(&tk_fast_mono.seq, seq));
  286. return now;
  287. }
  288. EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
  289. /* Suspend-time cycles value for halted fast timekeeper. */
  290. static cycle_t cycles_at_suspend;
  291. static cycle_t dummy_clock_read(struct clocksource *cs)
  292. {
  293. return cycles_at_suspend;
  294. }
  295. /**
  296. * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
  297. * @tk: Timekeeper to snapshot.
  298. *
  299. * It generally is unsafe to access the clocksource after timekeeping has been
  300. * suspended, so take a snapshot of the readout base of @tk and use it as the
  301. * fast timekeeper's readout base while suspended. It will return the same
  302. * number of cycles every time until timekeeping is resumed at which time the
  303. * proper readout base for the fast timekeeper will be restored automatically.
  304. */
  305. static void halt_fast_timekeeper(struct timekeeper *tk)
  306. {
  307. static struct tk_read_base tkr_dummy;
  308. struct tk_read_base *tkr = &tk->tkr;
  309. memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
  310. cycles_at_suspend = tkr->read(tkr->clock);
  311. tkr_dummy.read = dummy_clock_read;
  312. update_fast_timekeeper(&tkr_dummy);
  313. }
  314. #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
  315. static inline void update_vsyscall(struct timekeeper *tk)
  316. {
  317. struct timespec xt, wm;
  318. xt = timespec64_to_timespec(tk_xtime(tk));
  319. wm = timespec64_to_timespec(tk->wall_to_monotonic);
  320. update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult,
  321. tk->tkr.cycle_last);
  322. }
  323. static inline void old_vsyscall_fixup(struct timekeeper *tk)
  324. {
  325. s64 remainder;
  326. /*
  327. * Store only full nanoseconds into xtime_nsec after rounding
  328. * it up and add the remainder to the error difference.
  329. * XXX - This is necessary to avoid small 1ns inconsistnecies caused
  330. * by truncating the remainder in vsyscalls. However, it causes
  331. * additional work to be done in timekeeping_adjust(). Once
  332. * the vsyscall implementations are converted to use xtime_nsec
  333. * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
  334. * users are removed, this can be killed.
  335. */
  336. remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1);
  337. tk->tkr.xtime_nsec -= remainder;
  338. tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift;
  339. tk->ntp_error += remainder << tk->ntp_error_shift;
  340. tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift;
  341. }
  342. #else
  343. #define old_vsyscall_fixup(tk)
  344. #endif
  345. static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
  346. static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
  347. {
  348. raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
  349. }
  350. /**
  351. * pvclock_gtod_register_notifier - register a pvclock timedata update listener
  352. */
  353. int pvclock_gtod_register_notifier(struct notifier_block *nb)
  354. {
  355. struct timekeeper *tk = &tk_core.timekeeper;
  356. unsigned long flags;
  357. int ret;
  358. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  359. ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
  360. update_pvclock_gtod(tk, true);
  361. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  362. return ret;
  363. }
  364. EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
  365. /**
  366. * pvclock_gtod_unregister_notifier - unregister a pvclock
  367. * timedata update listener
  368. */
  369. int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
  370. {
  371. unsigned long flags;
  372. int ret;
  373. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  374. ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
  375. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  376. return ret;
  377. }
  378. EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
  379. /*
  380. * Update the ktime_t based scalar nsec members of the timekeeper
  381. */
  382. static inline void tk_update_ktime_data(struct timekeeper *tk)
  383. {
  384. u64 seconds;
  385. u32 nsec;
  386. /*
  387. * The xtime based monotonic readout is:
  388. * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
  389. * The ktime based monotonic readout is:
  390. * nsec = base_mono + now();
  391. * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
  392. */
  393. seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
  394. nsec = (u32) tk->wall_to_monotonic.tv_nsec;
  395. tk->tkr.base_mono = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
  396. /* Update the monotonic raw base */
  397. tk->base_raw = timespec64_to_ktime(tk->raw_time);
  398. /*
  399. * The sum of the nanoseconds portions of xtime and
  400. * wall_to_monotonic can be greater/equal one second. Take
  401. * this into account before updating tk->ktime_sec.
  402. */
  403. nsec += (u32)(tk->tkr.xtime_nsec >> tk->tkr.shift);
  404. if (nsec >= NSEC_PER_SEC)
  405. seconds++;
  406. tk->ktime_sec = seconds;
  407. }
  408. /* must hold timekeeper_lock */
  409. static void timekeeping_update(struct timekeeper *tk, unsigned int action)
  410. {
  411. if (action & TK_CLEAR_NTP) {
  412. tk->ntp_error = 0;
  413. ntp_clear();
  414. }
  415. tk_update_ktime_data(tk);
  416. update_vsyscall(tk);
  417. update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
  418. if (action & TK_MIRROR)
  419. memcpy(&shadow_timekeeper, &tk_core.timekeeper,
  420. sizeof(tk_core.timekeeper));
  421. update_fast_timekeeper(&tk->tkr);
  422. }
  423. /**
  424. * timekeeping_forward_now - update clock to the current time
  425. *
  426. * Forward the current clock to update its state since the last call to
  427. * update_wall_time(). This is useful before significant clock changes,
  428. * as it avoids having to deal with this time offset explicitly.
  429. */
  430. static void timekeeping_forward_now(struct timekeeper *tk)
  431. {
  432. struct clocksource *clock = tk->tkr.clock;
  433. cycle_t cycle_now, delta;
  434. s64 nsec;
  435. cycle_now = tk->tkr.read(clock);
  436. delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
  437. tk->tkr.cycle_last = cycle_now;
  438. tk->tkr.xtime_nsec += delta * tk->tkr.mult;
  439. /* If arch requires, add in get_arch_timeoffset() */
  440. tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift;
  441. tk_normalize_xtime(tk);
  442. nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
  443. timespec64_add_ns(&tk->raw_time, nsec);
  444. }
  445. /**
  446. * __getnstimeofday64 - Returns the time of day in a timespec64.
  447. * @ts: pointer to the timespec to be set
  448. *
  449. * Updates the time of day in the timespec.
  450. * Returns 0 on success, or -ve when suspended (timespec will be undefined).
  451. */
  452. int __getnstimeofday64(struct timespec64 *ts)
  453. {
  454. struct timekeeper *tk = &tk_core.timekeeper;
  455. unsigned long seq;
  456. s64 nsecs = 0;
  457. do {
  458. seq = read_seqcount_begin(&tk_core.seq);
  459. ts->tv_sec = tk->xtime_sec;
  460. nsecs = timekeeping_get_ns(&tk->tkr);
  461. } while (read_seqcount_retry(&tk_core.seq, seq));
  462. ts->tv_nsec = 0;
  463. timespec64_add_ns(ts, nsecs);
  464. /*
  465. * Do not bail out early, in case there were callers still using
  466. * the value, even in the face of the WARN_ON.
  467. */
  468. if (unlikely(timekeeping_suspended))
  469. return -EAGAIN;
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(__getnstimeofday64);
  473. /**
  474. * getnstimeofday64 - Returns the time of day in a timespec64.
  475. * @ts: pointer to the timespec64 to be set
  476. *
  477. * Returns the time of day in a timespec64 (WARN if suspended).
  478. */
  479. void getnstimeofday64(struct timespec64 *ts)
  480. {
  481. WARN_ON(__getnstimeofday64(ts));
  482. }
  483. EXPORT_SYMBOL(getnstimeofday64);
  484. ktime_t ktime_get(void)
  485. {
  486. struct timekeeper *tk = &tk_core.timekeeper;
  487. unsigned int seq;
  488. ktime_t base;
  489. s64 nsecs;
  490. WARN_ON(timekeeping_suspended);
  491. do {
  492. seq = read_seqcount_begin(&tk_core.seq);
  493. base = tk->tkr.base_mono;
  494. nsecs = timekeeping_get_ns(&tk->tkr);
  495. } while (read_seqcount_retry(&tk_core.seq, seq));
  496. return ktime_add_ns(base, nsecs);
  497. }
  498. EXPORT_SYMBOL_GPL(ktime_get);
  499. static ktime_t *offsets[TK_OFFS_MAX] = {
  500. [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
  501. [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
  502. [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
  503. };
  504. ktime_t ktime_get_with_offset(enum tk_offsets offs)
  505. {
  506. struct timekeeper *tk = &tk_core.timekeeper;
  507. unsigned int seq;
  508. ktime_t base, *offset = offsets[offs];
  509. s64 nsecs;
  510. WARN_ON(timekeeping_suspended);
  511. do {
  512. seq = read_seqcount_begin(&tk_core.seq);
  513. base = ktime_add(tk->tkr.base_mono, *offset);
  514. nsecs = timekeeping_get_ns(&tk->tkr);
  515. } while (read_seqcount_retry(&tk_core.seq, seq));
  516. return ktime_add_ns(base, nsecs);
  517. }
  518. EXPORT_SYMBOL_GPL(ktime_get_with_offset);
  519. /**
  520. * ktime_mono_to_any() - convert mononotic time to any other time
  521. * @tmono: time to convert.
  522. * @offs: which offset to use
  523. */
  524. ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
  525. {
  526. ktime_t *offset = offsets[offs];
  527. unsigned long seq;
  528. ktime_t tconv;
  529. do {
  530. seq = read_seqcount_begin(&tk_core.seq);
  531. tconv = ktime_add(tmono, *offset);
  532. } while (read_seqcount_retry(&tk_core.seq, seq));
  533. return tconv;
  534. }
  535. EXPORT_SYMBOL_GPL(ktime_mono_to_any);
  536. /**
  537. * ktime_get_raw - Returns the raw monotonic time in ktime_t format
  538. */
  539. ktime_t ktime_get_raw(void)
  540. {
  541. struct timekeeper *tk = &tk_core.timekeeper;
  542. unsigned int seq;
  543. ktime_t base;
  544. s64 nsecs;
  545. do {
  546. seq = read_seqcount_begin(&tk_core.seq);
  547. base = tk->base_raw;
  548. nsecs = timekeeping_get_ns_raw(tk);
  549. } while (read_seqcount_retry(&tk_core.seq, seq));
  550. return ktime_add_ns(base, nsecs);
  551. }
  552. EXPORT_SYMBOL_GPL(ktime_get_raw);
  553. /**
  554. * ktime_get_ts64 - get the monotonic clock in timespec64 format
  555. * @ts: pointer to timespec variable
  556. *
  557. * The function calculates the monotonic clock from the realtime
  558. * clock and the wall_to_monotonic offset and stores the result
  559. * in normalized timespec64 format in the variable pointed to by @ts.
  560. */
  561. void ktime_get_ts64(struct timespec64 *ts)
  562. {
  563. struct timekeeper *tk = &tk_core.timekeeper;
  564. struct timespec64 tomono;
  565. s64 nsec;
  566. unsigned int seq;
  567. WARN_ON(timekeeping_suspended);
  568. do {
  569. seq = read_seqcount_begin(&tk_core.seq);
  570. ts->tv_sec = tk->xtime_sec;
  571. nsec = timekeeping_get_ns(&tk->tkr);
  572. tomono = tk->wall_to_monotonic;
  573. } while (read_seqcount_retry(&tk_core.seq, seq));
  574. ts->tv_sec += tomono.tv_sec;
  575. ts->tv_nsec = 0;
  576. timespec64_add_ns(ts, nsec + tomono.tv_nsec);
  577. }
  578. EXPORT_SYMBOL_GPL(ktime_get_ts64);
  579. /**
  580. * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
  581. *
  582. * Returns the seconds portion of CLOCK_MONOTONIC with a single non
  583. * serialized read. tk->ktime_sec is of type 'unsigned long' so this
  584. * works on both 32 and 64 bit systems. On 32 bit systems the readout
  585. * covers ~136 years of uptime which should be enough to prevent
  586. * premature wrap arounds.
  587. */
  588. time64_t ktime_get_seconds(void)
  589. {
  590. struct timekeeper *tk = &tk_core.timekeeper;
  591. WARN_ON(timekeeping_suspended);
  592. return tk->ktime_sec;
  593. }
  594. EXPORT_SYMBOL_GPL(ktime_get_seconds);
  595. /**
  596. * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
  597. *
  598. * Returns the wall clock seconds since 1970. This replaces the
  599. * get_seconds() interface which is not y2038 safe on 32bit systems.
  600. *
  601. * For 64bit systems the fast access to tk->xtime_sec is preserved. On
  602. * 32bit systems the access must be protected with the sequence
  603. * counter to provide "atomic" access to the 64bit tk->xtime_sec
  604. * value.
  605. */
  606. time64_t ktime_get_real_seconds(void)
  607. {
  608. struct timekeeper *tk = &tk_core.timekeeper;
  609. time64_t seconds;
  610. unsigned int seq;
  611. if (IS_ENABLED(CONFIG_64BIT))
  612. return tk->xtime_sec;
  613. do {
  614. seq = read_seqcount_begin(&tk_core.seq);
  615. seconds = tk->xtime_sec;
  616. } while (read_seqcount_retry(&tk_core.seq, seq));
  617. return seconds;
  618. }
  619. EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
  620. #ifdef CONFIG_NTP_PPS
  621. /**
  622. * getnstime_raw_and_real - get day and raw monotonic time in timespec format
  623. * @ts_raw: pointer to the timespec to be set to raw monotonic time
  624. * @ts_real: pointer to the timespec to be set to the time of day
  625. *
  626. * This function reads both the time of day and raw monotonic time at the
  627. * same time atomically and stores the resulting timestamps in timespec
  628. * format.
  629. */
  630. void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  631. {
  632. struct timekeeper *tk = &tk_core.timekeeper;
  633. unsigned long seq;
  634. s64 nsecs_raw, nsecs_real;
  635. WARN_ON_ONCE(timekeeping_suspended);
  636. do {
  637. seq = read_seqcount_begin(&tk_core.seq);
  638. *ts_raw = timespec64_to_timespec(tk->raw_time);
  639. ts_real->tv_sec = tk->xtime_sec;
  640. ts_real->tv_nsec = 0;
  641. nsecs_raw = timekeeping_get_ns_raw(tk);
  642. nsecs_real = timekeeping_get_ns(&tk->tkr);
  643. } while (read_seqcount_retry(&tk_core.seq, seq));
  644. timespec_add_ns(ts_raw, nsecs_raw);
  645. timespec_add_ns(ts_real, nsecs_real);
  646. }
  647. EXPORT_SYMBOL(getnstime_raw_and_real);
  648. #endif /* CONFIG_NTP_PPS */
  649. /**
  650. * do_gettimeofday - Returns the time of day in a timeval
  651. * @tv: pointer to the timeval to be set
  652. *
  653. * NOTE: Users should be converted to using getnstimeofday()
  654. */
  655. void do_gettimeofday(struct timeval *tv)
  656. {
  657. struct timespec64 now;
  658. getnstimeofday64(&now);
  659. tv->tv_sec = now.tv_sec;
  660. tv->tv_usec = now.tv_nsec/1000;
  661. }
  662. EXPORT_SYMBOL(do_gettimeofday);
  663. /**
  664. * do_settimeofday64 - Sets the time of day.
  665. * @ts: pointer to the timespec64 variable containing the new time
  666. *
  667. * Sets the time of day to the new time and update NTP and notify hrtimers
  668. */
  669. int do_settimeofday64(const struct timespec64 *ts)
  670. {
  671. struct timekeeper *tk = &tk_core.timekeeper;
  672. struct timespec64 ts_delta, xt;
  673. unsigned long flags;
  674. if (!timespec64_valid_strict(ts))
  675. return -EINVAL;
  676. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  677. write_seqcount_begin(&tk_core.seq);
  678. timekeeping_forward_now(tk);
  679. xt = tk_xtime(tk);
  680. ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
  681. ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
  682. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
  683. tk_set_xtime(tk, ts);
  684. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  685. write_seqcount_end(&tk_core.seq);
  686. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  687. /* signal hrtimers about time change */
  688. clock_was_set();
  689. return 0;
  690. }
  691. EXPORT_SYMBOL(do_settimeofday64);
  692. /**
  693. * timekeeping_inject_offset - Adds or subtracts from the current time.
  694. * @tv: pointer to the timespec variable containing the offset
  695. *
  696. * Adds or subtracts an offset value from the current time.
  697. */
  698. int timekeeping_inject_offset(struct timespec *ts)
  699. {
  700. struct timekeeper *tk = &tk_core.timekeeper;
  701. unsigned long flags;
  702. struct timespec64 ts64, tmp;
  703. int ret = 0;
  704. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  705. return -EINVAL;
  706. ts64 = timespec_to_timespec64(*ts);
  707. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  708. write_seqcount_begin(&tk_core.seq);
  709. timekeeping_forward_now(tk);
  710. /* Make sure the proposed value is valid */
  711. tmp = timespec64_add(tk_xtime(tk), ts64);
  712. if (!timespec64_valid_strict(&tmp)) {
  713. ret = -EINVAL;
  714. goto error;
  715. }
  716. tk_xtime_add(tk, &ts64);
  717. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
  718. error: /* even if we error out, we forwarded the time, so call update */
  719. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  720. write_seqcount_end(&tk_core.seq);
  721. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  722. /* signal hrtimers about time change */
  723. clock_was_set();
  724. return ret;
  725. }
  726. EXPORT_SYMBOL(timekeeping_inject_offset);
  727. /**
  728. * timekeeping_get_tai_offset - Returns current TAI offset from UTC
  729. *
  730. */
  731. s32 timekeeping_get_tai_offset(void)
  732. {
  733. struct timekeeper *tk = &tk_core.timekeeper;
  734. unsigned int seq;
  735. s32 ret;
  736. do {
  737. seq = read_seqcount_begin(&tk_core.seq);
  738. ret = tk->tai_offset;
  739. } while (read_seqcount_retry(&tk_core.seq, seq));
  740. return ret;
  741. }
  742. /**
  743. * __timekeeping_set_tai_offset - Lock free worker function
  744. *
  745. */
  746. static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
  747. {
  748. tk->tai_offset = tai_offset;
  749. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
  750. }
  751. /**
  752. * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
  753. *
  754. */
  755. void timekeeping_set_tai_offset(s32 tai_offset)
  756. {
  757. struct timekeeper *tk = &tk_core.timekeeper;
  758. unsigned long flags;
  759. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  760. write_seqcount_begin(&tk_core.seq);
  761. __timekeeping_set_tai_offset(tk, tai_offset);
  762. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  763. write_seqcount_end(&tk_core.seq);
  764. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  765. clock_was_set();
  766. }
  767. /**
  768. * change_clocksource - Swaps clocksources if a new one is available
  769. *
  770. * Accumulates current time interval and initializes new clocksource
  771. */
  772. static int change_clocksource(void *data)
  773. {
  774. struct timekeeper *tk = &tk_core.timekeeper;
  775. struct clocksource *new, *old;
  776. unsigned long flags;
  777. new = (struct clocksource *) data;
  778. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  779. write_seqcount_begin(&tk_core.seq);
  780. timekeeping_forward_now(tk);
  781. /*
  782. * If the cs is in module, get a module reference. Succeeds
  783. * for built-in code (owner == NULL) as well.
  784. */
  785. if (try_module_get(new->owner)) {
  786. if (!new->enable || new->enable(new) == 0) {
  787. old = tk->tkr.clock;
  788. tk_setup_internals(tk, new);
  789. if (old->disable)
  790. old->disable(old);
  791. module_put(old->owner);
  792. } else {
  793. module_put(new->owner);
  794. }
  795. }
  796. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  797. write_seqcount_end(&tk_core.seq);
  798. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  799. return 0;
  800. }
  801. /**
  802. * timekeeping_notify - Install a new clock source
  803. * @clock: pointer to the clock source
  804. *
  805. * This function is called from clocksource.c after a new, better clock
  806. * source has been registered. The caller holds the clocksource_mutex.
  807. */
  808. int timekeeping_notify(struct clocksource *clock)
  809. {
  810. struct timekeeper *tk = &tk_core.timekeeper;
  811. if (tk->tkr.clock == clock)
  812. return 0;
  813. stop_machine(change_clocksource, clock, NULL);
  814. tick_clock_notify();
  815. return tk->tkr.clock == clock ? 0 : -1;
  816. }
  817. /**
  818. * getrawmonotonic64 - Returns the raw monotonic time in a timespec
  819. * @ts: pointer to the timespec64 to be set
  820. *
  821. * Returns the raw monotonic time (completely un-modified by ntp)
  822. */
  823. void getrawmonotonic64(struct timespec64 *ts)
  824. {
  825. struct timekeeper *tk = &tk_core.timekeeper;
  826. struct timespec64 ts64;
  827. unsigned long seq;
  828. s64 nsecs;
  829. do {
  830. seq = read_seqcount_begin(&tk_core.seq);
  831. nsecs = timekeeping_get_ns_raw(tk);
  832. ts64 = tk->raw_time;
  833. } while (read_seqcount_retry(&tk_core.seq, seq));
  834. timespec64_add_ns(&ts64, nsecs);
  835. *ts = ts64;
  836. }
  837. EXPORT_SYMBOL(getrawmonotonic64);
  838. /**
  839. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  840. */
  841. int timekeeping_valid_for_hres(void)
  842. {
  843. struct timekeeper *tk = &tk_core.timekeeper;
  844. unsigned long seq;
  845. int ret;
  846. do {
  847. seq = read_seqcount_begin(&tk_core.seq);
  848. ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  849. } while (read_seqcount_retry(&tk_core.seq, seq));
  850. return ret;
  851. }
  852. /**
  853. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  854. */
  855. u64 timekeeping_max_deferment(void)
  856. {
  857. struct timekeeper *tk = &tk_core.timekeeper;
  858. unsigned long seq;
  859. u64 ret;
  860. do {
  861. seq = read_seqcount_begin(&tk_core.seq);
  862. ret = tk->tkr.clock->max_idle_ns;
  863. } while (read_seqcount_retry(&tk_core.seq, seq));
  864. return ret;
  865. }
  866. /**
  867. * read_persistent_clock - Return time from the persistent clock.
  868. *
  869. * Weak dummy function for arches that do not yet support it.
  870. * Reads the time from the battery backed persistent clock.
  871. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  872. *
  873. * XXX - Do be sure to remove it once all arches implement it.
  874. */
  875. void __weak read_persistent_clock(struct timespec *ts)
  876. {
  877. ts->tv_sec = 0;
  878. ts->tv_nsec = 0;
  879. }
  880. /**
  881. * read_boot_clock - Return time of the system start.
  882. *
  883. * Weak dummy function for arches that do not yet support it.
  884. * Function to read the exact time the system has been started.
  885. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  886. *
  887. * XXX - Do be sure to remove it once all arches implement it.
  888. */
  889. void __weak read_boot_clock(struct timespec *ts)
  890. {
  891. ts->tv_sec = 0;
  892. ts->tv_nsec = 0;
  893. }
  894. /*
  895. * timekeeping_init - Initializes the clocksource and common timekeeping values
  896. */
  897. void __init timekeeping_init(void)
  898. {
  899. struct timekeeper *tk = &tk_core.timekeeper;
  900. struct clocksource *clock;
  901. unsigned long flags;
  902. struct timespec64 now, boot, tmp;
  903. struct timespec ts;
  904. read_persistent_clock(&ts);
  905. now = timespec_to_timespec64(ts);
  906. if (!timespec64_valid_strict(&now)) {
  907. pr_warn("WARNING: Persistent clock returned invalid value!\n"
  908. " Check your CMOS/BIOS settings.\n");
  909. now.tv_sec = 0;
  910. now.tv_nsec = 0;
  911. } else if (now.tv_sec || now.tv_nsec)
  912. persistent_clock_exist = true;
  913. read_boot_clock(&ts);
  914. boot = timespec_to_timespec64(ts);
  915. if (!timespec64_valid_strict(&boot)) {
  916. pr_warn("WARNING: Boot clock returned invalid value!\n"
  917. " Check your CMOS/BIOS settings.\n");
  918. boot.tv_sec = 0;
  919. boot.tv_nsec = 0;
  920. }
  921. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  922. write_seqcount_begin(&tk_core.seq);
  923. ntp_init();
  924. clock = clocksource_default_clock();
  925. if (clock->enable)
  926. clock->enable(clock);
  927. tk_setup_internals(tk, clock);
  928. tk_set_xtime(tk, &now);
  929. tk->raw_time.tv_sec = 0;
  930. tk->raw_time.tv_nsec = 0;
  931. tk->base_raw.tv64 = 0;
  932. if (boot.tv_sec == 0 && boot.tv_nsec == 0)
  933. boot = tk_xtime(tk);
  934. set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
  935. tk_set_wall_to_mono(tk, tmp);
  936. timekeeping_update(tk, TK_MIRROR);
  937. write_seqcount_end(&tk_core.seq);
  938. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  939. }
  940. /* time in seconds when suspend began */
  941. static struct timespec64 timekeeping_suspend_time;
  942. /**
  943. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  944. * @delta: pointer to a timespec delta value
  945. *
  946. * Takes a timespec offset measuring a suspend interval and properly
  947. * adds the sleep offset to the timekeeping variables.
  948. */
  949. static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  950. struct timespec64 *delta)
  951. {
  952. if (!timespec64_valid_strict(delta)) {
  953. printk_deferred(KERN_WARNING
  954. "__timekeeping_inject_sleeptime: Invalid "
  955. "sleep delta value!\n");
  956. return;
  957. }
  958. tk_xtime_add(tk, delta);
  959. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
  960. tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
  961. tk_debug_account_sleep_time(delta);
  962. }
  963. /**
  964. * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
  965. * @delta: pointer to a timespec64 delta value
  966. *
  967. * This hook is for architectures that cannot support read_persistent_clock
  968. * because their RTC/persistent clock is only accessible when irqs are enabled.
  969. *
  970. * This function should only be called by rtc_resume(), and allows
  971. * a suspend offset to be injected into the timekeeping values.
  972. */
  973. void timekeeping_inject_sleeptime64(struct timespec64 *delta)
  974. {
  975. struct timekeeper *tk = &tk_core.timekeeper;
  976. unsigned long flags;
  977. /*
  978. * Make sure we don't set the clock twice, as timekeeping_resume()
  979. * already did it
  980. */
  981. if (has_persistent_clock())
  982. return;
  983. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  984. write_seqcount_begin(&tk_core.seq);
  985. timekeeping_forward_now(tk);
  986. __timekeeping_inject_sleeptime(tk, delta);
  987. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  988. write_seqcount_end(&tk_core.seq);
  989. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  990. /* signal hrtimers about time change */
  991. clock_was_set();
  992. }
  993. /**
  994. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  995. *
  996. * This is for the generic clocksource timekeeping.
  997. * xtime/wall_to_monotonic/jiffies/etc are
  998. * still managed by arch specific suspend/resume code.
  999. */
  1000. void timekeeping_resume(void)
  1001. {
  1002. struct timekeeper *tk = &tk_core.timekeeper;
  1003. struct clocksource *clock = tk->tkr.clock;
  1004. unsigned long flags;
  1005. struct timespec64 ts_new, ts_delta;
  1006. struct timespec tmp;
  1007. cycle_t cycle_now, cycle_delta;
  1008. bool suspendtime_found = false;
  1009. read_persistent_clock(&tmp);
  1010. ts_new = timespec_to_timespec64(tmp);
  1011. clockevents_resume();
  1012. clocksource_resume();
  1013. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1014. write_seqcount_begin(&tk_core.seq);
  1015. /*
  1016. * After system resumes, we need to calculate the suspended time and
  1017. * compensate it for the OS time. There are 3 sources that could be
  1018. * used: Nonstop clocksource during suspend, persistent clock and rtc
  1019. * device.
  1020. *
  1021. * One specific platform may have 1 or 2 or all of them, and the
  1022. * preference will be:
  1023. * suspend-nonstop clocksource -> persistent clock -> rtc
  1024. * The less preferred source will only be tried if there is no better
  1025. * usable source. The rtc part is handled separately in rtc core code.
  1026. */
  1027. cycle_now = tk->tkr.read(clock);
  1028. if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
  1029. cycle_now > tk->tkr.cycle_last) {
  1030. u64 num, max = ULLONG_MAX;
  1031. u32 mult = clock->mult;
  1032. u32 shift = clock->shift;
  1033. s64 nsec = 0;
  1034. cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last,
  1035. tk->tkr.mask);
  1036. /*
  1037. * "cycle_delta * mutl" may cause 64 bits overflow, if the
  1038. * suspended time is too long. In that case we need do the
  1039. * 64 bits math carefully
  1040. */
  1041. do_div(max, mult);
  1042. if (cycle_delta > max) {
  1043. num = div64_u64(cycle_delta, max);
  1044. nsec = (((u64) max * mult) >> shift) * num;
  1045. cycle_delta -= num * max;
  1046. }
  1047. nsec += ((u64) cycle_delta * mult) >> shift;
  1048. ts_delta = ns_to_timespec64(nsec);
  1049. suspendtime_found = true;
  1050. } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  1051. ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
  1052. suspendtime_found = true;
  1053. }
  1054. if (suspendtime_found)
  1055. __timekeeping_inject_sleeptime(tk, &ts_delta);
  1056. /* Re-base the last cycle value */
  1057. tk->tkr.cycle_last = cycle_now;
  1058. tk->ntp_error = 0;
  1059. timekeeping_suspended = 0;
  1060. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1061. write_seqcount_end(&tk_core.seq);
  1062. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1063. touch_softlockup_watchdog();
  1064. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  1065. /* Resume hrtimers */
  1066. hrtimers_resume();
  1067. }
  1068. int timekeeping_suspend(void)
  1069. {
  1070. struct timekeeper *tk = &tk_core.timekeeper;
  1071. unsigned long flags;
  1072. struct timespec64 delta, delta_delta;
  1073. static struct timespec64 old_delta;
  1074. struct timespec tmp;
  1075. read_persistent_clock(&tmp);
  1076. timekeeping_suspend_time = timespec_to_timespec64(tmp);
  1077. /*
  1078. * On some systems the persistent_clock can not be detected at
  1079. * timekeeping_init by its return value, so if we see a valid
  1080. * value returned, update the persistent_clock_exists flag.
  1081. */
  1082. if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
  1083. persistent_clock_exist = true;
  1084. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1085. write_seqcount_begin(&tk_core.seq);
  1086. timekeeping_forward_now(tk);
  1087. timekeeping_suspended = 1;
  1088. /*
  1089. * To avoid drift caused by repeated suspend/resumes,
  1090. * which each can add ~1 second drift error,
  1091. * try to compensate so the difference in system time
  1092. * and persistent_clock time stays close to constant.
  1093. */
  1094. delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
  1095. delta_delta = timespec64_sub(delta, old_delta);
  1096. if (abs(delta_delta.tv_sec) >= 2) {
  1097. /*
  1098. * if delta_delta is too large, assume time correction
  1099. * has occured and set old_delta to the current delta.
  1100. */
  1101. old_delta = delta;
  1102. } else {
  1103. /* Otherwise try to adjust old_system to compensate */
  1104. timekeeping_suspend_time =
  1105. timespec64_add(timekeeping_suspend_time, delta_delta);
  1106. }
  1107. timekeeping_update(tk, TK_MIRROR);
  1108. halt_fast_timekeeper(tk);
  1109. write_seqcount_end(&tk_core.seq);
  1110. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1111. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  1112. clocksource_suspend();
  1113. clockevents_suspend();
  1114. return 0;
  1115. }
  1116. /* sysfs resume/suspend bits for timekeeping */
  1117. static struct syscore_ops timekeeping_syscore_ops = {
  1118. .resume = timekeeping_resume,
  1119. .suspend = timekeeping_suspend,
  1120. };
  1121. static int __init timekeeping_init_ops(void)
  1122. {
  1123. register_syscore_ops(&timekeeping_syscore_ops);
  1124. return 0;
  1125. }
  1126. device_initcall(timekeeping_init_ops);
  1127. /*
  1128. * Apply a multiplier adjustment to the timekeeper
  1129. */
  1130. static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  1131. s64 offset,
  1132. bool negative,
  1133. int adj_scale)
  1134. {
  1135. s64 interval = tk->cycle_interval;
  1136. s32 mult_adj = 1;
  1137. if (negative) {
  1138. mult_adj = -mult_adj;
  1139. interval = -interval;
  1140. offset = -offset;
  1141. }
  1142. mult_adj <<= adj_scale;
  1143. interval <<= adj_scale;
  1144. offset <<= adj_scale;
  1145. /*
  1146. * So the following can be confusing.
  1147. *
  1148. * To keep things simple, lets assume mult_adj == 1 for now.
  1149. *
  1150. * When mult_adj != 1, remember that the interval and offset values
  1151. * have been appropriately scaled so the math is the same.
  1152. *
  1153. * The basic idea here is that we're increasing the multiplier
  1154. * by one, this causes the xtime_interval to be incremented by
  1155. * one cycle_interval. This is because:
  1156. * xtime_interval = cycle_interval * mult
  1157. * So if mult is being incremented by one:
  1158. * xtime_interval = cycle_interval * (mult + 1)
  1159. * Its the same as:
  1160. * xtime_interval = (cycle_interval * mult) + cycle_interval
  1161. * Which can be shortened to:
  1162. * xtime_interval += cycle_interval
  1163. *
  1164. * So offset stores the non-accumulated cycles. Thus the current
  1165. * time (in shifted nanoseconds) is:
  1166. * now = (offset * adj) + xtime_nsec
  1167. * Now, even though we're adjusting the clock frequency, we have
  1168. * to keep time consistent. In other words, we can't jump back
  1169. * in time, and we also want to avoid jumping forward in time.
  1170. *
  1171. * So given the same offset value, we need the time to be the same
  1172. * both before and after the freq adjustment.
  1173. * now = (offset * adj_1) + xtime_nsec_1
  1174. * now = (offset * adj_2) + xtime_nsec_2
  1175. * So:
  1176. * (offset * adj_1) + xtime_nsec_1 =
  1177. * (offset * adj_2) + xtime_nsec_2
  1178. * And we know:
  1179. * adj_2 = adj_1 + 1
  1180. * So:
  1181. * (offset * adj_1) + xtime_nsec_1 =
  1182. * (offset * (adj_1+1)) + xtime_nsec_2
  1183. * (offset * adj_1) + xtime_nsec_1 =
  1184. * (offset * adj_1) + offset + xtime_nsec_2
  1185. * Canceling the sides:
  1186. * xtime_nsec_1 = offset + xtime_nsec_2
  1187. * Which gives us:
  1188. * xtime_nsec_2 = xtime_nsec_1 - offset
  1189. * Which simplfies to:
  1190. * xtime_nsec -= offset
  1191. *
  1192. * XXX - TODO: Doc ntp_error calculation.
  1193. */
  1194. if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
  1195. /* NTP adjustment caused clocksource mult overflow */
  1196. WARN_ON_ONCE(1);
  1197. return;
  1198. }
  1199. tk->tkr.mult += mult_adj;
  1200. tk->xtime_interval += interval;
  1201. tk->tkr.xtime_nsec -= offset;
  1202. tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
  1203. }
  1204. /*
  1205. * Calculate the multiplier adjustment needed to match the frequency
  1206. * specified by NTP
  1207. */
  1208. static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
  1209. s64 offset)
  1210. {
  1211. s64 interval = tk->cycle_interval;
  1212. s64 xinterval = tk->xtime_interval;
  1213. s64 tick_error;
  1214. bool negative;
  1215. u32 adj;
  1216. /* Remove any current error adj from freq calculation */
  1217. if (tk->ntp_err_mult)
  1218. xinterval -= tk->cycle_interval;
  1219. tk->ntp_tick = ntp_tick_length();
  1220. /* Calculate current error per tick */
  1221. tick_error = ntp_tick_length() >> tk->ntp_error_shift;
  1222. tick_error -= (xinterval + tk->xtime_remainder);
  1223. /* Don't worry about correcting it if its small */
  1224. if (likely((tick_error >= 0) && (tick_error <= interval)))
  1225. return;
  1226. /* preserve the direction of correction */
  1227. negative = (tick_error < 0);
  1228. /* Sort out the magnitude of the correction */
  1229. tick_error = abs(tick_error);
  1230. for (adj = 0; tick_error > interval; adj++)
  1231. tick_error >>= 1;
  1232. /* scale the corrections */
  1233. timekeeping_apply_adjustment(tk, offset, negative, adj);
  1234. }
  1235. /*
  1236. * Adjust the timekeeper's multiplier to the correct frequency
  1237. * and also to reduce the accumulated error value.
  1238. */
  1239. static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
  1240. {
  1241. /* Correct for the current frequency error */
  1242. timekeeping_freqadjust(tk, offset);
  1243. /* Next make a small adjustment to fix any cumulative error */
  1244. if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
  1245. tk->ntp_err_mult = 1;
  1246. timekeeping_apply_adjustment(tk, offset, 0, 0);
  1247. } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
  1248. /* Undo any existing error adjustment */
  1249. timekeeping_apply_adjustment(tk, offset, 1, 0);
  1250. tk->ntp_err_mult = 0;
  1251. }
  1252. if (unlikely(tk->tkr.clock->maxadj &&
  1253. (abs(tk->tkr.mult - tk->tkr.clock->mult)
  1254. > tk->tkr.clock->maxadj))) {
  1255. printk_once(KERN_WARNING
  1256. "Adjusting %s more than 11%% (%ld vs %ld)\n",
  1257. tk->tkr.clock->name, (long)tk->tkr.mult,
  1258. (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj);
  1259. }
  1260. /*
  1261. * It may be possible that when we entered this function, xtime_nsec
  1262. * was very small. Further, if we're slightly speeding the clocksource
  1263. * in the code above, its possible the required corrective factor to
  1264. * xtime_nsec could cause it to underflow.
  1265. *
  1266. * Now, since we already accumulated the second, cannot simply roll
  1267. * the accumulated second back, since the NTP subsystem has been
  1268. * notified via second_overflow. So instead we push xtime_nsec forward
  1269. * by the amount we underflowed, and add that amount into the error.
  1270. *
  1271. * We'll correct this error next time through this function, when
  1272. * xtime_nsec is not as small.
  1273. */
  1274. if (unlikely((s64)tk->tkr.xtime_nsec < 0)) {
  1275. s64 neg = -(s64)tk->tkr.xtime_nsec;
  1276. tk->tkr.xtime_nsec = 0;
  1277. tk->ntp_error += neg << tk->ntp_error_shift;
  1278. }
  1279. }
  1280. /**
  1281. * accumulate_nsecs_to_secs - Accumulates nsecs into secs
  1282. *
  1283. * Helper function that accumulates a the nsecs greater then a second
  1284. * from the xtime_nsec field to the xtime_secs field.
  1285. * It also calls into the NTP code to handle leapsecond processing.
  1286. *
  1287. */
  1288. static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
  1289. {
  1290. u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift;
  1291. unsigned int clock_set = 0;
  1292. while (tk->tkr.xtime_nsec >= nsecps) {
  1293. int leap;
  1294. tk->tkr.xtime_nsec -= nsecps;
  1295. tk->xtime_sec++;
  1296. /* Figure out if its a leap sec and apply if needed */
  1297. leap = second_overflow(tk->xtime_sec);
  1298. if (unlikely(leap)) {
  1299. struct timespec64 ts;
  1300. tk->xtime_sec += leap;
  1301. ts.tv_sec = leap;
  1302. ts.tv_nsec = 0;
  1303. tk_set_wall_to_mono(tk,
  1304. timespec64_sub(tk->wall_to_monotonic, ts));
  1305. __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
  1306. clock_set = TK_CLOCK_WAS_SET;
  1307. }
  1308. }
  1309. return clock_set;
  1310. }
  1311. /**
  1312. * logarithmic_accumulation - shifted accumulation of cycles
  1313. *
  1314. * This functions accumulates a shifted interval of cycles into
  1315. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  1316. * loop.
  1317. *
  1318. * Returns the unconsumed cycles.
  1319. */
  1320. static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
  1321. u32 shift,
  1322. unsigned int *clock_set)
  1323. {
  1324. cycle_t interval = tk->cycle_interval << shift;
  1325. u64 raw_nsecs;
  1326. /* If the offset is smaller then a shifted interval, do nothing */
  1327. if (offset < interval)
  1328. return offset;
  1329. /* Accumulate one shifted interval */
  1330. offset -= interval;
  1331. tk->tkr.cycle_last += interval;
  1332. tk->tkr.xtime_nsec += tk->xtime_interval << shift;
  1333. *clock_set |= accumulate_nsecs_to_secs(tk);
  1334. /* Accumulate raw time */
  1335. raw_nsecs = (u64)tk->raw_interval << shift;
  1336. raw_nsecs += tk->raw_time.tv_nsec;
  1337. if (raw_nsecs >= NSEC_PER_SEC) {
  1338. u64 raw_secs = raw_nsecs;
  1339. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  1340. tk->raw_time.tv_sec += raw_secs;
  1341. }
  1342. tk->raw_time.tv_nsec = raw_nsecs;
  1343. /* Accumulate error between NTP and clock interval */
  1344. tk->ntp_error += tk->ntp_tick << shift;
  1345. tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
  1346. (tk->ntp_error_shift + shift);
  1347. return offset;
  1348. }
  1349. /**
  1350. * update_wall_time - Uses the current clocksource to increment the wall time
  1351. *
  1352. */
  1353. void update_wall_time(void)
  1354. {
  1355. struct timekeeper *real_tk = &tk_core.timekeeper;
  1356. struct timekeeper *tk = &shadow_timekeeper;
  1357. cycle_t offset;
  1358. int shift = 0, maxshift;
  1359. unsigned int clock_set = 0;
  1360. unsigned long flags;
  1361. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1362. /* Make sure we're fully resumed: */
  1363. if (unlikely(timekeeping_suspended))
  1364. goto out;
  1365. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  1366. offset = real_tk->cycle_interval;
  1367. #else
  1368. offset = clocksource_delta(tk->tkr.read(tk->tkr.clock),
  1369. tk->tkr.cycle_last, tk->tkr.mask);
  1370. #endif
  1371. /* Check if there's really nothing to do */
  1372. if (offset < real_tk->cycle_interval)
  1373. goto out;
  1374. /*
  1375. * With NO_HZ we may have to accumulate many cycle_intervals
  1376. * (think "ticks") worth of time at once. To do this efficiently,
  1377. * we calculate the largest doubling multiple of cycle_intervals
  1378. * that is smaller than the offset. We then accumulate that
  1379. * chunk in one go, and then try to consume the next smaller
  1380. * doubled multiple.
  1381. */
  1382. shift = ilog2(offset) - ilog2(tk->cycle_interval);
  1383. shift = max(0, shift);
  1384. /* Bound shift to one less than what overflows tick_length */
  1385. maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
  1386. shift = min(shift, maxshift);
  1387. while (offset >= tk->cycle_interval) {
  1388. offset = logarithmic_accumulation(tk, offset, shift,
  1389. &clock_set);
  1390. if (offset < tk->cycle_interval<<shift)
  1391. shift--;
  1392. }
  1393. /* correct the clock when NTP error is too big */
  1394. timekeeping_adjust(tk, offset);
  1395. /*
  1396. * XXX This can be killed once everyone converts
  1397. * to the new update_vsyscall.
  1398. */
  1399. old_vsyscall_fixup(tk);
  1400. /*
  1401. * Finally, make sure that after the rounding
  1402. * xtime_nsec isn't larger than NSEC_PER_SEC
  1403. */
  1404. clock_set |= accumulate_nsecs_to_secs(tk);
  1405. write_seqcount_begin(&tk_core.seq);
  1406. /*
  1407. * Update the real timekeeper.
  1408. *
  1409. * We could avoid this memcpy by switching pointers, but that
  1410. * requires changes to all other timekeeper usage sites as
  1411. * well, i.e. move the timekeeper pointer getter into the
  1412. * spinlocked/seqcount protected sections. And we trade this
  1413. * memcpy under the tk_core.seq against one before we start
  1414. * updating.
  1415. */
  1416. memcpy(real_tk, tk, sizeof(*tk));
  1417. timekeeping_update(real_tk, clock_set);
  1418. write_seqcount_end(&tk_core.seq);
  1419. out:
  1420. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1421. if (clock_set)
  1422. /* Have to call _delayed version, since in irq context*/
  1423. clock_was_set_delayed();
  1424. }
  1425. /**
  1426. * getboottime64 - Return the real time of system boot.
  1427. * @ts: pointer to the timespec64 to be set
  1428. *
  1429. * Returns the wall-time of boot in a timespec64.
  1430. *
  1431. * This is based on the wall_to_monotonic offset and the total suspend
  1432. * time. Calls to settimeofday will affect the value returned (which
  1433. * basically means that however wrong your real time clock is at boot time,
  1434. * you get the right time here).
  1435. */
  1436. void getboottime64(struct timespec64 *ts)
  1437. {
  1438. struct timekeeper *tk = &tk_core.timekeeper;
  1439. ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
  1440. *ts = ktime_to_timespec64(t);
  1441. }
  1442. EXPORT_SYMBOL_GPL(getboottime64);
  1443. unsigned long get_seconds(void)
  1444. {
  1445. struct timekeeper *tk = &tk_core.timekeeper;
  1446. return tk->xtime_sec;
  1447. }
  1448. EXPORT_SYMBOL(get_seconds);
  1449. struct timespec __current_kernel_time(void)
  1450. {
  1451. struct timekeeper *tk = &tk_core.timekeeper;
  1452. return timespec64_to_timespec(tk_xtime(tk));
  1453. }
  1454. struct timespec current_kernel_time(void)
  1455. {
  1456. struct timekeeper *tk = &tk_core.timekeeper;
  1457. struct timespec64 now;
  1458. unsigned long seq;
  1459. do {
  1460. seq = read_seqcount_begin(&tk_core.seq);
  1461. now = tk_xtime(tk);
  1462. } while (read_seqcount_retry(&tk_core.seq, seq));
  1463. return timespec64_to_timespec(now);
  1464. }
  1465. EXPORT_SYMBOL(current_kernel_time);
  1466. struct timespec64 get_monotonic_coarse64(void)
  1467. {
  1468. struct timekeeper *tk = &tk_core.timekeeper;
  1469. struct timespec64 now, mono;
  1470. unsigned long seq;
  1471. do {
  1472. seq = read_seqcount_begin(&tk_core.seq);
  1473. now = tk_xtime(tk);
  1474. mono = tk->wall_to_monotonic;
  1475. } while (read_seqcount_retry(&tk_core.seq, seq));
  1476. set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
  1477. now.tv_nsec + mono.tv_nsec);
  1478. return now;
  1479. }
  1480. /*
  1481. * Must hold jiffies_lock
  1482. */
  1483. void do_timer(unsigned long ticks)
  1484. {
  1485. jiffies_64 += ticks;
  1486. calc_global_load(ticks);
  1487. }
  1488. /**
  1489. * ktime_get_update_offsets_tick - hrtimer helper
  1490. * @offs_real: pointer to storage for monotonic -> realtime offset
  1491. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1492. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1493. *
  1494. * Returns monotonic time at last tick and various offsets
  1495. */
  1496. ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
  1497. ktime_t *offs_tai)
  1498. {
  1499. struct timekeeper *tk = &tk_core.timekeeper;
  1500. unsigned int seq;
  1501. ktime_t base;
  1502. u64 nsecs;
  1503. do {
  1504. seq = read_seqcount_begin(&tk_core.seq);
  1505. base = tk->tkr.base_mono;
  1506. nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift;
  1507. *offs_real = tk->offs_real;
  1508. *offs_boot = tk->offs_boot;
  1509. *offs_tai = tk->offs_tai;
  1510. } while (read_seqcount_retry(&tk_core.seq, seq));
  1511. return ktime_add_ns(base, nsecs);
  1512. }
  1513. #ifdef CONFIG_HIGH_RES_TIMERS
  1514. /**
  1515. * ktime_get_update_offsets_now - hrtimer helper
  1516. * @offs_real: pointer to storage for monotonic -> realtime offset
  1517. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1518. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1519. *
  1520. * Returns current monotonic time and updates the offsets
  1521. * Called from hrtimer_interrupt() or retrigger_next_event()
  1522. */
  1523. ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
  1524. ktime_t *offs_tai)
  1525. {
  1526. struct timekeeper *tk = &tk_core.timekeeper;
  1527. unsigned int seq;
  1528. ktime_t base;
  1529. u64 nsecs;
  1530. do {
  1531. seq = read_seqcount_begin(&tk_core.seq);
  1532. base = tk->tkr.base_mono;
  1533. nsecs = timekeeping_get_ns(&tk->tkr);
  1534. *offs_real = tk->offs_real;
  1535. *offs_boot = tk->offs_boot;
  1536. *offs_tai = tk->offs_tai;
  1537. } while (read_seqcount_retry(&tk_core.seq, seq));
  1538. return ktime_add_ns(base, nsecs);
  1539. }
  1540. #endif
  1541. /**
  1542. * do_adjtimex() - Accessor function to NTP __do_adjtimex function
  1543. */
  1544. int do_adjtimex(struct timex *txc)
  1545. {
  1546. struct timekeeper *tk = &tk_core.timekeeper;
  1547. unsigned long flags;
  1548. struct timespec64 ts;
  1549. s32 orig_tai, tai;
  1550. int ret;
  1551. /* Validate the data before disabling interrupts */
  1552. ret = ntp_validate_timex(txc);
  1553. if (ret)
  1554. return ret;
  1555. if (txc->modes & ADJ_SETOFFSET) {
  1556. struct timespec delta;
  1557. delta.tv_sec = txc->time.tv_sec;
  1558. delta.tv_nsec = txc->time.tv_usec;
  1559. if (!(txc->modes & ADJ_NANO))
  1560. delta.tv_nsec *= 1000;
  1561. ret = timekeeping_inject_offset(&delta);
  1562. if (ret)
  1563. return ret;
  1564. }
  1565. getnstimeofday64(&ts);
  1566. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1567. write_seqcount_begin(&tk_core.seq);
  1568. orig_tai = tai = tk->tai_offset;
  1569. ret = __do_adjtimex(txc, &ts, &tai);
  1570. if (tai != orig_tai) {
  1571. __timekeeping_set_tai_offset(tk, tai);
  1572. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1573. }
  1574. write_seqcount_end(&tk_core.seq);
  1575. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1576. if (tai != orig_tai)
  1577. clock_was_set();
  1578. ntp_notify_cmos_timer();
  1579. return ret;
  1580. }
  1581. #ifdef CONFIG_NTP_PPS
  1582. /**
  1583. * hardpps() - Accessor function to NTP __hardpps function
  1584. */
  1585. void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
  1586. {
  1587. unsigned long flags;
  1588. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1589. write_seqcount_begin(&tk_core.seq);
  1590. __hardpps(phase_ts, raw_ts);
  1591. write_seqcount_end(&tk_core.seq);
  1592. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1593. }
  1594. EXPORT_SYMBOL(hardpps);
  1595. #endif
  1596. /**
  1597. * xtime_update() - advances the timekeeping infrastructure
  1598. * @ticks: number of ticks, that have elapsed since the last call.
  1599. *
  1600. * Must be called with interrupts disabled.
  1601. */
  1602. void xtime_update(unsigned long ticks)
  1603. {
  1604. write_seqlock(&jiffies_lock);
  1605. do_timer(ticks);
  1606. write_sequnlock(&jiffies_lock);
  1607. update_wall_time();
  1608. }