timekeeping.c 45 KB

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