clocksource.c 29 KB

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