clocksource.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * linux/kernel/time/clocksource.c
  3. *
  4. * This file contains the functions which manage clocksource drivers.
  5. *
  6. * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * TODO WishList:
  23. * o Allow clocksource drivers to be unregistered
  24. */
  25. #include <linux/device.h>
  26. #include <linux/clocksource.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
  30. #include <linux/tick.h>
  31. #include <linux/kthread.h>
  32. void timecounter_init(struct timecounter *tc,
  33. const struct cyclecounter *cc,
  34. u64 start_tstamp)
  35. {
  36. tc->cc = cc;
  37. tc->cycle_last = cc->read(cc);
  38. tc->nsec = start_tstamp;
  39. }
  40. EXPORT_SYMBOL_GPL(timecounter_init);
  41. /**
  42. * timecounter_read_delta - get nanoseconds since last call of this function
  43. * @tc: Pointer to time counter
  44. *
  45. * When the underlying cycle counter runs over, this will be handled
  46. * correctly as long as it does not run over more than once between
  47. * calls.
  48. *
  49. * The first call to this function for a new time counter initializes
  50. * the time tracking and returns an undefined result.
  51. */
  52. static u64 timecounter_read_delta(struct timecounter *tc)
  53. {
  54. cycle_t cycle_now, cycle_delta;
  55. u64 ns_offset;
  56. /* read cycle counter: */
  57. cycle_now = tc->cc->read(tc->cc);
  58. /* calculate the delta since the last timecounter_read_delta(): */
  59. cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
  60. /* convert to nanoseconds: */
  61. ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
  62. /* update time stamp of timecounter_read_delta() call: */
  63. tc->cycle_last = cycle_now;
  64. return ns_offset;
  65. }
  66. u64 timecounter_read(struct timecounter *tc)
  67. {
  68. u64 nsec;
  69. /* increment time by nanoseconds since last call */
  70. nsec = timecounter_read_delta(tc);
  71. nsec += tc->nsec;
  72. tc->nsec = nsec;
  73. return nsec;
  74. }
  75. EXPORT_SYMBOL_GPL(timecounter_read);
  76. u64 timecounter_cyc2time(struct timecounter *tc,
  77. cycle_t cycle_tstamp)
  78. {
  79. u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
  80. u64 nsec;
  81. /*
  82. * Instead of always treating cycle_tstamp as more recent
  83. * than tc->cycle_last, detect when it is too far in the
  84. * future and treat it as old time stamp instead.
  85. */
  86. if (cycle_delta > tc->cc->mask / 2) {
  87. cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
  88. nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
  89. } else {
  90. nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
  91. }
  92. return nsec;
  93. }
  94. EXPORT_SYMBOL_GPL(timecounter_cyc2time);
  95. /**
  96. * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
  97. * @mult: pointer to mult variable
  98. * @shift: pointer to shift variable
  99. * @from: frequency to convert from
  100. * @to: frequency to convert to
  101. * @maxsec: guaranteed runtime conversion range in seconds
  102. *
  103. * The function evaluates the shift/mult pair for the scaled math
  104. * operations of clocksources and clockevents.
  105. *
  106. * @to and @from are frequency values in HZ. For clock sources @to is
  107. * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
  108. * event @to is the counter frequency and @from is NSEC_PER_SEC.
  109. *
  110. * The @maxsec conversion range argument controls the time frame in
  111. * seconds which must be covered by the runtime conversion with the
  112. * calculated mult and shift factors. This guarantees that no 64bit
  113. * overflow happens when the input value of the conversion is
  114. * multiplied with the calculated mult factor. Larger ranges may
  115. * reduce the conversion accuracy by chosing smaller mult and shift
  116. * factors.
  117. */
  118. void
  119. clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
  120. {
  121. u64 tmp;
  122. u32 sft, sftacc= 32;
  123. /*
  124. * Calculate the shift factor which is limiting the conversion
  125. * range:
  126. */
  127. tmp = ((u64)maxsec * from) >> 32;
  128. while (tmp) {
  129. tmp >>=1;
  130. sftacc--;
  131. }
  132. /*
  133. * Find the conversion shift/mult pair which has the best
  134. * accuracy and fits the maxsec conversion range:
  135. */
  136. for (sft = 32; sft > 0; sft--) {
  137. tmp = (u64) to << sft;
  138. tmp += from / 2;
  139. do_div(tmp, from);
  140. if ((tmp >> sftacc) == 0)
  141. break;
  142. }
  143. *mult = tmp;
  144. *shift = sft;
  145. }
  146. /*[Clocksource internal variables]---------
  147. * curr_clocksource:
  148. * currently selected clocksource.
  149. * clocksource_list:
  150. * linked list with the registered clocksources
  151. * clocksource_mutex:
  152. * protects manipulations to curr_clocksource and the clocksource_list
  153. * override_name:
  154. * Name of the user-specified clocksource.
  155. */
  156. static struct clocksource *curr_clocksource;
  157. static LIST_HEAD(clocksource_list);
  158. static DEFINE_MUTEX(clocksource_mutex);
  159. static char override_name[32];
  160. static int finished_booting;
  161. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  162. static void clocksource_watchdog_work(struct work_struct *work);
  163. static LIST_HEAD(watchdog_list);
  164. static struct clocksource *watchdog;
  165. static struct timer_list watchdog_timer;
  166. static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
  167. static DEFINE_SPINLOCK(watchdog_lock);
  168. static int watchdog_running;
  169. static atomic_t watchdog_reset_pending;
  170. static int clocksource_watchdog_kthread(void *data);
  171. static void __clocksource_change_rating(struct clocksource *cs, int rating);
  172. /*
  173. * Interval: 0.5sec Threshold: 0.0625s
  174. */
  175. #define WATCHDOG_INTERVAL (HZ >> 1)
  176. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
  177. static void clocksource_watchdog_work(struct work_struct *work)
  178. {
  179. /*
  180. * If kthread_run fails the next watchdog scan over the
  181. * watchdog_list will find the unstable clock again.
  182. */
  183. kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
  184. }
  185. static void __clocksource_unstable(struct clocksource *cs)
  186. {
  187. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  188. cs->flags |= CLOCK_SOURCE_UNSTABLE;
  189. if (finished_booting)
  190. schedule_work(&watchdog_work);
  191. }
  192. static void clocksource_unstable(struct clocksource *cs, int64_t delta)
  193. {
  194. printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
  195. cs->name, delta);
  196. __clocksource_unstable(cs);
  197. }
  198. /**
  199. * clocksource_mark_unstable - mark clocksource unstable via watchdog
  200. * @cs: clocksource to be marked unstable
  201. *
  202. * This function is called instead of clocksource_change_rating from
  203. * cpu hotplug code to avoid a deadlock between the clocksource mutex
  204. * and the cpu hotplug mutex. It defers the update of the clocksource
  205. * to the watchdog thread.
  206. */
  207. void clocksource_mark_unstable(struct clocksource *cs)
  208. {
  209. unsigned long flags;
  210. spin_lock_irqsave(&watchdog_lock, flags);
  211. if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
  212. if (list_empty(&cs->wd_list))
  213. list_add(&cs->wd_list, &watchdog_list);
  214. __clocksource_unstable(cs);
  215. }
  216. spin_unlock_irqrestore(&watchdog_lock, flags);
  217. }
  218. static void clocksource_watchdog(unsigned long data)
  219. {
  220. struct clocksource *cs;
  221. cycle_t csnow, wdnow;
  222. int64_t wd_nsec, cs_nsec;
  223. int next_cpu, reset_pending;
  224. spin_lock(&watchdog_lock);
  225. if (!watchdog_running)
  226. goto out;
  227. reset_pending = atomic_read(&watchdog_reset_pending);
  228. list_for_each_entry(cs, &watchdog_list, wd_list) {
  229. /* Clocksource already marked unstable? */
  230. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  231. if (finished_booting)
  232. schedule_work(&watchdog_work);
  233. continue;
  234. }
  235. local_irq_disable();
  236. csnow = cs->read(cs);
  237. wdnow = watchdog->read(watchdog);
  238. local_irq_enable();
  239. /* Clocksource initialized ? */
  240. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
  241. atomic_read(&watchdog_reset_pending)) {
  242. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  243. cs->wd_last = wdnow;
  244. cs->cs_last = csnow;
  245. continue;
  246. }
  247. wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
  248. watchdog->mult, watchdog->shift);
  249. cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
  250. cs->mask, cs->mult, cs->shift);
  251. cs->cs_last = csnow;
  252. cs->wd_last = wdnow;
  253. if (atomic_read(&watchdog_reset_pending))
  254. continue;
  255. /* Check the deviation from the watchdog clocksource. */
  256. if ((abs64(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
  257. clocksource_unstable(cs, cs_nsec - wd_nsec);
  258. continue;
  259. }
  260. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  261. (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  262. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  263. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  264. /*
  265. * We just marked the clocksource as highres-capable,
  266. * notify the rest of the system as well so that we
  267. * transition into high-res mode:
  268. */
  269. tick_clock_notify();
  270. }
  271. }
  272. /*
  273. * We only clear the watchdog_reset_pending, when we did a
  274. * full cycle through all clocksources.
  275. */
  276. if (reset_pending)
  277. atomic_dec(&watchdog_reset_pending);
  278. /*
  279. * Cycle through CPUs to check if the CPUs stay synchronized
  280. * to each other.
  281. */
  282. next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  283. if (next_cpu >= nr_cpu_ids)
  284. next_cpu = cpumask_first(cpu_online_mask);
  285. /*
  286. * Arm timer if not already pending: could race with concurrent
  287. * pair clocksource_stop_watchdog() clocksource_start_watchdog().
  288. */
  289. if (!timer_pending(&watchdog_timer)) {
  290. watchdog_timer.expires += WATCHDOG_INTERVAL;
  291. add_timer_on(&watchdog_timer, next_cpu);
  292. }
  293. out:
  294. spin_unlock(&watchdog_lock);
  295. }
  296. static inline void clocksource_start_watchdog(void)
  297. {
  298. if (watchdog_running || !watchdog || list_empty(&watchdog_list))
  299. return;
  300. init_timer(&watchdog_timer);
  301. watchdog_timer.function = clocksource_watchdog;
  302. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  303. add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
  304. watchdog_running = 1;
  305. }
  306. static inline void clocksource_stop_watchdog(void)
  307. {
  308. if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
  309. return;
  310. del_timer(&watchdog_timer);
  311. watchdog_running = 0;
  312. }
  313. static inline void clocksource_reset_watchdog(void)
  314. {
  315. struct clocksource *cs;
  316. list_for_each_entry(cs, &watchdog_list, wd_list)
  317. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  318. }
  319. static void clocksource_resume_watchdog(void)
  320. {
  321. atomic_inc(&watchdog_reset_pending);
  322. }
  323. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  324. {
  325. unsigned long flags;
  326. spin_lock_irqsave(&watchdog_lock, flags);
  327. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  328. /* cs is a clocksource to be watched. */
  329. list_add(&cs->wd_list, &watchdog_list);
  330. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  331. } else {
  332. /* cs is a watchdog. */
  333. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  334. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  335. /* Pick the best watchdog. */
  336. if (!watchdog || cs->rating > watchdog->rating) {
  337. watchdog = cs;
  338. /* Reset watchdog cycles */
  339. clocksource_reset_watchdog();
  340. }
  341. }
  342. /* Check if the watchdog timer needs to be started. */
  343. clocksource_start_watchdog();
  344. spin_unlock_irqrestore(&watchdog_lock, flags);
  345. }
  346. static void clocksource_dequeue_watchdog(struct clocksource *cs)
  347. {
  348. struct clocksource *tmp;
  349. unsigned long flags;
  350. spin_lock_irqsave(&watchdog_lock, flags);
  351. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  352. /* cs is a watched clocksource. */
  353. list_del_init(&cs->wd_list);
  354. } else if (cs == watchdog) {
  355. /* Reset watchdog cycles */
  356. clocksource_reset_watchdog();
  357. /* Current watchdog is removed. Find an alternative. */
  358. watchdog = NULL;
  359. list_for_each_entry(tmp, &clocksource_list, list) {
  360. if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
  361. continue;
  362. if (!watchdog || tmp->rating > watchdog->rating)
  363. watchdog = tmp;
  364. }
  365. }
  366. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  367. /* Check if the watchdog timer needs to be stopped. */
  368. clocksource_stop_watchdog();
  369. spin_unlock_irqrestore(&watchdog_lock, flags);
  370. }
  371. static int clocksource_watchdog_kthread(void *data)
  372. {
  373. struct clocksource *cs, *tmp;
  374. unsigned long flags;
  375. LIST_HEAD(unstable);
  376. mutex_lock(&clocksource_mutex);
  377. spin_lock_irqsave(&watchdog_lock, flags);
  378. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
  379. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  380. list_del_init(&cs->wd_list);
  381. list_add(&cs->wd_list, &unstable);
  382. }
  383. /* Check if the watchdog timer needs to be stopped. */
  384. clocksource_stop_watchdog();
  385. spin_unlock_irqrestore(&watchdog_lock, flags);
  386. /* Needs to be done outside of watchdog lock */
  387. list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
  388. list_del_init(&cs->wd_list);
  389. __clocksource_change_rating(cs, 0);
  390. }
  391. mutex_unlock(&clocksource_mutex);
  392. return 0;
  393. }
  394. #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
  395. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  396. {
  397. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  398. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  399. }
  400. static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
  401. static inline void clocksource_resume_watchdog(void) { }
  402. static inline int clocksource_watchdog_kthread(void *data) { return 0; }
  403. #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
  404. /**
  405. * clocksource_suspend - suspend the clocksource(s)
  406. */
  407. void clocksource_suspend(void)
  408. {
  409. struct clocksource *cs;
  410. list_for_each_entry_reverse(cs, &clocksource_list, list)
  411. if (cs->suspend)
  412. cs->suspend(cs);
  413. }
  414. /**
  415. * clocksource_resume - resume the clocksource(s)
  416. */
  417. void clocksource_resume(void)
  418. {
  419. struct clocksource *cs;
  420. list_for_each_entry(cs, &clocksource_list, list)
  421. if (cs->resume)
  422. cs->resume(cs);
  423. clocksource_resume_watchdog();
  424. }
  425. /**
  426. * clocksource_touch_watchdog - Update watchdog
  427. *
  428. * Update the watchdog after exception contexts such as kgdb so as not
  429. * to incorrectly trip the watchdog. This might fail when the kernel
  430. * was stopped in code which holds watchdog_lock.
  431. */
  432. void clocksource_touch_watchdog(void)
  433. {
  434. clocksource_resume_watchdog();
  435. }
  436. /**
  437. * clocksource_max_adjustment- Returns max adjustment amount
  438. * @cs: Pointer to clocksource
  439. *
  440. */
  441. static u32 clocksource_max_adjustment(struct clocksource *cs)
  442. {
  443. u64 ret;
  444. /*
  445. * We won't try to correct for more than 11% adjustments (110,000 ppm),
  446. */
  447. ret = (u64)cs->mult * 11;
  448. do_div(ret,100);
  449. return (u32)ret;
  450. }
  451. /**
  452. * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
  453. * @mult: cycle to nanosecond multiplier
  454. * @shift: cycle to nanosecond divisor (power of two)
  455. * @maxadj: maximum adjustment value to mult (~11%)
  456. * @mask: bitmask for two's complement subtraction of non 64 bit counters
  457. */
  458. u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
  459. {
  460. u64 max_nsecs, max_cycles;
  461. /*
  462. * Calculate the maximum number of cycles that we can pass to the
  463. * cyc2ns function without overflowing a 64-bit signed result. The
  464. * maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
  465. * which is equivalent to the below.
  466. * max_cycles < (2^63)/(mult + maxadj)
  467. * max_cycles < 2^(log2((2^63)/(mult + maxadj)))
  468. * max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
  469. * max_cycles < 2^(63 - log2(mult + maxadj))
  470. * max_cycles < 1 << (63 - log2(mult + maxadj))
  471. * Please note that we add 1 to the result of the log2 to account for
  472. * any rounding errors, ensure the above inequality is satisfied and
  473. * no overflow will occur.
  474. */
  475. max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1));
  476. /*
  477. * The actual maximum number of cycles we can defer the clocksource is
  478. * determined by the minimum of max_cycles and mask.
  479. * Note: Here we subtract the maxadj to make sure we don't sleep for
  480. * too long if there's a large negative adjustment.
  481. */
  482. max_cycles = min(max_cycles, mask);
  483. max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
  484. return max_nsecs;
  485. }
  486. /**
  487. * clocksource_max_deferment - Returns max time the clocksource can be deferred
  488. * @cs: Pointer to clocksource
  489. *
  490. */
  491. static u64 clocksource_max_deferment(struct clocksource *cs)
  492. {
  493. u64 max_nsecs;
  494. max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
  495. cs->mask);
  496. /*
  497. * To ensure that the clocksource does not wrap whilst we are idle,
  498. * limit the time the clocksource can be deferred by 12.5%. Please
  499. * note a margin of 12.5% is used because this can be computed with
  500. * a shift, versus say 10% which would require division.
  501. */
  502. return max_nsecs - (max_nsecs >> 3);
  503. }
  504. #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
  505. /**
  506. * clocksource_select - Select the best clocksource available
  507. *
  508. * Private function. Must hold clocksource_mutex when called.
  509. *
  510. * Select the clocksource with the best rating, or the clocksource,
  511. * which is selected by userspace override.
  512. */
  513. static void clocksource_select(void)
  514. {
  515. struct clocksource *best, *cs;
  516. if (!finished_booting || list_empty(&clocksource_list))
  517. return;
  518. /* First clocksource on the list has the best rating. */
  519. best = list_first_entry(&clocksource_list, struct clocksource, list);
  520. /* Check for the override clocksource. */
  521. list_for_each_entry(cs, &clocksource_list, list) {
  522. if (strcmp(cs->name, override_name) != 0)
  523. continue;
  524. /*
  525. * Check to make sure we don't switch to a non-highres
  526. * capable clocksource if the tick code is in oneshot
  527. * mode (highres or nohz)
  528. */
  529. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  530. tick_oneshot_mode_active()) {
  531. /* Override clocksource cannot be used. */
  532. printk(KERN_WARNING "Override clocksource %s is not "
  533. "HRT compatible. Cannot switch while in "
  534. "HRT/NOHZ mode\n", cs->name);
  535. override_name[0] = 0;
  536. } else
  537. /* Override clocksource can be used. */
  538. best = cs;
  539. break;
  540. }
  541. if (curr_clocksource != best) {
  542. printk(KERN_INFO "Switching to clocksource %s\n", best->name);
  543. curr_clocksource = best;
  544. timekeeping_notify(curr_clocksource);
  545. }
  546. }
  547. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  548. static inline void clocksource_select(void) { }
  549. #endif
  550. /*
  551. * clocksource_done_booting - Called near the end of core bootup
  552. *
  553. * Hack to avoid lots of clocksource churn at boot time.
  554. * We use fs_initcall because we want this to start before
  555. * device_initcall but after subsys_initcall.
  556. */
  557. static int __init clocksource_done_booting(void)
  558. {
  559. mutex_lock(&clocksource_mutex);
  560. curr_clocksource = clocksource_default_clock();
  561. mutex_unlock(&clocksource_mutex);
  562. finished_booting = 1;
  563. /*
  564. * Run the watchdog first to eliminate unstable clock sources
  565. */
  566. clocksource_watchdog_kthread(NULL);
  567. mutex_lock(&clocksource_mutex);
  568. clocksource_select();
  569. mutex_unlock(&clocksource_mutex);
  570. return 0;
  571. }
  572. fs_initcall(clocksource_done_booting);
  573. /*
  574. * Enqueue the clocksource sorted by rating
  575. */
  576. static void clocksource_enqueue(struct clocksource *cs)
  577. {
  578. struct list_head *entry = &clocksource_list;
  579. struct clocksource *tmp;
  580. list_for_each_entry(tmp, &clocksource_list, list)
  581. /* Keep track of the place, where to insert */
  582. if (tmp->rating >= cs->rating)
  583. entry = &tmp->list;
  584. list_add(&cs->list, entry);
  585. }
  586. /**
  587. * __clocksource_updatefreq_scale - Used update clocksource with new freq
  588. * @cs: clocksource to be registered
  589. * @scale: Scale factor multiplied against freq to get clocksource hz
  590. * @freq: clocksource frequency (cycles per second) divided by scale
  591. *
  592. * This should only be called from the clocksource->enable() method.
  593. *
  594. * This *SHOULD NOT* be called directly! Please use the
  595. * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
  596. */
  597. void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
  598. {
  599. u64 sec;
  600. /*
  601. * Calc the maximum number of seconds which we can run before
  602. * wrapping around. For clocksources which have a mask > 32bit
  603. * we need to limit the max sleep time to have a good
  604. * conversion precision. 10 minutes is still a reasonable
  605. * amount. That results in a shift value of 24 for a
  606. * clocksource with mask >= 40bit and f >= 4GHz. That maps to
  607. * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
  608. * margin as we do in clocksource_max_deferment()
  609. */
  610. sec = (cs->mask - (cs->mask >> 3));
  611. do_div(sec, freq);
  612. do_div(sec, scale);
  613. if (!sec)
  614. sec = 1;
  615. else if (sec > 600 && cs->mask > UINT_MAX)
  616. sec = 600;
  617. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  618. NSEC_PER_SEC / scale, sec * scale);
  619. /*
  620. * for clocksources that have large mults, to avoid overflow.
  621. * Since mult may be adjusted by ntp, add an safety extra margin
  622. *
  623. */
  624. cs->maxadj = clocksource_max_adjustment(cs);
  625. while ((cs->mult + cs->maxadj < cs->mult)
  626. || (cs->mult - cs->maxadj > cs->mult)) {
  627. cs->mult >>= 1;
  628. cs->shift--;
  629. cs->maxadj = clocksource_max_adjustment(cs);
  630. }
  631. cs->max_idle_ns = clocksource_max_deferment(cs);
  632. }
  633. EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
  634. /**
  635. * __clocksource_register_scale - Used to install new clocksources
  636. * @cs: clocksource to be registered
  637. * @scale: Scale factor multiplied against freq to get clocksource hz
  638. * @freq: clocksource frequency (cycles per second) divided by scale
  639. *
  640. * Returns -EBUSY if registration fails, zero otherwise.
  641. *
  642. * This *SHOULD NOT* be called directly! Please use the
  643. * clocksource_register_hz() or clocksource_register_khz helper functions.
  644. */
  645. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  646. {
  647. /* Initialize mult/shift and max_idle_ns */
  648. __clocksource_updatefreq_scale(cs, scale, freq);
  649. /* Add clocksource to the clcoksource list */
  650. mutex_lock(&clocksource_mutex);
  651. clocksource_enqueue(cs);
  652. clocksource_enqueue_watchdog(cs);
  653. clocksource_select();
  654. mutex_unlock(&clocksource_mutex);
  655. return 0;
  656. }
  657. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  658. /**
  659. * clocksource_register - Used to install new clocksources
  660. * @cs: clocksource to be registered
  661. *
  662. * Returns -EBUSY if registration fails, zero otherwise.
  663. */
  664. int clocksource_register(struct clocksource *cs)
  665. {
  666. /* calculate max adjustment for given mult/shift */
  667. cs->maxadj = clocksource_max_adjustment(cs);
  668. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  669. "Clocksource %s might overflow on 11%% adjustment\n",
  670. cs->name);
  671. /* calculate max idle time permitted for this clocksource */
  672. cs->max_idle_ns = clocksource_max_deferment(cs);
  673. mutex_lock(&clocksource_mutex);
  674. clocksource_enqueue(cs);
  675. clocksource_enqueue_watchdog(cs);
  676. clocksource_select();
  677. mutex_unlock(&clocksource_mutex);
  678. return 0;
  679. }
  680. EXPORT_SYMBOL(clocksource_register);
  681. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  682. {
  683. list_del(&cs->list);
  684. cs->rating = rating;
  685. clocksource_enqueue(cs);
  686. clocksource_select();
  687. }
  688. /**
  689. * clocksource_change_rating - Change the rating of a registered clocksource
  690. * @cs: clocksource to be changed
  691. * @rating: new rating
  692. */
  693. void clocksource_change_rating(struct clocksource *cs, int rating)
  694. {
  695. mutex_lock(&clocksource_mutex);
  696. __clocksource_change_rating(cs, rating);
  697. mutex_unlock(&clocksource_mutex);
  698. }
  699. EXPORT_SYMBOL(clocksource_change_rating);
  700. /**
  701. * clocksource_unregister - remove a registered clocksource
  702. * @cs: clocksource to be unregistered
  703. */
  704. void clocksource_unregister(struct clocksource *cs)
  705. {
  706. mutex_lock(&clocksource_mutex);
  707. clocksource_dequeue_watchdog(cs);
  708. list_del(&cs->list);
  709. clocksource_select();
  710. mutex_unlock(&clocksource_mutex);
  711. }
  712. EXPORT_SYMBOL(clocksource_unregister);
  713. #ifdef CONFIG_SYSFS
  714. /**
  715. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  716. * @dev: unused
  717. * @attr: unused
  718. * @buf: char buffer to be filled with clocksource list
  719. *
  720. * Provides sysfs interface for listing current clocksource.
  721. */
  722. static ssize_t
  723. sysfs_show_current_clocksources(struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. ssize_t count = 0;
  727. mutex_lock(&clocksource_mutex);
  728. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  729. mutex_unlock(&clocksource_mutex);
  730. return count;
  731. }
  732. /**
  733. * sysfs_override_clocksource - interface for manually overriding clocksource
  734. * @dev: unused
  735. * @attr: unused
  736. * @buf: name of override clocksource
  737. * @count: length of buffer
  738. *
  739. * Takes input from sysfs interface for manually overriding the default
  740. * clocksource selection.
  741. */
  742. static ssize_t sysfs_override_clocksource(struct device *dev,
  743. struct device_attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. size_t ret = count;
  747. /* strings from sysfs write are not 0 terminated! */
  748. if (count >= sizeof(override_name))
  749. return -EINVAL;
  750. /* strip of \n: */
  751. if (buf[count-1] == '\n')
  752. count--;
  753. mutex_lock(&clocksource_mutex);
  754. if (count > 0)
  755. memcpy(override_name, buf, count);
  756. override_name[count] = 0;
  757. clocksource_select();
  758. mutex_unlock(&clocksource_mutex);
  759. return ret;
  760. }
  761. /**
  762. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  763. * @dev: unused
  764. * @attr: unused
  765. * @buf: char buffer to be filled with clocksource list
  766. *
  767. * Provides sysfs interface for listing registered clocksources
  768. */
  769. static ssize_t
  770. sysfs_show_available_clocksources(struct device *dev,
  771. struct device_attribute *attr,
  772. char *buf)
  773. {
  774. struct clocksource *src;
  775. ssize_t count = 0;
  776. mutex_lock(&clocksource_mutex);
  777. list_for_each_entry(src, &clocksource_list, list) {
  778. /*
  779. * Don't show non-HRES clocksource if the tick code is
  780. * in one shot mode (highres=on or nohz=on)
  781. */
  782. if (!tick_oneshot_mode_active() ||
  783. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  784. count += snprintf(buf + count,
  785. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  786. "%s ", src->name);
  787. }
  788. mutex_unlock(&clocksource_mutex);
  789. count += snprintf(buf + count,
  790. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  791. return count;
  792. }
  793. /*
  794. * Sysfs setup bits:
  795. */
  796. static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  797. sysfs_override_clocksource);
  798. static DEVICE_ATTR(available_clocksource, 0444,
  799. sysfs_show_available_clocksources, NULL);
  800. static struct bus_type clocksource_subsys = {
  801. .name = "clocksource",
  802. .dev_name = "clocksource",
  803. };
  804. static struct device device_clocksource = {
  805. .id = 0,
  806. .bus = &clocksource_subsys,
  807. };
  808. static int __init init_clocksource_sysfs(void)
  809. {
  810. int error = subsys_system_register(&clocksource_subsys, NULL);
  811. if (!error)
  812. error = device_register(&device_clocksource);
  813. if (!error)
  814. error = device_create_file(
  815. &device_clocksource,
  816. &dev_attr_current_clocksource);
  817. if (!error)
  818. error = device_create_file(
  819. &device_clocksource,
  820. &dev_attr_available_clocksource);
  821. return error;
  822. }
  823. device_initcall(init_clocksource_sysfs);
  824. #endif /* CONFIG_SYSFS */
  825. /**
  826. * boot_override_clocksource - boot clock override
  827. * @str: override name
  828. *
  829. * Takes a clocksource= boot argument and uses it
  830. * as the clocksource override name.
  831. */
  832. static int __init boot_override_clocksource(char* str)
  833. {
  834. mutex_lock(&clocksource_mutex);
  835. if (str)
  836. strlcpy(override_name, str, sizeof(override_name));
  837. mutex_unlock(&clocksource_mutex);
  838. return 1;
  839. }
  840. __setup("clocksource=", boot_override_clocksource);
  841. /**
  842. * boot_override_clock - Compatibility layer for deprecated boot option
  843. * @str: override name
  844. *
  845. * DEPRECATED! Takes a clock= boot argument and uses it
  846. * as the clocksource override name
  847. */
  848. static int __init boot_override_clock(char* str)
  849. {
  850. if (!strcmp(str, "pmtmr")) {
  851. printk("Warning: clock=pmtmr is deprecated. "
  852. "Use clocksource=acpi_pm.\n");
  853. return boot_override_clocksource("acpi_pm");
  854. }
  855. printk("Warning! clock= boot option is deprecated. "
  856. "Use clocksource=xyz\n");
  857. return boot_override_clocksource(str);
  858. }
  859. __setup("clock=", boot_override_clock);