timekeeping.c 42 KB

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