clocksource.c 28 KB

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