timekeeping.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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. 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. 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. ntp_init();
  645. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  646. write_seqcount_begin(&timekeeper_seq);
  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->clock->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. timekeeping_update(tk, false);
  1166. out:
  1167. write_seqcount_end(&timekeeper_seq);
  1168. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1169. }
  1170. /**
  1171. * getboottime - Return the real time of system boot.
  1172. * @ts: pointer to the timespec to be set
  1173. *
  1174. * Returns the wall-time of boot in a timespec.
  1175. *
  1176. * This is based on the wall_to_monotonic offset and the total suspend
  1177. * time. Calls to settimeofday will affect the value returned (which
  1178. * basically means that however wrong your real time clock is at boot time,
  1179. * you get the right time here).
  1180. */
  1181. void getboottime(struct timespec *ts)
  1182. {
  1183. struct timekeeper *tk = &timekeeper;
  1184. struct timespec boottime = {
  1185. .tv_sec = tk->wall_to_monotonic.tv_sec +
  1186. tk->total_sleep_time.tv_sec,
  1187. .tv_nsec = tk->wall_to_monotonic.tv_nsec +
  1188. tk->total_sleep_time.tv_nsec
  1189. };
  1190. set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
  1191. }
  1192. EXPORT_SYMBOL_GPL(getboottime);
  1193. /**
  1194. * get_monotonic_boottime - Returns monotonic time since boot
  1195. * @ts: pointer to the timespec to be set
  1196. *
  1197. * Returns the monotonic time since boot in a timespec.
  1198. *
  1199. * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
  1200. * includes the time spent in suspend.
  1201. */
  1202. void get_monotonic_boottime(struct timespec *ts)
  1203. {
  1204. struct timekeeper *tk = &timekeeper;
  1205. struct timespec tomono, sleep;
  1206. s64 nsec;
  1207. unsigned int seq;
  1208. WARN_ON(timekeeping_suspended);
  1209. do {
  1210. seq = read_seqcount_begin(&timekeeper_seq);
  1211. ts->tv_sec = tk->xtime_sec;
  1212. nsec = timekeeping_get_ns(tk);
  1213. tomono = tk->wall_to_monotonic;
  1214. sleep = tk->total_sleep_time;
  1215. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1216. ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
  1217. ts->tv_nsec = 0;
  1218. timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
  1219. }
  1220. EXPORT_SYMBOL_GPL(get_monotonic_boottime);
  1221. /**
  1222. * ktime_get_boottime - Returns monotonic time since boot in a ktime
  1223. *
  1224. * Returns the monotonic time since boot in a ktime
  1225. *
  1226. * This is similar to CLOCK_MONTONIC/ktime_get, but also
  1227. * includes the time spent in suspend.
  1228. */
  1229. ktime_t ktime_get_boottime(void)
  1230. {
  1231. struct timespec ts;
  1232. get_monotonic_boottime(&ts);
  1233. return timespec_to_ktime(ts);
  1234. }
  1235. EXPORT_SYMBOL_GPL(ktime_get_boottime);
  1236. /**
  1237. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  1238. * @ts: pointer to the timespec to be converted
  1239. */
  1240. void monotonic_to_bootbased(struct timespec *ts)
  1241. {
  1242. struct timekeeper *tk = &timekeeper;
  1243. *ts = timespec_add(*ts, tk->total_sleep_time);
  1244. }
  1245. EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
  1246. unsigned long get_seconds(void)
  1247. {
  1248. struct timekeeper *tk = &timekeeper;
  1249. return tk->xtime_sec;
  1250. }
  1251. EXPORT_SYMBOL(get_seconds);
  1252. struct timespec __current_kernel_time(void)
  1253. {
  1254. struct timekeeper *tk = &timekeeper;
  1255. return tk_xtime(tk);
  1256. }
  1257. struct timespec current_kernel_time(void)
  1258. {
  1259. struct timekeeper *tk = &timekeeper;
  1260. struct timespec now;
  1261. unsigned long seq;
  1262. do {
  1263. seq = read_seqcount_begin(&timekeeper_seq);
  1264. now = tk_xtime(tk);
  1265. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1266. return now;
  1267. }
  1268. EXPORT_SYMBOL(current_kernel_time);
  1269. struct timespec get_monotonic_coarse(void)
  1270. {
  1271. struct timekeeper *tk = &timekeeper;
  1272. struct timespec now, mono;
  1273. unsigned long seq;
  1274. do {
  1275. seq = read_seqcount_begin(&timekeeper_seq);
  1276. now = tk_xtime(tk);
  1277. mono = tk->wall_to_monotonic;
  1278. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1279. set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
  1280. now.tv_nsec + mono.tv_nsec);
  1281. return now;
  1282. }
  1283. /*
  1284. * Must hold jiffies_lock
  1285. */
  1286. void do_timer(unsigned long ticks)
  1287. {
  1288. jiffies_64 += ticks;
  1289. update_wall_time();
  1290. calc_global_load(ticks);
  1291. }
  1292. /**
  1293. * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
  1294. * and sleep offsets.
  1295. * @xtim: pointer to timespec to be set with xtime
  1296. * @wtom: pointer to timespec to be set with wall_to_monotonic
  1297. * @sleep: pointer to timespec to be set with time in suspend
  1298. */
  1299. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  1300. struct timespec *wtom, struct timespec *sleep)
  1301. {
  1302. struct timekeeper *tk = &timekeeper;
  1303. unsigned long seq;
  1304. do {
  1305. seq = read_seqcount_begin(&timekeeper_seq);
  1306. *xtim = tk_xtime(tk);
  1307. *wtom = tk->wall_to_monotonic;
  1308. *sleep = tk->total_sleep_time;
  1309. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1310. }
  1311. #ifdef CONFIG_HIGH_RES_TIMERS
  1312. /**
  1313. * ktime_get_update_offsets - hrtimer helper
  1314. * @offs_real: pointer to storage for monotonic -> realtime offset
  1315. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1316. *
  1317. * Returns current monotonic time and updates the offsets
  1318. * Called from hrtimer_interupt() or retrigger_next_event()
  1319. */
  1320. ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
  1321. ktime_t *offs_tai)
  1322. {
  1323. struct timekeeper *tk = &timekeeper;
  1324. ktime_t now;
  1325. unsigned int seq;
  1326. u64 secs, nsecs;
  1327. do {
  1328. seq = read_seqcount_begin(&timekeeper_seq);
  1329. secs = tk->xtime_sec;
  1330. nsecs = timekeeping_get_ns(tk);
  1331. *offs_real = tk->offs_real;
  1332. *offs_boot = tk->offs_boot;
  1333. *offs_tai = tk->offs_tai;
  1334. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1335. now = ktime_add_ns(ktime_set(secs, 0), nsecs);
  1336. now = ktime_sub(now, *offs_real);
  1337. return now;
  1338. }
  1339. #endif
  1340. /**
  1341. * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
  1342. */
  1343. ktime_t ktime_get_monotonic_offset(void)
  1344. {
  1345. struct timekeeper *tk = &timekeeper;
  1346. unsigned long seq;
  1347. struct timespec wtom;
  1348. do {
  1349. seq = read_seqcount_begin(&timekeeper_seq);
  1350. wtom = tk->wall_to_monotonic;
  1351. } while (read_seqcount_retry(&timekeeper_seq, seq));
  1352. return timespec_to_ktime(wtom);
  1353. }
  1354. EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
  1355. /**
  1356. * do_adjtimex() - Accessor function to NTP __do_adjtimex function
  1357. */
  1358. int do_adjtimex(struct timex *txc)
  1359. {
  1360. struct timespec ts;
  1361. s32 tai, orig_tai;
  1362. int ret;
  1363. /* Validate the data before disabling interrupts */
  1364. ret = ntp_validate_timex(txc);
  1365. if (ret)
  1366. return ret;
  1367. if (txc->modes & ADJ_SETOFFSET) {
  1368. struct timespec delta;
  1369. delta.tv_sec = txc->time.tv_sec;
  1370. delta.tv_nsec = txc->time.tv_usec;
  1371. if (!(txc->modes & ADJ_NANO))
  1372. delta.tv_nsec *= 1000;
  1373. ret = timekeeping_inject_offset(&delta);
  1374. if (ret)
  1375. return ret;
  1376. }
  1377. getnstimeofday(&ts);
  1378. orig_tai = tai = timekeeping_get_tai_offset();
  1379. ret = __do_adjtimex(txc, &ts, &tai);
  1380. if (tai != orig_tai)
  1381. timekeeping_set_tai_offset(tai);
  1382. return ret;
  1383. }
  1384. #ifdef CONFIG_NTP_PPS
  1385. /**
  1386. * hardpps() - Accessor function to NTP __hardpps function
  1387. */
  1388. void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
  1389. {
  1390. __hardpps(phase_ts, raw_ts);
  1391. }
  1392. EXPORT_SYMBOL(hardpps);
  1393. #endif
  1394. /**
  1395. * xtime_update() - advances the timekeeping infrastructure
  1396. * @ticks: number of ticks, that have elapsed since the last call.
  1397. *
  1398. * Must be called with interrupts disabled.
  1399. */
  1400. void xtime_update(unsigned long ticks)
  1401. {
  1402. write_seqlock(&jiffies_lock);
  1403. do_timer(ticks);
  1404. write_sequnlock(&jiffies_lock);
  1405. }