clocksource.c 29 KB

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