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