clocksource.c 30 KB

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