timekeeping.c 44 KB

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