clocksource.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 void inline clocksource_watchdog_lock(unsigned long *flags)
  113. {
  114. spin_lock_irqsave(&watchdog_lock, *flags);
  115. }
  116. static void inline clocksource_watchdog_unlock(unsigned long *flags)
  117. {
  118. spin_unlock_irqrestore(&watchdog_lock, *flags);
  119. }
  120. /*
  121. * Interval: 0.5sec Threshold: 0.0625s
  122. */
  123. #define WATCHDOG_INTERVAL (HZ >> 1)
  124. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
  125. static void __clocksource_unstable(struct clocksource *cs)
  126. {
  127. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  128. cs->flags |= CLOCK_SOURCE_UNSTABLE;
  129. /*
  130. * If the clocksource is registered clocksource_watchdog_work() will
  131. * re-rate and re-select.
  132. */
  133. if (list_empty(&cs->list)) {
  134. cs->rating = 0;
  135. return;
  136. }
  137. if (cs->mark_unstable)
  138. cs->mark_unstable(cs);
  139. /* kick clocksource_watchdog_work() */
  140. if (finished_booting)
  141. schedule_work(&watchdog_work);
  142. }
  143. /**
  144. * clocksource_mark_unstable - mark clocksource unstable via watchdog
  145. * @cs: clocksource to be marked unstable
  146. *
  147. * This function is called by the x86 TSC code to mark clocksources as unstable;
  148. * it defers demotion and re-selection to a work.
  149. */
  150. void clocksource_mark_unstable(struct clocksource *cs)
  151. {
  152. unsigned long flags;
  153. spin_lock_irqsave(&watchdog_lock, flags);
  154. if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
  155. if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
  156. list_add(&cs->wd_list, &watchdog_list);
  157. __clocksource_unstable(cs);
  158. }
  159. spin_unlock_irqrestore(&watchdog_lock, flags);
  160. }
  161. static void clocksource_watchdog(struct timer_list *unused)
  162. {
  163. struct clocksource *cs;
  164. u64 csnow, wdnow, cslast, wdlast, delta;
  165. int64_t wd_nsec, cs_nsec;
  166. int next_cpu, reset_pending;
  167. spin_lock(&watchdog_lock);
  168. if (!watchdog_running)
  169. goto out;
  170. reset_pending = atomic_read(&watchdog_reset_pending);
  171. list_for_each_entry(cs, &watchdog_list, wd_list) {
  172. /* Clocksource already marked unstable? */
  173. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  174. if (finished_booting)
  175. schedule_work(&watchdog_work);
  176. continue;
  177. }
  178. local_irq_disable();
  179. csnow = cs->read(cs);
  180. wdnow = watchdog->read(watchdog);
  181. local_irq_enable();
  182. /* Clocksource initialized ? */
  183. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
  184. atomic_read(&watchdog_reset_pending)) {
  185. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  186. cs->wd_last = wdnow;
  187. cs->cs_last = csnow;
  188. continue;
  189. }
  190. delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
  191. wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
  192. watchdog->shift);
  193. delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
  194. cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
  195. wdlast = cs->wd_last; /* save these in case we print them */
  196. cslast = cs->cs_last;
  197. cs->cs_last = csnow;
  198. cs->wd_last = wdnow;
  199. if (atomic_read(&watchdog_reset_pending))
  200. continue;
  201. /* Check the deviation from the watchdog clocksource. */
  202. if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
  203. pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
  204. smp_processor_id(), cs->name);
  205. pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
  206. watchdog->name, wdnow, wdlast, watchdog->mask);
  207. pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
  208. cs->name, csnow, cslast, cs->mask);
  209. __clocksource_unstable(cs);
  210. continue;
  211. }
  212. if (cs == curr_clocksource && cs->tick_stable)
  213. cs->tick_stable(cs);
  214. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  215. (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  216. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  217. /* Mark it valid for high-res. */
  218. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  219. /*
  220. * clocksource_done_booting() will sort it if
  221. * finished_booting is not set yet.
  222. */
  223. if (!finished_booting)
  224. continue;
  225. /*
  226. * If this is not the current clocksource let
  227. * the watchdog thread reselect it. Due to the
  228. * change to high res this clocksource might
  229. * be preferred now. If it is the current
  230. * clocksource let the tick code know about
  231. * that change.
  232. */
  233. if (cs != curr_clocksource) {
  234. cs->flags |= CLOCK_SOURCE_RESELECT;
  235. schedule_work(&watchdog_work);
  236. } else {
  237. tick_clock_notify();
  238. }
  239. }
  240. }
  241. /*
  242. * We only clear the watchdog_reset_pending, when we did a
  243. * full cycle through all clocksources.
  244. */
  245. if (reset_pending)
  246. atomic_dec(&watchdog_reset_pending);
  247. /*
  248. * Cycle through CPUs to check if the CPUs stay synchronized
  249. * to each other.
  250. */
  251. next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  252. if (next_cpu >= nr_cpu_ids)
  253. next_cpu = cpumask_first(cpu_online_mask);
  254. watchdog_timer.expires += WATCHDOG_INTERVAL;
  255. add_timer_on(&watchdog_timer, next_cpu);
  256. out:
  257. spin_unlock(&watchdog_lock);
  258. }
  259. static inline void clocksource_start_watchdog(void)
  260. {
  261. if (watchdog_running || !watchdog || list_empty(&watchdog_list))
  262. return;
  263. timer_setup(&watchdog_timer, clocksource_watchdog, 0);
  264. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  265. add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
  266. watchdog_running = 1;
  267. }
  268. static inline void clocksource_stop_watchdog(void)
  269. {
  270. if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
  271. return;
  272. del_timer(&watchdog_timer);
  273. watchdog_running = 0;
  274. }
  275. static inline void clocksource_reset_watchdog(void)
  276. {
  277. struct clocksource *cs;
  278. list_for_each_entry(cs, &watchdog_list, wd_list)
  279. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  280. }
  281. static void clocksource_resume_watchdog(void)
  282. {
  283. atomic_inc(&watchdog_reset_pending);
  284. }
  285. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  286. {
  287. INIT_LIST_HEAD(&cs->wd_list);
  288. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  289. /* cs is a clocksource to be watched. */
  290. list_add(&cs->wd_list, &watchdog_list);
  291. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  292. } else {
  293. /* cs is a watchdog. */
  294. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  295. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  296. }
  297. }
  298. static void clocksource_select_watchdog(bool fallback)
  299. {
  300. struct clocksource *cs, *old_wd;
  301. unsigned long flags;
  302. spin_lock_irqsave(&watchdog_lock, flags);
  303. /* save current watchdog */
  304. old_wd = watchdog;
  305. if (fallback)
  306. watchdog = NULL;
  307. list_for_each_entry(cs, &clocksource_list, list) {
  308. /* cs is a clocksource to be watched. */
  309. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
  310. continue;
  311. /* Skip current if we were requested for a fallback. */
  312. if (fallback && cs == old_wd)
  313. continue;
  314. /* Pick the best watchdog. */
  315. if (!watchdog || cs->rating > watchdog->rating)
  316. watchdog = cs;
  317. }
  318. /* If we failed to find a fallback restore the old one. */
  319. if (!watchdog)
  320. watchdog = old_wd;
  321. /* If we changed the watchdog we need to reset cycles. */
  322. if (watchdog != old_wd)
  323. clocksource_reset_watchdog();
  324. /* Check if the watchdog timer needs to be started. */
  325. clocksource_start_watchdog();
  326. spin_unlock_irqrestore(&watchdog_lock, flags);
  327. }
  328. static void clocksource_dequeue_watchdog(struct clocksource *cs)
  329. {
  330. if (cs != watchdog) {
  331. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  332. /* cs is a watched clocksource. */
  333. list_del_init(&cs->wd_list);
  334. /* Check if the watchdog timer needs to be stopped. */
  335. clocksource_stop_watchdog();
  336. }
  337. }
  338. }
  339. static void __clocksource_change_rating(struct clocksource *cs, int rating);
  340. static int __clocksource_watchdog_work(void)
  341. {
  342. struct clocksource *cs, *tmp;
  343. unsigned long flags;
  344. int select = 0;
  345. spin_lock_irqsave(&watchdog_lock, flags);
  346. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
  347. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  348. list_del_init(&cs->wd_list);
  349. __clocksource_change_rating(cs, 0);
  350. select = 1;
  351. }
  352. if (cs->flags & CLOCK_SOURCE_RESELECT) {
  353. cs->flags &= ~CLOCK_SOURCE_RESELECT;
  354. select = 1;
  355. }
  356. }
  357. /* Check if the watchdog timer needs to be stopped. */
  358. clocksource_stop_watchdog();
  359. spin_unlock_irqrestore(&watchdog_lock, flags);
  360. return select;
  361. }
  362. static void clocksource_watchdog_work(struct work_struct *work)
  363. {
  364. mutex_lock(&clocksource_mutex);
  365. if (__clocksource_watchdog_work())
  366. clocksource_select();
  367. mutex_unlock(&clocksource_mutex);
  368. }
  369. static bool clocksource_is_watchdog(struct clocksource *cs)
  370. {
  371. return cs == watchdog;
  372. }
  373. #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
  374. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  375. {
  376. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  377. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  378. }
  379. static void clocksource_select_watchdog(bool fallback) { }
  380. static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
  381. static inline void clocksource_resume_watchdog(void) { }
  382. static inline int __clocksource_watchdog_work(void) { return 0; }
  383. static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
  384. void clocksource_mark_unstable(struct clocksource *cs) { }
  385. static inline void clocksource_watchdog_lock(unsigned long *flags) { }
  386. static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
  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. if (!strlen(override_name))
  514. goto found;
  515. /* Check for the override clocksource. */
  516. list_for_each_entry(cs, &clocksource_list, list) {
  517. if (skipcur && cs == curr_clocksource)
  518. continue;
  519. if (strcmp(cs->name, override_name) != 0)
  520. continue;
  521. /*
  522. * Check to make sure we don't switch to a non-highres
  523. * capable clocksource if the tick code is in oneshot
  524. * mode (highres or nohz)
  525. */
  526. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
  527. /* Override clocksource cannot be used. */
  528. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  529. pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
  530. cs->name);
  531. override_name[0] = 0;
  532. } else {
  533. /*
  534. * The override cannot be currently verified.
  535. * Deferring to let the watchdog check.
  536. */
  537. pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
  538. cs->name);
  539. }
  540. } else
  541. /* Override clocksource can be used. */
  542. best = cs;
  543. break;
  544. }
  545. found:
  546. if (curr_clocksource != best && !timekeeping_notify(best)) {
  547. pr_info("Switched to clocksource %s\n", best->name);
  548. curr_clocksource = best;
  549. }
  550. }
  551. /**
  552. * clocksource_select - Select the best clocksource available
  553. *
  554. * Private function. Must hold clocksource_mutex when called.
  555. *
  556. * Select the clocksource with the best rating, or the clocksource,
  557. * which is selected by userspace override.
  558. */
  559. static void clocksource_select(void)
  560. {
  561. __clocksource_select(false);
  562. }
  563. static void clocksource_select_fallback(void)
  564. {
  565. __clocksource_select(true);
  566. }
  567. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  568. static inline void clocksource_select(void) { }
  569. static inline void clocksource_select_fallback(void) { }
  570. #endif
  571. /*
  572. * clocksource_done_booting - Called near the end of core bootup
  573. *
  574. * Hack to avoid lots of clocksource churn at boot time.
  575. * We use fs_initcall because we want this to start before
  576. * device_initcall but after subsys_initcall.
  577. */
  578. static int __init clocksource_done_booting(void)
  579. {
  580. mutex_lock(&clocksource_mutex);
  581. curr_clocksource = clocksource_default_clock();
  582. finished_booting = 1;
  583. /*
  584. * Run the watchdog first to eliminate unstable clock sources
  585. */
  586. __clocksource_watchdog_work();
  587. clocksource_select();
  588. mutex_unlock(&clocksource_mutex);
  589. return 0;
  590. }
  591. fs_initcall(clocksource_done_booting);
  592. /*
  593. * Enqueue the clocksource sorted by rating
  594. */
  595. static void clocksource_enqueue(struct clocksource *cs)
  596. {
  597. struct list_head *entry = &clocksource_list;
  598. struct clocksource *tmp;
  599. list_for_each_entry(tmp, &clocksource_list, list) {
  600. /* Keep track of the place, where to insert */
  601. if (tmp->rating < cs->rating)
  602. break;
  603. entry = &tmp->list;
  604. }
  605. list_add(&cs->list, entry);
  606. }
  607. /**
  608. * __clocksource_update_freq_scale - Used update clocksource with new freq
  609. * @cs: clocksource to be registered
  610. * @scale: Scale factor multiplied against freq to get clocksource hz
  611. * @freq: clocksource frequency (cycles per second) divided by scale
  612. *
  613. * This should only be called from the clocksource->enable() method.
  614. *
  615. * This *SHOULD NOT* be called directly! Please use the
  616. * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
  617. * functions.
  618. */
  619. void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
  620. {
  621. u64 sec;
  622. /*
  623. * Default clocksources are *special* and self-define their mult/shift.
  624. * But, you're not special, so you should specify a freq value.
  625. */
  626. if (freq) {
  627. /*
  628. * Calc the maximum number of seconds which we can run before
  629. * wrapping around. For clocksources which have a mask > 32-bit
  630. * we need to limit the max sleep time to have a good
  631. * conversion precision. 10 minutes is still a reasonable
  632. * amount. That results in a shift value of 24 for a
  633. * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
  634. * ~ 0.06ppm granularity for NTP.
  635. */
  636. sec = cs->mask;
  637. do_div(sec, freq);
  638. do_div(sec, scale);
  639. if (!sec)
  640. sec = 1;
  641. else if (sec > 600 && cs->mask > UINT_MAX)
  642. sec = 600;
  643. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  644. NSEC_PER_SEC / scale, sec * scale);
  645. }
  646. /*
  647. * Ensure clocksources that have large 'mult' values don't overflow
  648. * when adjusted.
  649. */
  650. cs->maxadj = clocksource_max_adjustment(cs);
  651. while (freq && ((cs->mult + cs->maxadj < cs->mult)
  652. || (cs->mult - cs->maxadj > cs->mult))) {
  653. cs->mult >>= 1;
  654. cs->shift--;
  655. cs->maxadj = clocksource_max_adjustment(cs);
  656. }
  657. /*
  658. * Only warn for *special* clocksources that self-define
  659. * their mult/shift values and don't specify a freq.
  660. */
  661. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  662. "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
  663. cs->name);
  664. clocksource_update_max_deferment(cs);
  665. pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
  666. cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
  667. }
  668. EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
  669. /**
  670. * __clocksource_register_scale - Used to install new clocksources
  671. * @cs: clocksource to be registered
  672. * @scale: Scale factor multiplied against freq to get clocksource hz
  673. * @freq: clocksource frequency (cycles per second) divided by scale
  674. *
  675. * Returns -EBUSY if registration fails, zero otherwise.
  676. *
  677. * This *SHOULD NOT* be called directly! Please use the
  678. * clocksource_register_hz() or clocksource_register_khz helper functions.
  679. */
  680. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  681. {
  682. unsigned long flags;
  683. /* Initialize mult/shift and max_idle_ns */
  684. __clocksource_update_freq_scale(cs, scale, freq);
  685. /* Add clocksource to the clocksource list */
  686. mutex_lock(&clocksource_mutex);
  687. clocksource_watchdog_lock(&flags);
  688. clocksource_enqueue(cs);
  689. clocksource_enqueue_watchdog(cs);
  690. clocksource_watchdog_unlock(&flags);
  691. clocksource_select();
  692. clocksource_select_watchdog(false);
  693. mutex_unlock(&clocksource_mutex);
  694. return 0;
  695. }
  696. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  697. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  698. {
  699. list_del(&cs->list);
  700. cs->rating = rating;
  701. clocksource_enqueue(cs);
  702. }
  703. /**
  704. * clocksource_change_rating - Change the rating of a registered clocksource
  705. * @cs: clocksource to be changed
  706. * @rating: new rating
  707. */
  708. void clocksource_change_rating(struct clocksource *cs, int rating)
  709. {
  710. unsigned long flags;
  711. mutex_lock(&clocksource_mutex);
  712. clocksource_watchdog_lock(&flags);
  713. __clocksource_change_rating(cs, rating);
  714. clocksource_watchdog_unlock(&flags);
  715. clocksource_select();
  716. clocksource_select_watchdog(false);
  717. mutex_unlock(&clocksource_mutex);
  718. }
  719. EXPORT_SYMBOL(clocksource_change_rating);
  720. /*
  721. * Unbind clocksource @cs. Called with clocksource_mutex held
  722. */
  723. static int clocksource_unbind(struct clocksource *cs)
  724. {
  725. unsigned long flags;
  726. if (clocksource_is_watchdog(cs)) {
  727. /* Select and try to install a replacement watchdog. */
  728. clocksource_select_watchdog(true);
  729. if (clocksource_is_watchdog(cs))
  730. return -EBUSY;
  731. }
  732. if (cs == curr_clocksource) {
  733. /* Select and try to install a replacement clock source */
  734. clocksource_select_fallback();
  735. if (curr_clocksource == cs)
  736. return -EBUSY;
  737. }
  738. clocksource_watchdog_lock(&flags);
  739. clocksource_dequeue_watchdog(cs);
  740. list_del_init(&cs->list);
  741. clocksource_watchdog_unlock(&flags);
  742. return 0;
  743. }
  744. /**
  745. * clocksource_unregister - remove a registered clocksource
  746. * @cs: clocksource to be unregistered
  747. */
  748. int clocksource_unregister(struct clocksource *cs)
  749. {
  750. int ret = 0;
  751. mutex_lock(&clocksource_mutex);
  752. if (!list_empty(&cs->list))
  753. ret = clocksource_unbind(cs);
  754. mutex_unlock(&clocksource_mutex);
  755. return ret;
  756. }
  757. EXPORT_SYMBOL(clocksource_unregister);
  758. #ifdef CONFIG_SYSFS
  759. /**
  760. * current_clocksource_show - sysfs interface for current clocksource
  761. * @dev: unused
  762. * @attr: unused
  763. * @buf: char buffer to be filled with clocksource list
  764. *
  765. * Provides sysfs interface for listing current clocksource.
  766. */
  767. static ssize_t current_clocksource_show(struct device *dev,
  768. struct device_attribute *attr,
  769. char *buf)
  770. {
  771. ssize_t count = 0;
  772. mutex_lock(&clocksource_mutex);
  773. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  774. mutex_unlock(&clocksource_mutex);
  775. return count;
  776. }
  777. ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
  778. {
  779. size_t ret = cnt;
  780. /* strings from sysfs write are not 0 terminated! */
  781. if (!cnt || cnt >= CS_NAME_LEN)
  782. return -EINVAL;
  783. /* strip of \n: */
  784. if (buf[cnt-1] == '\n')
  785. cnt--;
  786. if (cnt > 0)
  787. memcpy(dst, buf, cnt);
  788. dst[cnt] = 0;
  789. return ret;
  790. }
  791. /**
  792. * current_clocksource_store - interface for manually overriding clocksource
  793. * @dev: unused
  794. * @attr: unused
  795. * @buf: name of override clocksource
  796. * @count: length of buffer
  797. *
  798. * Takes input from sysfs interface for manually overriding the default
  799. * clocksource selection.
  800. */
  801. static ssize_t current_clocksource_store(struct device *dev,
  802. struct device_attribute *attr,
  803. const char *buf, size_t count)
  804. {
  805. ssize_t ret;
  806. mutex_lock(&clocksource_mutex);
  807. ret = sysfs_get_uname(buf, override_name, count);
  808. if (ret >= 0)
  809. clocksource_select();
  810. mutex_unlock(&clocksource_mutex);
  811. return ret;
  812. }
  813. static DEVICE_ATTR_RW(current_clocksource);
  814. /**
  815. * unbind_clocksource_store - interface for manually unbinding clocksource
  816. * @dev: unused
  817. * @attr: unused
  818. * @buf: unused
  819. * @count: length of buffer
  820. *
  821. * Takes input from sysfs interface for manually unbinding a clocksource.
  822. */
  823. static ssize_t unbind_clocksource_store(struct device *dev,
  824. struct device_attribute *attr,
  825. const char *buf, size_t count)
  826. {
  827. struct clocksource *cs;
  828. char name[CS_NAME_LEN];
  829. ssize_t ret;
  830. ret = sysfs_get_uname(buf, name, count);
  831. if (ret < 0)
  832. return ret;
  833. ret = -ENODEV;
  834. mutex_lock(&clocksource_mutex);
  835. list_for_each_entry(cs, &clocksource_list, list) {
  836. if (strcmp(cs->name, name))
  837. continue;
  838. ret = clocksource_unbind(cs);
  839. break;
  840. }
  841. mutex_unlock(&clocksource_mutex);
  842. return ret ? ret : count;
  843. }
  844. static DEVICE_ATTR_WO(unbind_clocksource);
  845. /**
  846. * available_clocksource_show - sysfs interface for listing clocksource
  847. * @dev: unused
  848. * @attr: unused
  849. * @buf: char buffer to be filled with clocksource list
  850. *
  851. * Provides sysfs interface for listing registered clocksources
  852. */
  853. static ssize_t available_clocksource_show(struct device *dev,
  854. struct device_attribute *attr,
  855. char *buf)
  856. {
  857. struct clocksource *src;
  858. ssize_t count = 0;
  859. mutex_lock(&clocksource_mutex);
  860. list_for_each_entry(src, &clocksource_list, list) {
  861. /*
  862. * Don't show non-HRES clocksource if the tick code is
  863. * in one shot mode (highres=on or nohz=on)
  864. */
  865. if (!tick_oneshot_mode_active() ||
  866. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  867. count += snprintf(buf + count,
  868. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  869. "%s ", src->name);
  870. }
  871. mutex_unlock(&clocksource_mutex);
  872. count += snprintf(buf + count,
  873. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  874. return count;
  875. }
  876. static DEVICE_ATTR_RO(available_clocksource);
  877. static struct attribute *clocksource_attrs[] = {
  878. &dev_attr_current_clocksource.attr,
  879. &dev_attr_unbind_clocksource.attr,
  880. &dev_attr_available_clocksource.attr,
  881. NULL
  882. };
  883. ATTRIBUTE_GROUPS(clocksource);
  884. static struct bus_type clocksource_subsys = {
  885. .name = "clocksource",
  886. .dev_name = "clocksource",
  887. };
  888. static struct device device_clocksource = {
  889. .id = 0,
  890. .bus = &clocksource_subsys,
  891. .groups = clocksource_groups,
  892. };
  893. static int __init init_clocksource_sysfs(void)
  894. {
  895. int error = subsys_system_register(&clocksource_subsys, NULL);
  896. if (!error)
  897. error = device_register(&device_clocksource);
  898. return error;
  899. }
  900. device_initcall(init_clocksource_sysfs);
  901. #endif /* CONFIG_SYSFS */
  902. /**
  903. * boot_override_clocksource - boot clock override
  904. * @str: override name
  905. *
  906. * Takes a clocksource= boot argument and uses it
  907. * as the clocksource override name.
  908. */
  909. static int __init boot_override_clocksource(char* str)
  910. {
  911. mutex_lock(&clocksource_mutex);
  912. if (str)
  913. strlcpy(override_name, str, sizeof(override_name));
  914. mutex_unlock(&clocksource_mutex);
  915. return 1;
  916. }
  917. __setup("clocksource=", boot_override_clocksource);
  918. /**
  919. * boot_override_clock - Compatibility layer for deprecated boot option
  920. * @str: override name
  921. *
  922. * DEPRECATED! Takes a clock= boot argument and uses it
  923. * as the clocksource override name
  924. */
  925. static int __init boot_override_clock(char* str)
  926. {
  927. if (!strcmp(str, "pmtmr")) {
  928. pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
  929. return boot_override_clocksource("acpi_pm");
  930. }
  931. pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
  932. return boot_override_clocksource(str);
  933. }
  934. __setup("clock=", boot_override_clock);