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