clocksource.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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%, so that bad clock values
  420. * can be detected.
  421. */
  422. u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
  423. {
  424. u64 max_nsecs, max_cycles;
  425. /*
  426. * Calculate the maximum number of cycles that we can pass to the
  427. * cyc2ns() function without overflowing a 64-bit result.
  428. */
  429. max_cycles = ULLONG_MAX;
  430. do_div(max_cycles, mult+maxadj);
  431. /*
  432. * The actual maximum number of cycles we can defer the clocksource is
  433. * determined by the minimum of max_cycles and mask.
  434. * Note: Here we subtract the maxadj to make sure we don't sleep for
  435. * too long if there's a large negative adjustment.
  436. */
  437. max_cycles = min(max_cycles, mask);
  438. max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
  439. /* return the max_cycles value as well if requested */
  440. if (max_cyc)
  441. *max_cyc = max_cycles;
  442. /* Return 50% of the actual maximum, so we can detect bad values */
  443. max_nsecs >>= 1;
  444. return max_nsecs;
  445. }
  446. /**
  447. * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
  448. * @cs: Pointer to clocksource to be updated
  449. *
  450. */
  451. static inline void clocksource_update_max_deferment(struct clocksource *cs)
  452. {
  453. cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
  454. cs->maxadj, cs->mask,
  455. &cs->max_cycles);
  456. }
  457. #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
  458. static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
  459. {
  460. struct clocksource *cs;
  461. if (!finished_booting || list_empty(&clocksource_list))
  462. return NULL;
  463. /*
  464. * We pick the clocksource with the highest rating. If oneshot
  465. * mode is active, we pick the highres valid clocksource with
  466. * the best rating.
  467. */
  468. list_for_each_entry(cs, &clocksource_list, list) {
  469. if (skipcur && cs == curr_clocksource)
  470. continue;
  471. if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  472. continue;
  473. return cs;
  474. }
  475. return NULL;
  476. }
  477. static void __clocksource_select(bool skipcur)
  478. {
  479. bool oneshot = tick_oneshot_mode_active();
  480. struct clocksource *best, *cs;
  481. /* Find the best suitable clocksource */
  482. best = clocksource_find_best(oneshot, skipcur);
  483. if (!best)
  484. return;
  485. /* Check for the override clocksource. */
  486. list_for_each_entry(cs, &clocksource_list, list) {
  487. if (skipcur && cs == curr_clocksource)
  488. continue;
  489. if (strcmp(cs->name, override_name) != 0)
  490. continue;
  491. /*
  492. * Check to make sure we don't switch to a non-highres
  493. * capable clocksource if the tick code is in oneshot
  494. * mode (highres or nohz)
  495. */
  496. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
  497. /* Override clocksource cannot be used. */
  498. printk(KERN_WARNING "Override clocksource %s is not "
  499. "HRT compatible. Cannot switch while in "
  500. "HRT/NOHZ mode\n", cs->name);
  501. override_name[0] = 0;
  502. } else
  503. /* Override clocksource can be used. */
  504. best = cs;
  505. break;
  506. }
  507. if (curr_clocksource != best && !timekeeping_notify(best)) {
  508. pr_info("Switched to clocksource %s\n", best->name);
  509. curr_clocksource = best;
  510. }
  511. }
  512. /**
  513. * clocksource_select - Select the best clocksource available
  514. *
  515. * Private function. Must hold clocksource_mutex when called.
  516. *
  517. * Select the clocksource with the best rating, or the clocksource,
  518. * which is selected by userspace override.
  519. */
  520. static void clocksource_select(void)
  521. {
  522. return __clocksource_select(false);
  523. }
  524. static void clocksource_select_fallback(void)
  525. {
  526. return __clocksource_select(true);
  527. }
  528. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  529. static inline void clocksource_select(void) { }
  530. static inline void clocksource_select_fallback(void) { }
  531. #endif
  532. /*
  533. * clocksource_done_booting - Called near the end of core bootup
  534. *
  535. * Hack to avoid lots of clocksource churn at boot time.
  536. * We use fs_initcall because we want this to start before
  537. * device_initcall but after subsys_initcall.
  538. */
  539. static int __init clocksource_done_booting(void)
  540. {
  541. mutex_lock(&clocksource_mutex);
  542. curr_clocksource = clocksource_default_clock();
  543. finished_booting = 1;
  544. /*
  545. * Run the watchdog first to eliminate unstable clock sources
  546. */
  547. __clocksource_watchdog_kthread();
  548. clocksource_select();
  549. mutex_unlock(&clocksource_mutex);
  550. return 0;
  551. }
  552. fs_initcall(clocksource_done_booting);
  553. /*
  554. * Enqueue the clocksource sorted by rating
  555. */
  556. static void clocksource_enqueue(struct clocksource *cs)
  557. {
  558. struct list_head *entry = &clocksource_list;
  559. struct clocksource *tmp;
  560. list_for_each_entry(tmp, &clocksource_list, list)
  561. /* Keep track of the place, where to insert */
  562. if (tmp->rating >= cs->rating)
  563. entry = &tmp->list;
  564. list_add(&cs->list, entry);
  565. }
  566. /**
  567. * __clocksource_update_freq_scale - Used update clocksource with new freq
  568. * @cs: clocksource to be registered
  569. * @scale: Scale factor multiplied against freq to get clocksource hz
  570. * @freq: clocksource frequency (cycles per second) divided by scale
  571. *
  572. * This should only be called from the clocksource->enable() method.
  573. *
  574. * This *SHOULD NOT* be called directly! Please use the
  575. * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
  576. * functions.
  577. */
  578. void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
  579. {
  580. u64 sec;
  581. /*
  582. * Default clocksources are *special* and self-define their mult/shift.
  583. * But, you're not special, so you should specify a freq value.
  584. */
  585. if (freq) {
  586. /*
  587. * Calc the maximum number of seconds which we can run before
  588. * wrapping around. For clocksources which have a mask > 32-bit
  589. * we need to limit the max sleep time to have a good
  590. * conversion precision. 10 minutes is still a reasonable
  591. * amount. That results in a shift value of 24 for a
  592. * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
  593. * ~ 0.06ppm granularity for NTP.
  594. */
  595. sec = cs->mask;
  596. do_div(sec, freq);
  597. do_div(sec, scale);
  598. if (!sec)
  599. sec = 1;
  600. else if (sec > 600 && cs->mask > UINT_MAX)
  601. sec = 600;
  602. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  603. NSEC_PER_SEC / scale, sec * scale);
  604. }
  605. /*
  606. * Ensure clocksources that have large 'mult' values don't overflow
  607. * when adjusted.
  608. */
  609. cs->maxadj = clocksource_max_adjustment(cs);
  610. while (freq && ((cs->mult + cs->maxadj < cs->mult)
  611. || (cs->mult - cs->maxadj > cs->mult))) {
  612. cs->mult >>= 1;
  613. cs->shift--;
  614. cs->maxadj = clocksource_max_adjustment(cs);
  615. }
  616. /*
  617. * Only warn for *special* clocksources that self-define
  618. * their mult/shift values and don't specify a freq.
  619. */
  620. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  621. "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
  622. cs->name);
  623. clocksource_update_max_deferment(cs);
  624. pr_info("clocksource %s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
  625. cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
  626. }
  627. EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
  628. /**
  629. * __clocksource_register_scale - Used to install new clocksources
  630. * @cs: clocksource to be registered
  631. * @scale: Scale factor multiplied against freq to get clocksource hz
  632. * @freq: clocksource frequency (cycles per second) divided by scale
  633. *
  634. * Returns -EBUSY if registration fails, zero otherwise.
  635. *
  636. * This *SHOULD NOT* be called directly! Please use the
  637. * clocksource_register_hz() or clocksource_register_khz helper functions.
  638. */
  639. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  640. {
  641. /* Initialize mult/shift and max_idle_ns */
  642. __clocksource_update_freq_scale(cs, scale, freq);
  643. /* Add clocksource to the clocksource list */
  644. mutex_lock(&clocksource_mutex);
  645. clocksource_enqueue(cs);
  646. clocksource_enqueue_watchdog(cs);
  647. clocksource_select();
  648. mutex_unlock(&clocksource_mutex);
  649. return 0;
  650. }
  651. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  652. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  653. {
  654. list_del(&cs->list);
  655. cs->rating = rating;
  656. clocksource_enqueue(cs);
  657. }
  658. /**
  659. * clocksource_change_rating - Change the rating of a registered clocksource
  660. * @cs: clocksource to be changed
  661. * @rating: new rating
  662. */
  663. void clocksource_change_rating(struct clocksource *cs, int rating)
  664. {
  665. mutex_lock(&clocksource_mutex);
  666. __clocksource_change_rating(cs, rating);
  667. clocksource_select();
  668. mutex_unlock(&clocksource_mutex);
  669. }
  670. EXPORT_SYMBOL(clocksource_change_rating);
  671. /*
  672. * Unbind clocksource @cs. Called with clocksource_mutex held
  673. */
  674. static int clocksource_unbind(struct clocksource *cs)
  675. {
  676. /*
  677. * I really can't convince myself to support this on hardware
  678. * designed by lobotomized monkeys.
  679. */
  680. if (clocksource_is_watchdog(cs))
  681. return -EBUSY;
  682. if (cs == curr_clocksource) {
  683. /* Select and try to install a replacement clock source */
  684. clocksource_select_fallback();
  685. if (curr_clocksource == cs)
  686. return -EBUSY;
  687. }
  688. clocksource_dequeue_watchdog(cs);
  689. list_del_init(&cs->list);
  690. return 0;
  691. }
  692. /**
  693. * clocksource_unregister - remove a registered clocksource
  694. * @cs: clocksource to be unregistered
  695. */
  696. int clocksource_unregister(struct clocksource *cs)
  697. {
  698. int ret = 0;
  699. mutex_lock(&clocksource_mutex);
  700. if (!list_empty(&cs->list))
  701. ret = clocksource_unbind(cs);
  702. mutex_unlock(&clocksource_mutex);
  703. return ret;
  704. }
  705. EXPORT_SYMBOL(clocksource_unregister);
  706. #ifdef CONFIG_SYSFS
  707. /**
  708. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  709. * @dev: unused
  710. * @attr: unused
  711. * @buf: char buffer to be filled with clocksource list
  712. *
  713. * Provides sysfs interface for listing current clocksource.
  714. */
  715. static ssize_t
  716. sysfs_show_current_clocksources(struct device *dev,
  717. struct device_attribute *attr, char *buf)
  718. {
  719. ssize_t count = 0;
  720. mutex_lock(&clocksource_mutex);
  721. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  722. mutex_unlock(&clocksource_mutex);
  723. return count;
  724. }
  725. ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
  726. {
  727. size_t ret = cnt;
  728. /* strings from sysfs write are not 0 terminated! */
  729. if (!cnt || cnt >= CS_NAME_LEN)
  730. return -EINVAL;
  731. /* strip of \n: */
  732. if (buf[cnt-1] == '\n')
  733. cnt--;
  734. if (cnt > 0)
  735. memcpy(dst, buf, cnt);
  736. dst[cnt] = 0;
  737. return ret;
  738. }
  739. /**
  740. * sysfs_override_clocksource - interface for manually overriding clocksource
  741. * @dev: unused
  742. * @attr: unused
  743. * @buf: name of override clocksource
  744. * @count: length of buffer
  745. *
  746. * Takes input from sysfs interface for manually overriding the default
  747. * clocksource selection.
  748. */
  749. static ssize_t sysfs_override_clocksource(struct device *dev,
  750. struct device_attribute *attr,
  751. const char *buf, size_t count)
  752. {
  753. ssize_t ret;
  754. mutex_lock(&clocksource_mutex);
  755. ret = sysfs_get_uname(buf, override_name, count);
  756. if (ret >= 0)
  757. clocksource_select();
  758. mutex_unlock(&clocksource_mutex);
  759. return ret;
  760. }
  761. /**
  762. * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
  763. * @dev: unused
  764. * @attr: unused
  765. * @buf: unused
  766. * @count: length of buffer
  767. *
  768. * Takes input from sysfs interface for manually unbinding a clocksource.
  769. */
  770. static ssize_t sysfs_unbind_clocksource(struct device *dev,
  771. struct device_attribute *attr,
  772. const char *buf, size_t count)
  773. {
  774. struct clocksource *cs;
  775. char name[CS_NAME_LEN];
  776. ssize_t ret;
  777. ret = sysfs_get_uname(buf, name, count);
  778. if (ret < 0)
  779. return ret;
  780. ret = -ENODEV;
  781. mutex_lock(&clocksource_mutex);
  782. list_for_each_entry(cs, &clocksource_list, list) {
  783. if (strcmp(cs->name, name))
  784. continue;
  785. ret = clocksource_unbind(cs);
  786. break;
  787. }
  788. mutex_unlock(&clocksource_mutex);
  789. return ret ? ret : count;
  790. }
  791. /**
  792. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  793. * @dev: unused
  794. * @attr: unused
  795. * @buf: char buffer to be filled with clocksource list
  796. *
  797. * Provides sysfs interface for listing registered clocksources
  798. */
  799. static ssize_t
  800. sysfs_show_available_clocksources(struct device *dev,
  801. struct device_attribute *attr,
  802. char *buf)
  803. {
  804. struct clocksource *src;
  805. ssize_t count = 0;
  806. mutex_lock(&clocksource_mutex);
  807. list_for_each_entry(src, &clocksource_list, list) {
  808. /*
  809. * Don't show non-HRES clocksource if the tick code is
  810. * in one shot mode (highres=on or nohz=on)
  811. */
  812. if (!tick_oneshot_mode_active() ||
  813. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  814. count += snprintf(buf + count,
  815. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  816. "%s ", src->name);
  817. }
  818. mutex_unlock(&clocksource_mutex);
  819. count += snprintf(buf + count,
  820. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  821. return count;
  822. }
  823. /*
  824. * Sysfs setup bits:
  825. */
  826. static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  827. sysfs_override_clocksource);
  828. static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
  829. static DEVICE_ATTR(available_clocksource, 0444,
  830. sysfs_show_available_clocksources, NULL);
  831. static struct bus_type clocksource_subsys = {
  832. .name = "clocksource",
  833. .dev_name = "clocksource",
  834. };
  835. static struct device device_clocksource = {
  836. .id = 0,
  837. .bus = &clocksource_subsys,
  838. };
  839. static int __init init_clocksource_sysfs(void)
  840. {
  841. int error = subsys_system_register(&clocksource_subsys, NULL);
  842. if (!error)
  843. error = device_register(&device_clocksource);
  844. if (!error)
  845. error = device_create_file(
  846. &device_clocksource,
  847. &dev_attr_current_clocksource);
  848. if (!error)
  849. error = device_create_file(&device_clocksource,
  850. &dev_attr_unbind_clocksource);
  851. if (!error)
  852. error = device_create_file(
  853. &device_clocksource,
  854. &dev_attr_available_clocksource);
  855. return error;
  856. }
  857. device_initcall(init_clocksource_sysfs);
  858. #endif /* CONFIG_SYSFS */
  859. /**
  860. * boot_override_clocksource - boot clock override
  861. * @str: override name
  862. *
  863. * Takes a clocksource= boot argument and uses it
  864. * as the clocksource override name.
  865. */
  866. static int __init boot_override_clocksource(char* str)
  867. {
  868. mutex_lock(&clocksource_mutex);
  869. if (str)
  870. strlcpy(override_name, str, sizeof(override_name));
  871. mutex_unlock(&clocksource_mutex);
  872. return 1;
  873. }
  874. __setup("clocksource=", boot_override_clocksource);
  875. /**
  876. * boot_override_clock - Compatibility layer for deprecated boot option
  877. * @str: override name
  878. *
  879. * DEPRECATED! Takes a clock= boot argument and uses it
  880. * as the clocksource override name
  881. */
  882. static int __init boot_override_clock(char* str)
  883. {
  884. if (!strcmp(str, "pmtmr")) {
  885. printk("Warning: clock=pmtmr is deprecated. "
  886. "Use clocksource=acpi_pm.\n");
  887. return boot_override_clocksource("acpi_pm");
  888. }
  889. printk("Warning! clock= boot option is deprecated. "
  890. "Use clocksource=xyz\n");
  891. return boot_override_clocksource(str);
  892. }
  893. __setup("clock=", boot_override_clock);