clocksource.c 28 KB

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