clocksource.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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: Marking clocksource '%s' as unstable because the skew is too large:\n",
  196. 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. pr_warn("Override clocksource %s is not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
  527. cs->name);
  528. override_name[0] = 0;
  529. } else
  530. /* Override clocksource can be used. */
  531. best = cs;
  532. break;
  533. }
  534. if (curr_clocksource != best && !timekeeping_notify(best)) {
  535. pr_info("Switched to clocksource %s\n", best->name);
  536. curr_clocksource = best;
  537. }
  538. }
  539. /**
  540. * clocksource_select - Select the best clocksource available
  541. *
  542. * Private function. Must hold clocksource_mutex when called.
  543. *
  544. * Select the clocksource with the best rating, or the clocksource,
  545. * which is selected by userspace override.
  546. */
  547. static void clocksource_select(void)
  548. {
  549. __clocksource_select(false);
  550. }
  551. static void clocksource_select_fallback(void)
  552. {
  553. __clocksource_select(true);
  554. }
  555. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  556. static inline void clocksource_select(void) { }
  557. static inline void clocksource_select_fallback(void) { }
  558. #endif
  559. /*
  560. * clocksource_done_booting - Called near the end of core bootup
  561. *
  562. * Hack to avoid lots of clocksource churn at boot time.
  563. * We use fs_initcall because we want this to start before
  564. * device_initcall but after subsys_initcall.
  565. */
  566. static int __init clocksource_done_booting(void)
  567. {
  568. mutex_lock(&clocksource_mutex);
  569. curr_clocksource = clocksource_default_clock();
  570. finished_booting = 1;
  571. /*
  572. * Run the watchdog first to eliminate unstable clock sources
  573. */
  574. __clocksource_watchdog_kthread();
  575. clocksource_select();
  576. mutex_unlock(&clocksource_mutex);
  577. return 0;
  578. }
  579. fs_initcall(clocksource_done_booting);
  580. /*
  581. * Enqueue the clocksource sorted by rating
  582. */
  583. static void clocksource_enqueue(struct clocksource *cs)
  584. {
  585. struct list_head *entry = &clocksource_list;
  586. struct clocksource *tmp;
  587. list_for_each_entry(tmp, &clocksource_list, list)
  588. /* Keep track of the place, where to insert */
  589. if (tmp->rating >= cs->rating)
  590. entry = &tmp->list;
  591. list_add(&cs->list, entry);
  592. }
  593. /**
  594. * __clocksource_update_freq_scale - Used update clocksource with new freq
  595. * @cs: clocksource to be registered
  596. * @scale: Scale factor multiplied against freq to get clocksource hz
  597. * @freq: clocksource frequency (cycles per second) divided by scale
  598. *
  599. * This should only be called from the clocksource->enable() method.
  600. *
  601. * This *SHOULD NOT* be called directly! Please use the
  602. * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
  603. * functions.
  604. */
  605. void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
  606. {
  607. u64 sec;
  608. /*
  609. * Default clocksources are *special* and self-define their mult/shift.
  610. * But, you're not special, so you should specify a freq value.
  611. */
  612. if (freq) {
  613. /*
  614. * Calc the maximum number of seconds which we can run before
  615. * wrapping around. For clocksources which have a mask > 32-bit
  616. * we need to limit the max sleep time to have a good
  617. * conversion precision. 10 minutes is still a reasonable
  618. * amount. That results in a shift value of 24 for a
  619. * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
  620. * ~ 0.06ppm granularity for NTP.
  621. */
  622. sec = cs->mask;
  623. do_div(sec, freq);
  624. do_div(sec, scale);
  625. if (!sec)
  626. sec = 1;
  627. else if (sec > 600 && cs->mask > UINT_MAX)
  628. sec = 600;
  629. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  630. NSEC_PER_SEC / scale, sec * scale);
  631. }
  632. /*
  633. * Ensure clocksources that have large 'mult' values don't overflow
  634. * when adjusted.
  635. */
  636. cs->maxadj = clocksource_max_adjustment(cs);
  637. while (freq && ((cs->mult + cs->maxadj < cs->mult)
  638. || (cs->mult - cs->maxadj > cs->mult))) {
  639. cs->mult >>= 1;
  640. cs->shift--;
  641. cs->maxadj = clocksource_max_adjustment(cs);
  642. }
  643. /*
  644. * Only warn for *special* clocksources that self-define
  645. * their mult/shift values and don't specify a freq.
  646. */
  647. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  648. "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
  649. cs->name);
  650. clocksource_update_max_deferment(cs);
  651. pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
  652. cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
  653. }
  654. EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
  655. /**
  656. * __clocksource_register_scale - Used to install new clocksources
  657. * @cs: clocksource to be registered
  658. * @scale: Scale factor multiplied against freq to get clocksource hz
  659. * @freq: clocksource frequency (cycles per second) divided by scale
  660. *
  661. * Returns -EBUSY if registration fails, zero otherwise.
  662. *
  663. * This *SHOULD NOT* be called directly! Please use the
  664. * clocksource_register_hz() or clocksource_register_khz helper functions.
  665. */
  666. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  667. {
  668. /* Initialize mult/shift and max_idle_ns */
  669. __clocksource_update_freq_scale(cs, scale, freq);
  670. /* Add clocksource to the clocksource list */
  671. mutex_lock(&clocksource_mutex);
  672. clocksource_enqueue(cs);
  673. clocksource_enqueue_watchdog(cs);
  674. clocksource_select();
  675. clocksource_select_watchdog(false);
  676. mutex_unlock(&clocksource_mutex);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  680. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  681. {
  682. list_del(&cs->list);
  683. cs->rating = rating;
  684. clocksource_enqueue(cs);
  685. }
  686. /**
  687. * clocksource_change_rating - Change the rating of a registered clocksource
  688. * @cs: clocksource to be changed
  689. * @rating: new rating
  690. */
  691. void clocksource_change_rating(struct clocksource *cs, int rating)
  692. {
  693. mutex_lock(&clocksource_mutex);
  694. __clocksource_change_rating(cs, rating);
  695. clocksource_select();
  696. clocksource_select_watchdog(false);
  697. mutex_unlock(&clocksource_mutex);
  698. }
  699. EXPORT_SYMBOL(clocksource_change_rating);
  700. /*
  701. * Unbind clocksource @cs. Called with clocksource_mutex held
  702. */
  703. static int clocksource_unbind(struct clocksource *cs)
  704. {
  705. if (clocksource_is_watchdog(cs)) {
  706. /* Select and try to install a replacement watchdog. */
  707. clocksource_select_watchdog(true);
  708. if (clocksource_is_watchdog(cs))
  709. return -EBUSY;
  710. }
  711. if (cs == curr_clocksource) {
  712. /* Select and try to install a replacement clock source */
  713. clocksource_select_fallback();
  714. if (curr_clocksource == cs)
  715. return -EBUSY;
  716. }
  717. clocksource_dequeue_watchdog(cs);
  718. list_del_init(&cs->list);
  719. return 0;
  720. }
  721. /**
  722. * clocksource_unregister - remove a registered clocksource
  723. * @cs: clocksource to be unregistered
  724. */
  725. int clocksource_unregister(struct clocksource *cs)
  726. {
  727. int ret = 0;
  728. mutex_lock(&clocksource_mutex);
  729. if (!list_empty(&cs->list))
  730. ret = clocksource_unbind(cs);
  731. mutex_unlock(&clocksource_mutex);
  732. return ret;
  733. }
  734. EXPORT_SYMBOL(clocksource_unregister);
  735. #ifdef CONFIG_SYSFS
  736. /**
  737. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  738. * @dev: unused
  739. * @attr: unused
  740. * @buf: char buffer to be filled with clocksource list
  741. *
  742. * Provides sysfs interface for listing current clocksource.
  743. */
  744. static ssize_t
  745. sysfs_show_current_clocksources(struct device *dev,
  746. struct device_attribute *attr, char *buf)
  747. {
  748. ssize_t count = 0;
  749. mutex_lock(&clocksource_mutex);
  750. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  751. mutex_unlock(&clocksource_mutex);
  752. return count;
  753. }
  754. ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
  755. {
  756. size_t ret = cnt;
  757. /* strings from sysfs write are not 0 terminated! */
  758. if (!cnt || cnt >= CS_NAME_LEN)
  759. return -EINVAL;
  760. /* strip of \n: */
  761. if (buf[cnt-1] == '\n')
  762. cnt--;
  763. if (cnt > 0)
  764. memcpy(dst, buf, cnt);
  765. dst[cnt] = 0;
  766. return ret;
  767. }
  768. /**
  769. * sysfs_override_clocksource - interface for manually overriding clocksource
  770. * @dev: unused
  771. * @attr: unused
  772. * @buf: name of override clocksource
  773. * @count: length of buffer
  774. *
  775. * Takes input from sysfs interface for manually overriding the default
  776. * clocksource selection.
  777. */
  778. static ssize_t sysfs_override_clocksource(struct device *dev,
  779. struct device_attribute *attr,
  780. const char *buf, size_t count)
  781. {
  782. ssize_t ret;
  783. mutex_lock(&clocksource_mutex);
  784. ret = sysfs_get_uname(buf, override_name, count);
  785. if (ret >= 0)
  786. clocksource_select();
  787. mutex_unlock(&clocksource_mutex);
  788. return ret;
  789. }
  790. /**
  791. * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
  792. * @dev: unused
  793. * @attr: unused
  794. * @buf: unused
  795. * @count: length of buffer
  796. *
  797. * Takes input from sysfs interface for manually unbinding a clocksource.
  798. */
  799. static ssize_t sysfs_unbind_clocksource(struct device *dev,
  800. struct device_attribute *attr,
  801. const char *buf, size_t count)
  802. {
  803. struct clocksource *cs;
  804. char name[CS_NAME_LEN];
  805. ssize_t ret;
  806. ret = sysfs_get_uname(buf, name, count);
  807. if (ret < 0)
  808. return ret;
  809. ret = -ENODEV;
  810. mutex_lock(&clocksource_mutex);
  811. list_for_each_entry(cs, &clocksource_list, list) {
  812. if (strcmp(cs->name, name))
  813. continue;
  814. ret = clocksource_unbind(cs);
  815. break;
  816. }
  817. mutex_unlock(&clocksource_mutex);
  818. return ret ? ret : count;
  819. }
  820. /**
  821. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  822. * @dev: unused
  823. * @attr: unused
  824. * @buf: char buffer to be filled with clocksource list
  825. *
  826. * Provides sysfs interface for listing registered clocksources
  827. */
  828. static ssize_t
  829. sysfs_show_available_clocksources(struct device *dev,
  830. struct device_attribute *attr,
  831. char *buf)
  832. {
  833. struct clocksource *src;
  834. ssize_t count = 0;
  835. mutex_lock(&clocksource_mutex);
  836. list_for_each_entry(src, &clocksource_list, list) {
  837. /*
  838. * Don't show non-HRES clocksource if the tick code is
  839. * in one shot mode (highres=on or nohz=on)
  840. */
  841. if (!tick_oneshot_mode_active() ||
  842. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  843. count += snprintf(buf + count,
  844. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  845. "%s ", src->name);
  846. }
  847. mutex_unlock(&clocksource_mutex);
  848. count += snprintf(buf + count,
  849. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  850. return count;
  851. }
  852. /*
  853. * Sysfs setup bits:
  854. */
  855. static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  856. sysfs_override_clocksource);
  857. static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
  858. static DEVICE_ATTR(available_clocksource, 0444,
  859. sysfs_show_available_clocksources, NULL);
  860. static struct bus_type clocksource_subsys = {
  861. .name = "clocksource",
  862. .dev_name = "clocksource",
  863. };
  864. static struct device device_clocksource = {
  865. .id = 0,
  866. .bus = &clocksource_subsys,
  867. };
  868. static int __init init_clocksource_sysfs(void)
  869. {
  870. int error = subsys_system_register(&clocksource_subsys, NULL);
  871. if (!error)
  872. error = device_register(&device_clocksource);
  873. if (!error)
  874. error = device_create_file(
  875. &device_clocksource,
  876. &dev_attr_current_clocksource);
  877. if (!error)
  878. error = device_create_file(&device_clocksource,
  879. &dev_attr_unbind_clocksource);
  880. if (!error)
  881. error = device_create_file(
  882. &device_clocksource,
  883. &dev_attr_available_clocksource);
  884. return error;
  885. }
  886. device_initcall(init_clocksource_sysfs);
  887. #endif /* CONFIG_SYSFS */
  888. /**
  889. * boot_override_clocksource - boot clock override
  890. * @str: override name
  891. *
  892. * Takes a clocksource= boot argument and uses it
  893. * as the clocksource override name.
  894. */
  895. static int __init boot_override_clocksource(char* str)
  896. {
  897. mutex_lock(&clocksource_mutex);
  898. if (str)
  899. strlcpy(override_name, str, sizeof(override_name));
  900. mutex_unlock(&clocksource_mutex);
  901. return 1;
  902. }
  903. __setup("clocksource=", boot_override_clocksource);
  904. /**
  905. * boot_override_clock - Compatibility layer for deprecated boot option
  906. * @str: override name
  907. *
  908. * DEPRECATED! Takes a clock= boot argument and uses it
  909. * as the clocksource override name
  910. */
  911. static int __init boot_override_clock(char* str)
  912. {
  913. if (!strcmp(str, "pmtmr")) {
  914. pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
  915. return boot_override_clocksource("acpi_pm");
  916. }
  917. pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
  918. return boot_override_clocksource(str);
  919. }
  920. __setup("clock=", boot_override_clock);