PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/time/ntp.c

https://bitbucket.org/androidarmv6/android_kernel_huawei_msm7x25
C | 554 lines | 334 code | 107 blank | 113 comment | 56 complexity | f9cf3e1ed1550223b21af9c5614bbcaf MD5 | raw file
  1. /*
  2. * NTP state machine interfaces and logic.
  3. *
  4. * This code was mainly moved from kernel/timer.c and kernel/time.c
  5. * Please see those files for relevant copyright info and historical
  6. * changelogs.
  7. */
  8. #include <linux/capability.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/math64.h>
  14. #include <linux/timex.h>
  15. #include <linux/time.h>
  16. #include <linux/mm.h>
  17. /*
  18. * NTP timekeeping variables:
  19. */
  20. /* USER_HZ period (usecs): */
  21. unsigned long tick_usec = TICK_USEC;
  22. /* ACTHZ period (nsecs): */
  23. unsigned long tick_nsec;
  24. u64 tick_length;
  25. static u64 tick_length_base;
  26. static struct hrtimer leap_timer;
  27. #define MAX_TICKADJ 500LL /* usecs */
  28. #define MAX_TICKADJ_SCALED \
  29. (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
  30. /*
  31. * phase-lock loop variables
  32. */
  33. /*
  34. * clock synchronization status
  35. *
  36. * (TIME_ERROR prevents overwriting the CMOS clock)
  37. */
  38. static int time_state = TIME_OK;
  39. /* clock status bits: */
  40. int time_status = STA_UNSYNC;
  41. /* TAI offset (secs): */
  42. static long time_tai;
  43. /* time adjustment (nsecs): */
  44. static s64 time_offset;
  45. /* pll time constant: */
  46. static long time_constant = 2;
  47. /* maximum error (usecs): */
  48. long time_maxerror = NTP_PHASE_LIMIT;
  49. /* estimated error (usecs): */
  50. long time_esterror = NTP_PHASE_LIMIT;
  51. /* frequency offset (scaled nsecs/secs): */
  52. static s64 time_freq;
  53. /* time at last adjustment (secs): */
  54. static long time_reftime;
  55. long time_adjust;
  56. /* constant (boot-param configurable) NTP tick adjustment (upscaled) */
  57. static s64 ntp_tick_adj;
  58. /*
  59. * NTP methods:
  60. */
  61. /*
  62. * Update (tick_length, tick_length_base, tick_nsec), based
  63. * on (tick_usec, ntp_tick_adj, time_freq):
  64. */
  65. static void ntp_update_frequency(void)
  66. {
  67. u64 second_length;
  68. u64 new_base;
  69. second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
  70. << NTP_SCALE_SHIFT;
  71. second_length += ntp_tick_adj;
  72. second_length += time_freq;
  73. tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
  74. new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
  75. /*
  76. * Don't wait for the next second_overflow, apply
  77. * the change to the tick length immediately:
  78. */
  79. tick_length += new_base - tick_length_base;
  80. tick_length_base = new_base;
  81. }
  82. static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
  83. {
  84. time_status &= ~STA_MODE;
  85. if (secs < MINSEC)
  86. return 0;
  87. if (!(time_status & STA_FLL) && (secs <= MAXSEC))
  88. return 0;
  89. time_status |= STA_MODE;
  90. return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
  91. }
  92. static void ntp_update_offset(long offset)
  93. {
  94. s64 freq_adj;
  95. s64 offset64;
  96. long secs;
  97. if (!(time_status & STA_PLL))
  98. return;
  99. if (!(time_status & STA_NANO))
  100. offset *= NSEC_PER_USEC;
  101. /*
  102. * Scale the phase adjustment and
  103. * clamp to the operating range.
  104. */
  105. offset = min(offset, MAXPHASE);
  106. offset = max(offset, -MAXPHASE);
  107. /*
  108. * Select how the frequency is to be controlled
  109. * and in which mode (PLL or FLL).
  110. */
  111. secs = xtime.tv_sec - time_reftime;
  112. if (unlikely(time_status & STA_FREQHOLD))
  113. secs = 0;
  114. time_reftime = xtime.tv_sec;
  115. offset64 = offset;
  116. freq_adj = (offset64 * secs) <<
  117. (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
  118. freq_adj += ntp_update_offset_fll(offset64, secs);
  119. freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
  120. time_freq = max(freq_adj, -MAXFREQ_SCALED);
  121. time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
  122. }
  123. /**
  124. * ntp_clear - Clears the NTP state variables
  125. *
  126. * Must be called while holding a write on the xtime_lock
  127. */
  128. void ntp_clear(void)
  129. {
  130. time_adjust = 0; /* stop active adjtime() */
  131. time_status |= STA_UNSYNC;
  132. time_maxerror = NTP_PHASE_LIMIT;
  133. time_esterror = NTP_PHASE_LIMIT;
  134. ntp_update_frequency();
  135. tick_length = tick_length_base;
  136. time_offset = 0;
  137. }
  138. /*
  139. * Leap second processing. If in leap-insert state at the end of the
  140. * day, the system clock is set back one second; if in leap-delete
  141. * state, the system clock is set ahead one second.
  142. */
  143. static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
  144. {
  145. enum hrtimer_restart res = HRTIMER_NORESTART;
  146. write_seqlock(&xtime_lock);
  147. switch (time_state) {
  148. case TIME_OK:
  149. break;
  150. case TIME_INS:
  151. timekeeping_leap_insert(-1);
  152. time_state = TIME_OOP;
  153. printk(KERN_NOTICE
  154. "Clock: inserting leap second 23:59:60 UTC\n");
  155. hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
  156. res = HRTIMER_RESTART;
  157. break;
  158. case TIME_DEL:
  159. timekeeping_leap_insert(1);
  160. time_tai--;
  161. time_state = TIME_WAIT;
  162. printk(KERN_NOTICE
  163. "Clock: deleting leap second 23:59:59 UTC\n");
  164. break;
  165. case TIME_OOP:
  166. time_tai++;
  167. time_state = TIME_WAIT;
  168. /* fall through */
  169. case TIME_WAIT:
  170. if (!(time_status & (STA_INS | STA_DEL)))
  171. time_state = TIME_OK;
  172. break;
  173. }
  174. write_sequnlock(&xtime_lock);
  175. return res;
  176. }
  177. /*
  178. * this routine handles the overflow of the microsecond field
  179. *
  180. * The tricky bits of code to handle the accurate clock support
  181. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  182. * They were originally developed for SUN and DEC kernels.
  183. * All the kudos should go to Dave for this stuff.
  184. */
  185. void second_overflow(void)
  186. {
  187. s64 delta;
  188. /* Bump the maxerror field */
  189. time_maxerror += MAXFREQ / NSEC_PER_USEC;
  190. if (time_maxerror > NTP_PHASE_LIMIT) {
  191. time_maxerror = NTP_PHASE_LIMIT;
  192. time_status |= STA_UNSYNC;
  193. }
  194. /*
  195. * Compute the phase adjustment for the next second. The offset is
  196. * reduced by a fixed factor times the time constant.
  197. */
  198. tick_length = tick_length_base;
  199. delta = shift_right(time_offset, SHIFT_PLL + time_constant);
  200. time_offset -= delta;
  201. tick_length += delta;
  202. if (!time_adjust)
  203. return;
  204. if (time_adjust > MAX_TICKADJ) {
  205. time_adjust -= MAX_TICKADJ;
  206. tick_length += MAX_TICKADJ_SCALED;
  207. return;
  208. }
  209. if (time_adjust < -MAX_TICKADJ) {
  210. time_adjust += MAX_TICKADJ;
  211. tick_length -= MAX_TICKADJ_SCALED;
  212. return;
  213. }
  214. tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
  215. << NTP_SCALE_SHIFT;
  216. time_adjust = 0;
  217. }
  218. #ifdef CONFIG_GENERIC_CMOS_UPDATE
  219. /* Disable the cmos update - used by virtualization and embedded */
  220. int no_sync_cmos_clock __read_mostly;
  221. static void sync_cmos_clock(struct work_struct *work);
  222. static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
  223. static void sync_cmos_clock(struct work_struct *work)
  224. {
  225. struct timespec now, next;
  226. int fail = 1;
  227. /*
  228. * If we have an externally synchronized Linux clock, then update
  229. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  230. * called as close as possible to 500 ms before the new second starts.
  231. * This code is run on a timer. If the clock is set, that timer
  232. * may not expire at the correct time. Thus, we adjust...
  233. */
  234. if (!ntp_synced()) {
  235. /*
  236. * Not synced, exit, do not restart a timer (if one is
  237. * running, let it run out).
  238. */
  239. return;
  240. }
  241. getnstimeofday(&now);
  242. if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
  243. fail = update_persistent_clock(now);
  244. next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
  245. if (next.tv_nsec <= 0)
  246. next.tv_nsec += NSEC_PER_SEC;
  247. if (!fail)
  248. next.tv_sec = 659;
  249. else
  250. next.tv_sec = 0;
  251. if (next.tv_nsec >= NSEC_PER_SEC) {
  252. next.tv_sec++;
  253. next.tv_nsec -= NSEC_PER_SEC;
  254. }
  255. schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
  256. }
  257. static void notify_cmos_timer(void)
  258. {
  259. if (!no_sync_cmos_clock)
  260. schedule_delayed_work(&sync_cmos_work, 0);
  261. }
  262. #else
  263. static inline void notify_cmos_timer(void) { }
  264. #endif
  265. /*
  266. * Start the leap seconds timer:
  267. */
  268. static inline void ntp_start_leap_timer(struct timespec *ts)
  269. {
  270. long now = ts->tv_sec;
  271. if (time_status & STA_INS) {
  272. time_state = TIME_INS;
  273. now += 86400 - now % 86400;
  274. hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
  275. return;
  276. }
  277. if (time_status & STA_DEL) {
  278. time_state = TIME_DEL;
  279. now += 86400 - (now + 1) % 86400;
  280. hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
  281. }
  282. }
  283. /*
  284. * Propagate a new txc->status value into the NTP state:
  285. */
  286. static inline void process_adj_status(struct timex *txc, struct timespec *ts)
  287. {
  288. if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
  289. time_state = TIME_OK;
  290. time_status = STA_UNSYNC;
  291. }
  292. /*
  293. * If we turn on PLL adjustments then reset the
  294. * reference time to current time.
  295. */
  296. if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
  297. time_reftime = xtime.tv_sec;
  298. /* only set allowed bits */
  299. time_status &= STA_RONLY;
  300. time_status |= txc->status & ~STA_RONLY;
  301. switch (time_state) {
  302. case TIME_OK:
  303. ntp_start_leap_timer(ts);
  304. break;
  305. case TIME_INS:
  306. case TIME_DEL:
  307. time_state = TIME_OK;
  308. ntp_start_leap_timer(ts);
  309. case TIME_WAIT:
  310. if (!(time_status & (STA_INS | STA_DEL)))
  311. time_state = TIME_OK;
  312. break;
  313. case TIME_OOP:
  314. hrtimer_restart(&leap_timer);
  315. break;
  316. }
  317. }
  318. /*
  319. * Called with the xtime lock held, so we can access and modify
  320. * all the global NTP state:
  321. */
  322. static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
  323. {
  324. if (txc->modes & ADJ_STATUS)
  325. process_adj_status(txc, ts);
  326. if (txc->modes & ADJ_NANO)
  327. time_status |= STA_NANO;
  328. if (txc->modes & ADJ_MICRO)
  329. time_status &= ~STA_NANO;
  330. if (txc->modes & ADJ_FREQUENCY) {
  331. time_freq = txc->freq * PPM_SCALE;
  332. time_freq = min(time_freq, MAXFREQ_SCALED);
  333. time_freq = max(time_freq, -MAXFREQ_SCALED);
  334. }
  335. if (txc->modes & ADJ_MAXERROR)
  336. time_maxerror = txc->maxerror;
  337. if (txc->modes & ADJ_ESTERROR)
  338. time_esterror = txc->esterror;
  339. if (txc->modes & ADJ_TIMECONST) {
  340. time_constant = txc->constant;
  341. if (!(time_status & STA_NANO))
  342. time_constant += 4;
  343. time_constant = min(time_constant, (long)MAXTC);
  344. time_constant = max(time_constant, 0l);
  345. }
  346. if (txc->modes & ADJ_TAI && txc->constant > 0)
  347. time_tai = txc->constant;
  348. if (txc->modes & ADJ_OFFSET)
  349. ntp_update_offset(txc->offset);
  350. if (txc->modes & ADJ_TICK)
  351. tick_usec = txc->tick;
  352. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  353. ntp_update_frequency();
  354. }
  355. /*
  356. * adjtimex mainly allows reading (and writing, if superuser) of
  357. * kernel time-keeping variables. used by xntpd.
  358. */
  359. int do_adjtimex(struct timex *txc)
  360. {
  361. struct timespec ts;
  362. int result;
  363. /* Validate the data before disabling interrupts */
  364. if (txc->modes & ADJ_ADJTIME) {
  365. /* singleshot must not be used with any other mode bits */
  366. if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
  367. return -EINVAL;
  368. if (!(txc->modes & ADJ_OFFSET_READONLY) &&
  369. !capable(CAP_SYS_TIME))
  370. return -EPERM;
  371. } else {
  372. /* In order to modify anything, you gotta be super-user! */
  373. if (txc->modes && !capable(CAP_SYS_TIME))
  374. return -EPERM;
  375. /*
  376. * if the quartz is off by more than 10% then
  377. * something is VERY wrong!
  378. */
  379. if (txc->modes & ADJ_TICK &&
  380. (txc->tick < 900000/USER_HZ ||
  381. txc->tick > 1100000/USER_HZ))
  382. return -EINVAL;
  383. if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
  384. hrtimer_cancel(&leap_timer);
  385. }
  386. getnstimeofday(&ts);
  387. write_seqlock_irq(&xtime_lock);
  388. if (txc->modes & ADJ_ADJTIME) {
  389. long save_adjust = time_adjust;
  390. if (!(txc->modes & ADJ_OFFSET_READONLY)) {
  391. /* adjtime() is independent from ntp_adjtime() */
  392. time_adjust = txc->offset;
  393. ntp_update_frequency();
  394. }
  395. txc->offset = save_adjust;
  396. } else {
  397. /* If there are input parameters, then process them: */
  398. if (txc->modes)
  399. process_adjtimex_modes(txc, &ts);
  400. txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
  401. NTP_SCALE_SHIFT);
  402. if (!(time_status & STA_NANO))
  403. txc->offset /= NSEC_PER_USEC;
  404. }
  405. result = time_state; /* mostly `TIME_OK' */
  406. if (time_status & (STA_UNSYNC|STA_CLOCKERR))
  407. result = TIME_ERROR;
  408. txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
  409. PPM_SCALE_INV, NTP_SCALE_SHIFT);
  410. txc->maxerror = time_maxerror;
  411. txc->esterror = time_esterror;
  412. txc->status = time_status;
  413. txc->constant = time_constant;
  414. txc->precision = 1;
  415. txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
  416. txc->tick = tick_usec;
  417. txc->tai = time_tai;
  418. /* PPS is not implemented, so these are zero */
  419. txc->ppsfreq = 0;
  420. txc->jitter = 0;
  421. txc->shift = 0;
  422. txc->stabil = 0;
  423. txc->jitcnt = 0;
  424. txc->calcnt = 0;
  425. txc->errcnt = 0;
  426. txc->stbcnt = 0;
  427. write_sequnlock_irq(&xtime_lock);
  428. txc->time.tv_sec = ts.tv_sec;
  429. txc->time.tv_usec = ts.tv_nsec;
  430. if (!(time_status & STA_NANO))
  431. txc->time.tv_usec /= NSEC_PER_USEC;
  432. notify_cmos_timer();
  433. return result;
  434. }
  435. static int __init ntp_tick_adj_setup(char *str)
  436. {
  437. ntp_tick_adj = simple_strtol(str, NULL, 0);
  438. ntp_tick_adj <<= NTP_SCALE_SHIFT;
  439. return 1;
  440. }
  441. __setup("ntp_tick_adj=", ntp_tick_adj_setup);
  442. void __init ntp_init(void)
  443. {
  444. ntp_clear();
  445. hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  446. leap_timer.function = ntp_leap_second;
  447. }