clocksource.c 29 KB

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