timekeeping.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/percpu.h>
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/sysdev.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/time.h>
  20. #include <linux/tick.h>
  21. #include <linux/stop_machine.h>
  22. /* Structure holding internal timekeeping values. */
  23. struct timekeeper {
  24. /* Current clocksource used for timekeeping. */
  25. struct clocksource *clock;
  26. /* The shift value of the current clocksource. */
  27. int shift;
  28. /* Number of clock cycles in one NTP interval. */
  29. cycle_t cycle_interval;
  30. /* Number of clock shifted nano seconds in one NTP interval. */
  31. u64 xtime_interval;
  32. /* shifted nano seconds left over when rounding cycle_interval */
  33. s64 xtime_remainder;
  34. /* Raw nano seconds accumulated per NTP interval. */
  35. u32 raw_interval;
  36. /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
  37. u64 xtime_nsec;
  38. /* Difference between accumulated time and NTP time in ntp
  39. * shifted nano seconds. */
  40. s64 ntp_error;
  41. /* Shift conversion between clock shifted nano seconds and
  42. * ntp shifted nano seconds. */
  43. int ntp_error_shift;
  44. /* NTP adjusted clock multiplier */
  45. u32 mult;
  46. };
  47. static struct timekeeper timekeeper;
  48. /**
  49. * timekeeper_setup_internals - Set up internals to use clocksource clock.
  50. *
  51. * @clock: Pointer to clocksource.
  52. *
  53. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  54. * pair and interval request.
  55. *
  56. * Unless you're the timekeeping code, you should not be using this!
  57. */
  58. static void timekeeper_setup_internals(struct clocksource *clock)
  59. {
  60. cycle_t interval;
  61. u64 tmp, ntpinterval;
  62. timekeeper.clock = clock;
  63. clock->cycle_last = clock->read(clock);
  64. /* Do the ns -> cycle conversion first, using original mult */
  65. tmp = NTP_INTERVAL_LENGTH;
  66. tmp <<= clock->shift;
  67. ntpinterval = tmp;
  68. tmp += clock->mult/2;
  69. do_div(tmp, clock->mult);
  70. if (tmp == 0)
  71. tmp = 1;
  72. interval = (cycle_t) tmp;
  73. timekeeper.cycle_interval = interval;
  74. /* Go back from cycles -> shifted ns */
  75. timekeeper.xtime_interval = (u64) interval * clock->mult;
  76. timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
  77. timekeeper.raw_interval =
  78. ((u64) interval * clock->mult) >> clock->shift;
  79. timekeeper.xtime_nsec = 0;
  80. timekeeper.shift = clock->shift;
  81. timekeeper.ntp_error = 0;
  82. timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  83. /*
  84. * The timekeeper keeps its own mult values for the currently
  85. * active clocksource. These value will be adjusted via NTP
  86. * to counteract clock drifting.
  87. */
  88. timekeeper.mult = clock->mult;
  89. }
  90. /* Timekeeper helper functions. */
  91. static inline s64 timekeeping_get_ns(void)
  92. {
  93. cycle_t cycle_now, cycle_delta;
  94. struct clocksource *clock;
  95. /* read clocksource: */
  96. clock = timekeeper.clock;
  97. cycle_now = clock->read(clock);
  98. /* calculate the delta since the last update_wall_time: */
  99. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  100. /* return delta convert to nanoseconds using ntp adjusted mult. */
  101. return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  102. timekeeper.shift);
  103. }
  104. static inline s64 timekeeping_get_ns_raw(void)
  105. {
  106. cycle_t cycle_now, cycle_delta;
  107. struct clocksource *clock;
  108. /* read clocksource: */
  109. clock = timekeeper.clock;
  110. cycle_now = clock->read(clock);
  111. /* calculate the delta since the last update_wall_time: */
  112. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  113. /* return delta convert to nanoseconds using ntp adjusted mult. */
  114. return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  115. }
  116. /*
  117. * This read-write spinlock protects us from races in SMP while
  118. * playing with xtime.
  119. */
  120. __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  121. /*
  122. * The current time
  123. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  124. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  125. * at zero at system boot time, so wall_to_monotonic will be negative,
  126. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  127. * the usual normalization.
  128. *
  129. * wall_to_monotonic is moved after resume from suspend for the monotonic
  130. * time not to jump. We need to add total_sleep_time to wall_to_monotonic
  131. * to get the real boot based time offset.
  132. *
  133. * - wall_to_monotonic is no longer the boot time, getboottime must be
  134. * used instead.
  135. */
  136. static struct timespec xtime __attribute__ ((aligned (16)));
  137. static struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  138. static struct timespec total_sleep_time;
  139. /*
  140. * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
  141. */
  142. static struct timespec raw_time;
  143. /* flag for if timekeeping is suspended */
  144. int __read_mostly timekeeping_suspended;
  145. /* must hold xtime_lock */
  146. void timekeeping_leap_insert(int leapsecond)
  147. {
  148. xtime.tv_sec += leapsecond;
  149. wall_to_monotonic.tv_sec -= leapsecond;
  150. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  151. timekeeper.mult);
  152. }
  153. /**
  154. * timekeeping_forward_now - update clock to the current time
  155. *
  156. * Forward the current clock to update its state since the last call to
  157. * update_wall_time(). This is useful before significant clock changes,
  158. * as it avoids having to deal with this time offset explicitly.
  159. */
  160. static void timekeeping_forward_now(void)
  161. {
  162. cycle_t cycle_now, cycle_delta;
  163. struct clocksource *clock;
  164. s64 nsec;
  165. clock = timekeeper.clock;
  166. cycle_now = clock->read(clock);
  167. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  168. clock->cycle_last = cycle_now;
  169. nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
  170. timekeeper.shift);
  171. /* If arch requires, add in gettimeoffset() */
  172. nsec += arch_gettimeoffset();
  173. timespec_add_ns(&xtime, nsec);
  174. nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
  175. timespec_add_ns(&raw_time, nsec);
  176. }
  177. /**
  178. * getnstimeofday - Returns the time of day in a timespec
  179. * @ts: pointer to the timespec to be set
  180. *
  181. * Returns the time of day in a timespec.
  182. */
  183. void getnstimeofday(struct timespec *ts)
  184. {
  185. unsigned long seq;
  186. s64 nsecs;
  187. WARN_ON(timekeeping_suspended);
  188. do {
  189. seq = read_seqbegin(&xtime_lock);
  190. *ts = xtime;
  191. nsecs = timekeeping_get_ns();
  192. /* If arch requires, add in gettimeoffset() */
  193. nsecs += arch_gettimeoffset();
  194. } while (read_seqretry(&xtime_lock, seq));
  195. timespec_add_ns(ts, nsecs);
  196. }
  197. EXPORT_SYMBOL(getnstimeofday);
  198. ktime_t ktime_get(void)
  199. {
  200. unsigned int seq;
  201. s64 secs, nsecs;
  202. WARN_ON(timekeeping_suspended);
  203. do {
  204. seq = read_seqbegin(&xtime_lock);
  205. secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
  206. nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
  207. nsecs += timekeeping_get_ns();
  208. } while (read_seqretry(&xtime_lock, seq));
  209. /*
  210. * Use ktime_set/ktime_add_ns to create a proper ktime on
  211. * 32-bit architectures without CONFIG_KTIME_SCALAR.
  212. */
  213. return ktime_add_ns(ktime_set(secs, 0), nsecs);
  214. }
  215. EXPORT_SYMBOL_GPL(ktime_get);
  216. /**
  217. * ktime_get_ts - get the monotonic clock in timespec format
  218. * @ts: pointer to timespec variable
  219. *
  220. * The function calculates the monotonic clock from the realtime
  221. * clock and the wall_to_monotonic offset and stores the result
  222. * in normalized timespec format in the variable pointed to by @ts.
  223. */
  224. void ktime_get_ts(struct timespec *ts)
  225. {
  226. struct timespec tomono;
  227. unsigned int seq;
  228. s64 nsecs;
  229. WARN_ON(timekeeping_suspended);
  230. do {
  231. seq = read_seqbegin(&xtime_lock);
  232. *ts = xtime;
  233. tomono = wall_to_monotonic;
  234. nsecs = timekeeping_get_ns();
  235. } while (read_seqretry(&xtime_lock, seq));
  236. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  237. ts->tv_nsec + tomono.tv_nsec + nsecs);
  238. }
  239. EXPORT_SYMBOL_GPL(ktime_get_ts);
  240. #ifdef CONFIG_NTP_PPS
  241. /**
  242. * getnstime_raw_and_real - get day and raw monotonic time in timespec format
  243. * @ts_raw: pointer to the timespec to be set to raw monotonic time
  244. * @ts_real: pointer to the timespec to be set to the time of day
  245. *
  246. * This function reads both the time of day and raw monotonic time at the
  247. * same time atomically and stores the resulting timestamps in timespec
  248. * format.
  249. */
  250. void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  251. {
  252. unsigned long seq;
  253. s64 nsecs_raw, nsecs_real;
  254. WARN_ON_ONCE(timekeeping_suspended);
  255. do {
  256. u32 arch_offset;
  257. seq = read_seqbegin(&xtime_lock);
  258. *ts_raw = raw_time;
  259. *ts_real = xtime;
  260. nsecs_raw = timekeeping_get_ns_raw();
  261. nsecs_real = timekeeping_get_ns();
  262. /* If arch requires, add in gettimeoffset() */
  263. arch_offset = arch_gettimeoffset();
  264. nsecs_raw += arch_offset;
  265. nsecs_real += arch_offset;
  266. } while (read_seqretry(&xtime_lock, seq));
  267. timespec_add_ns(ts_raw, nsecs_raw);
  268. timespec_add_ns(ts_real, nsecs_real);
  269. }
  270. EXPORT_SYMBOL(getnstime_raw_and_real);
  271. #endif /* CONFIG_NTP_PPS */
  272. /**
  273. * do_gettimeofday - Returns the time of day in a timeval
  274. * @tv: pointer to the timeval to be set
  275. *
  276. * NOTE: Users should be converted to using getnstimeofday()
  277. */
  278. void do_gettimeofday(struct timeval *tv)
  279. {
  280. struct timespec now;
  281. getnstimeofday(&now);
  282. tv->tv_sec = now.tv_sec;
  283. tv->tv_usec = now.tv_nsec/1000;
  284. }
  285. EXPORT_SYMBOL(do_gettimeofday);
  286. /**
  287. * do_settimeofday - Sets the time of day
  288. * @tv: pointer to the timespec variable containing the new time
  289. *
  290. * Sets the time of day to the new time and update NTP and notify hrtimers
  291. */
  292. int do_settimeofday(const struct timespec *tv)
  293. {
  294. struct timespec ts_delta;
  295. unsigned long flags;
  296. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  297. return -EINVAL;
  298. write_seqlock_irqsave(&xtime_lock, flags);
  299. timekeeping_forward_now();
  300. ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
  301. ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
  302. wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
  303. xtime = *tv;
  304. timekeeper.ntp_error = 0;
  305. ntp_clear();
  306. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  307. timekeeper.mult);
  308. write_sequnlock_irqrestore(&xtime_lock, flags);
  309. /* signal hrtimers about time change */
  310. clock_was_set();
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(do_settimeofday);
  314. /**
  315. * timekeeping_inject_offset - Adds or subtracts from the current time.
  316. * @tv: pointer to the timespec variable containing the offset
  317. *
  318. * Adds or subtracts an offset value from the current time.
  319. */
  320. int timekeeping_inject_offset(struct timespec *ts)
  321. {
  322. unsigned long flags;
  323. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  324. return -EINVAL;
  325. write_seqlock_irqsave(&xtime_lock, flags);
  326. timekeeping_forward_now();
  327. xtime = timespec_add(xtime, *ts);
  328. wall_to_monotonic = timespec_sub(wall_to_monotonic, *ts);
  329. timekeeper.ntp_error = 0;
  330. ntp_clear();
  331. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  332. timekeeper.mult);
  333. write_sequnlock_irqrestore(&xtime_lock, flags);
  334. /* signal hrtimers about time change */
  335. clock_was_set();
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(timekeeping_inject_offset);
  339. /**
  340. * change_clocksource - Swaps clocksources if a new one is available
  341. *
  342. * Accumulates current time interval and initializes new clocksource
  343. */
  344. static int change_clocksource(void *data)
  345. {
  346. struct clocksource *new, *old;
  347. new = (struct clocksource *) data;
  348. timekeeping_forward_now();
  349. if (!new->enable || new->enable(new) == 0) {
  350. old = timekeeper.clock;
  351. timekeeper_setup_internals(new);
  352. if (old->disable)
  353. old->disable(old);
  354. }
  355. return 0;
  356. }
  357. /**
  358. * timekeeping_notify - Install a new clock source
  359. * @clock: pointer to the clock source
  360. *
  361. * This function is called from clocksource.c after a new, better clock
  362. * source has been registered. The caller holds the clocksource_mutex.
  363. */
  364. void timekeeping_notify(struct clocksource *clock)
  365. {
  366. if (timekeeper.clock == clock)
  367. return;
  368. stop_machine(change_clocksource, clock, NULL);
  369. tick_clock_notify();
  370. }
  371. /**
  372. * ktime_get_real - get the real (wall-) time in ktime_t format
  373. *
  374. * returns the time in ktime_t format
  375. */
  376. ktime_t ktime_get_real(void)
  377. {
  378. struct timespec now;
  379. getnstimeofday(&now);
  380. return timespec_to_ktime(now);
  381. }
  382. EXPORT_SYMBOL_GPL(ktime_get_real);
  383. /**
  384. * getrawmonotonic - Returns the raw monotonic time in a timespec
  385. * @ts: pointer to the timespec to be set
  386. *
  387. * Returns the raw monotonic time (completely un-modified by ntp)
  388. */
  389. void getrawmonotonic(struct timespec *ts)
  390. {
  391. unsigned long seq;
  392. s64 nsecs;
  393. do {
  394. seq = read_seqbegin(&xtime_lock);
  395. nsecs = timekeeping_get_ns_raw();
  396. *ts = raw_time;
  397. } while (read_seqretry(&xtime_lock, seq));
  398. timespec_add_ns(ts, nsecs);
  399. }
  400. EXPORT_SYMBOL(getrawmonotonic);
  401. /**
  402. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  403. */
  404. int timekeeping_valid_for_hres(void)
  405. {
  406. unsigned long seq;
  407. int ret;
  408. do {
  409. seq = read_seqbegin(&xtime_lock);
  410. ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  411. } while (read_seqretry(&xtime_lock, seq));
  412. return ret;
  413. }
  414. /**
  415. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  416. *
  417. * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
  418. * ensure that the clocksource does not change!
  419. */
  420. u64 timekeeping_max_deferment(void)
  421. {
  422. return timekeeper.clock->max_idle_ns;
  423. }
  424. /**
  425. * read_persistent_clock - Return time from the persistent clock.
  426. *
  427. * Weak dummy function for arches that do not yet support it.
  428. * Reads the time from the battery backed persistent clock.
  429. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  430. *
  431. * XXX - Do be sure to remove it once all arches implement it.
  432. */
  433. void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
  434. {
  435. ts->tv_sec = 0;
  436. ts->tv_nsec = 0;
  437. }
  438. /**
  439. * read_boot_clock - Return time of the system start.
  440. *
  441. * Weak dummy function for arches that do not yet support it.
  442. * Function to read the exact time the system has been started.
  443. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  444. *
  445. * XXX - Do be sure to remove it once all arches implement it.
  446. */
  447. void __attribute__((weak)) read_boot_clock(struct timespec *ts)
  448. {
  449. ts->tv_sec = 0;
  450. ts->tv_nsec = 0;
  451. }
  452. /*
  453. * timekeeping_init - Initializes the clocksource and common timekeeping values
  454. */
  455. void __init timekeeping_init(void)
  456. {
  457. struct clocksource *clock;
  458. unsigned long flags;
  459. struct timespec now, boot;
  460. read_persistent_clock(&now);
  461. read_boot_clock(&boot);
  462. write_seqlock_irqsave(&xtime_lock, flags);
  463. ntp_init();
  464. clock = clocksource_default_clock();
  465. if (clock->enable)
  466. clock->enable(clock);
  467. timekeeper_setup_internals(clock);
  468. xtime.tv_sec = now.tv_sec;
  469. xtime.tv_nsec = now.tv_nsec;
  470. raw_time.tv_sec = 0;
  471. raw_time.tv_nsec = 0;
  472. if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
  473. boot.tv_sec = xtime.tv_sec;
  474. boot.tv_nsec = xtime.tv_nsec;
  475. }
  476. set_normalized_timespec(&wall_to_monotonic,
  477. -boot.tv_sec, -boot.tv_nsec);
  478. total_sleep_time.tv_sec = 0;
  479. total_sleep_time.tv_nsec = 0;
  480. write_sequnlock_irqrestore(&xtime_lock, flags);
  481. }
  482. /* time in seconds when suspend began */
  483. static struct timespec timekeeping_suspend_time;
  484. /**
  485. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  486. * @dev: unused
  487. *
  488. * This is for the generic clocksource timekeeping.
  489. * xtime/wall_to_monotonic/jiffies/etc are
  490. * still managed by arch specific suspend/resume code.
  491. */
  492. static int timekeeping_resume(struct sys_device *dev)
  493. {
  494. unsigned long flags;
  495. struct timespec ts;
  496. read_persistent_clock(&ts);
  497. clocksource_resume();
  498. write_seqlock_irqsave(&xtime_lock, flags);
  499. if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
  500. ts = timespec_sub(ts, timekeeping_suspend_time);
  501. xtime = timespec_add(xtime, ts);
  502. wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
  503. total_sleep_time = timespec_add(total_sleep_time, ts);
  504. }
  505. /* re-base the last cycle value */
  506. timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
  507. timekeeper.ntp_error = 0;
  508. timekeeping_suspended = 0;
  509. write_sequnlock_irqrestore(&xtime_lock, flags);
  510. touch_softlockup_watchdog();
  511. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  512. /* Resume hrtimers */
  513. hres_timers_resume();
  514. return 0;
  515. }
  516. static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
  517. {
  518. unsigned long flags;
  519. read_persistent_clock(&timekeeping_suspend_time);
  520. write_seqlock_irqsave(&xtime_lock, flags);
  521. timekeeping_forward_now();
  522. timekeeping_suspended = 1;
  523. write_sequnlock_irqrestore(&xtime_lock, flags);
  524. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  525. clocksource_suspend();
  526. return 0;
  527. }
  528. /* sysfs resume/suspend bits for timekeeping */
  529. static struct sysdev_class timekeeping_sysclass = {
  530. .name = "timekeeping",
  531. .resume = timekeeping_resume,
  532. .suspend = timekeeping_suspend,
  533. };
  534. static struct sys_device device_timer = {
  535. .id = 0,
  536. .cls = &timekeeping_sysclass,
  537. };
  538. static int __init timekeeping_init_device(void)
  539. {
  540. int error = sysdev_class_register(&timekeeping_sysclass);
  541. if (!error)
  542. error = sysdev_register(&device_timer);
  543. return error;
  544. }
  545. device_initcall(timekeeping_init_device);
  546. /*
  547. * If the error is already larger, we look ahead even further
  548. * to compensate for late or lost adjustments.
  549. */
  550. static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
  551. s64 *offset)
  552. {
  553. s64 tick_error, i;
  554. u32 look_ahead, adj;
  555. s32 error2, mult;
  556. /*
  557. * Use the current error value to determine how much to look ahead.
  558. * The larger the error the slower we adjust for it to avoid problems
  559. * with losing too many ticks, otherwise we would overadjust and
  560. * produce an even larger error. The smaller the adjustment the
  561. * faster we try to adjust for it, as lost ticks can do less harm
  562. * here. This is tuned so that an error of about 1 msec is adjusted
  563. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  564. */
  565. error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
  566. error2 = abs(error2);
  567. for (look_ahead = 0; error2 > 0; look_ahead++)
  568. error2 >>= 2;
  569. /*
  570. * Now calculate the error in (1 << look_ahead) ticks, but first
  571. * remove the single look ahead already included in the error.
  572. */
  573. tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
  574. tick_error -= timekeeper.xtime_interval >> 1;
  575. error = ((error - tick_error) >> look_ahead) + tick_error;
  576. /* Finally calculate the adjustment shift value. */
  577. i = *interval;
  578. mult = 1;
  579. if (error < 0) {
  580. error = -error;
  581. *interval = -*interval;
  582. *offset = -*offset;
  583. mult = -1;
  584. }
  585. for (adj = 0; error > i; adj++)
  586. error >>= 1;
  587. *interval <<= adj;
  588. *offset <<= adj;
  589. return mult << adj;
  590. }
  591. /*
  592. * Adjust the multiplier to reduce the error value,
  593. * this is optimized for the most common adjustments of -1,0,1,
  594. * for other values we can do a bit more work.
  595. */
  596. static void timekeeping_adjust(s64 offset)
  597. {
  598. s64 error, interval = timekeeper.cycle_interval;
  599. int adj;
  600. error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
  601. if (error > interval) {
  602. error >>= 2;
  603. if (likely(error <= interval))
  604. adj = 1;
  605. else
  606. adj = timekeeping_bigadjust(error, &interval, &offset);
  607. } else if (error < -interval) {
  608. error >>= 2;
  609. if (likely(error >= -interval)) {
  610. adj = -1;
  611. interval = -interval;
  612. offset = -offset;
  613. } else
  614. adj = timekeeping_bigadjust(error, &interval, &offset);
  615. } else
  616. return;
  617. timekeeper.mult += adj;
  618. timekeeper.xtime_interval += interval;
  619. timekeeper.xtime_nsec -= offset;
  620. timekeeper.ntp_error -= (interval - offset) <<
  621. timekeeper.ntp_error_shift;
  622. }
  623. /**
  624. * logarithmic_accumulation - shifted accumulation of cycles
  625. *
  626. * This functions accumulates a shifted interval of cycles into
  627. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  628. * loop.
  629. *
  630. * Returns the unconsumed cycles.
  631. */
  632. static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
  633. {
  634. u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
  635. u64 raw_nsecs;
  636. /* If the offset is smaller then a shifted interval, do nothing */
  637. if (offset < timekeeper.cycle_interval<<shift)
  638. return offset;
  639. /* Accumulate one shifted interval */
  640. offset -= timekeeper.cycle_interval << shift;
  641. timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
  642. timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
  643. while (timekeeper.xtime_nsec >= nsecps) {
  644. timekeeper.xtime_nsec -= nsecps;
  645. xtime.tv_sec++;
  646. second_overflow();
  647. }
  648. /* Accumulate raw time */
  649. raw_nsecs = timekeeper.raw_interval << shift;
  650. raw_nsecs += raw_time.tv_nsec;
  651. if (raw_nsecs >= NSEC_PER_SEC) {
  652. u64 raw_secs = raw_nsecs;
  653. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  654. raw_time.tv_sec += raw_secs;
  655. }
  656. raw_time.tv_nsec = raw_nsecs;
  657. /* Accumulate error between NTP and clock interval */
  658. timekeeper.ntp_error += tick_length << shift;
  659. timekeeper.ntp_error -=
  660. (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
  661. (timekeeper.ntp_error_shift + shift);
  662. return offset;
  663. }
  664. /**
  665. * update_wall_time - Uses the current clocksource to increment the wall time
  666. *
  667. * Called from the timer interrupt, must hold a write on xtime_lock.
  668. */
  669. static void update_wall_time(void)
  670. {
  671. struct clocksource *clock;
  672. cycle_t offset;
  673. int shift = 0, maxshift;
  674. /* Make sure we're fully resumed: */
  675. if (unlikely(timekeeping_suspended))
  676. return;
  677. clock = timekeeper.clock;
  678. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  679. offset = timekeeper.cycle_interval;
  680. #else
  681. offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
  682. #endif
  683. timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
  684. /*
  685. * With NO_HZ we may have to accumulate many cycle_intervals
  686. * (think "ticks") worth of time at once. To do this efficiently,
  687. * we calculate the largest doubling multiple of cycle_intervals
  688. * that is smaller then the offset. We then accumulate that
  689. * chunk in one go, and then try to consume the next smaller
  690. * doubled multiple.
  691. */
  692. shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
  693. shift = max(0, shift);
  694. /* Bound shift to one less then what overflows tick_length */
  695. maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
  696. shift = min(shift, maxshift);
  697. while (offset >= timekeeper.cycle_interval) {
  698. offset = logarithmic_accumulation(offset, shift);
  699. if(offset < timekeeper.cycle_interval<<shift)
  700. shift--;
  701. }
  702. /* correct the clock when NTP error is too big */
  703. timekeeping_adjust(offset);
  704. /*
  705. * Since in the loop above, we accumulate any amount of time
  706. * in xtime_nsec over a second into xtime.tv_sec, its possible for
  707. * xtime_nsec to be fairly small after the loop. Further, if we're
  708. * slightly speeding the clocksource up in timekeeping_adjust(),
  709. * its possible the required corrective factor to xtime_nsec could
  710. * cause it to underflow.
  711. *
  712. * Now, we cannot simply roll the accumulated second back, since
  713. * the NTP subsystem has been notified via second_overflow. So
  714. * instead we push xtime_nsec forward by the amount we underflowed,
  715. * and add that amount into the error.
  716. *
  717. * We'll correct this error next time through this function, when
  718. * xtime_nsec is not as small.
  719. */
  720. if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
  721. s64 neg = -(s64)timekeeper.xtime_nsec;
  722. timekeeper.xtime_nsec = 0;
  723. timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
  724. }
  725. /*
  726. * Store full nanoseconds into xtime after rounding it up and
  727. * add the remainder to the error difference.
  728. */
  729. xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
  730. timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
  731. timekeeper.ntp_error += timekeeper.xtime_nsec <<
  732. timekeeper.ntp_error_shift;
  733. /*
  734. * Finally, make sure that after the rounding
  735. * xtime.tv_nsec isn't larger then NSEC_PER_SEC
  736. */
  737. if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
  738. xtime.tv_nsec -= NSEC_PER_SEC;
  739. xtime.tv_sec++;
  740. second_overflow();
  741. }
  742. /* check to see if there is a new clocksource to use */
  743. update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
  744. timekeeper.mult);
  745. }
  746. /**
  747. * getboottime - Return the real time of system boot.
  748. * @ts: pointer to the timespec to be set
  749. *
  750. * Returns the wall-time of boot in a timespec.
  751. *
  752. * This is based on the wall_to_monotonic offset and the total suspend
  753. * time. Calls to settimeofday will affect the value returned (which
  754. * basically means that however wrong your real time clock is at boot time,
  755. * you get the right time here).
  756. */
  757. void getboottime(struct timespec *ts)
  758. {
  759. struct timespec boottime = {
  760. .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
  761. .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
  762. };
  763. set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
  764. }
  765. EXPORT_SYMBOL_GPL(getboottime);
  766. /**
  767. * get_monotonic_boottime - Returns monotonic time since boot
  768. * @ts: pointer to the timespec to be set
  769. *
  770. * Returns the monotonic time since boot in a timespec.
  771. *
  772. * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
  773. * includes the time spent in suspend.
  774. */
  775. void get_monotonic_boottime(struct timespec *ts)
  776. {
  777. struct timespec tomono, sleep;
  778. unsigned int seq;
  779. s64 nsecs;
  780. WARN_ON(timekeeping_suspended);
  781. do {
  782. seq = read_seqbegin(&xtime_lock);
  783. *ts = xtime;
  784. tomono = wall_to_monotonic;
  785. sleep = total_sleep_time;
  786. nsecs = timekeeping_get_ns();
  787. } while (read_seqretry(&xtime_lock, seq));
  788. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
  789. ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
  790. }
  791. EXPORT_SYMBOL_GPL(get_monotonic_boottime);
  792. /**
  793. * ktime_get_boottime - Returns monotonic time since boot in a ktime
  794. *
  795. * Returns the monotonic time since boot in a ktime
  796. *
  797. * This is similar to CLOCK_MONTONIC/ktime_get, but also
  798. * includes the time spent in suspend.
  799. */
  800. ktime_t ktime_get_boottime(void)
  801. {
  802. struct timespec ts;
  803. get_monotonic_boottime(&ts);
  804. return timespec_to_ktime(ts);
  805. }
  806. EXPORT_SYMBOL_GPL(ktime_get_boottime);
  807. /**
  808. * monotonic_to_bootbased - Convert the monotonic time to boot based.
  809. * @ts: pointer to the timespec to be converted
  810. */
  811. void monotonic_to_bootbased(struct timespec *ts)
  812. {
  813. *ts = timespec_add(*ts, total_sleep_time);
  814. }
  815. EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
  816. unsigned long get_seconds(void)
  817. {
  818. return xtime.tv_sec;
  819. }
  820. EXPORT_SYMBOL(get_seconds);
  821. struct timespec __current_kernel_time(void)
  822. {
  823. return xtime;
  824. }
  825. struct timespec current_kernel_time(void)
  826. {
  827. struct timespec now;
  828. unsigned long seq;
  829. do {
  830. seq = read_seqbegin(&xtime_lock);
  831. now = xtime;
  832. } while (read_seqretry(&xtime_lock, seq));
  833. return now;
  834. }
  835. EXPORT_SYMBOL(current_kernel_time);
  836. struct timespec get_monotonic_coarse(void)
  837. {
  838. struct timespec now, mono;
  839. unsigned long seq;
  840. do {
  841. seq = read_seqbegin(&xtime_lock);
  842. now = xtime;
  843. mono = wall_to_monotonic;
  844. } while (read_seqretry(&xtime_lock, seq));
  845. set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
  846. now.tv_nsec + mono.tv_nsec);
  847. return now;
  848. }
  849. /*
  850. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  851. * without sampling the sequence number in xtime_lock.
  852. * jiffies is defined in the linker script...
  853. */
  854. void do_timer(unsigned long ticks)
  855. {
  856. jiffies_64 += ticks;
  857. update_wall_time();
  858. calc_global_load(ticks);
  859. }
  860. /**
  861. * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
  862. * and sleep offsets.
  863. * @xtim: pointer to timespec to be set with xtime
  864. * @wtom: pointer to timespec to be set with wall_to_monotonic
  865. * @sleep: pointer to timespec to be set with time in suspend
  866. */
  867. void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
  868. struct timespec *wtom, struct timespec *sleep)
  869. {
  870. unsigned long seq;
  871. do {
  872. seq = read_seqbegin(&xtime_lock);
  873. *xtim = xtime;
  874. *wtom = wall_to_monotonic;
  875. *sleep = total_sleep_time;
  876. } while (read_seqretry(&xtime_lock, seq));
  877. }
  878. /**
  879. * xtime_update() - advances the timekeeping infrastructure
  880. * @ticks: number of ticks, that have elapsed since the last call.
  881. *
  882. * Must be called with interrupts disabled.
  883. */
  884. void xtime_update(unsigned long ticks)
  885. {
  886. write_seqlock(&xtime_lock);
  887. do_timer(ticks);
  888. write_sequnlock(&xtime_lock);
  889. }