clocksource.c 28 KB

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