clocksource.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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/clocksource.h>
  26. #include <linux/sysdev.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. * @minsec: 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 @minsec 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 minsec)
  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)minsec * 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. do_div(tmp, from);
  139. if ((tmp >> sftacc) == 0)
  140. break;
  141. }
  142. *mult = tmp;
  143. *shift = sft;
  144. }
  145. /*[Clocksource internal variables]---------
  146. * curr_clocksource:
  147. * currently selected clocksource.
  148. * clocksource_list:
  149. * linked list with the registered clocksources
  150. * clocksource_mutex:
  151. * protects manipulations to curr_clocksource and the clocksource_list
  152. * override_name:
  153. * Name of the user-specified clocksource.
  154. */
  155. static struct clocksource *curr_clocksource;
  156. static LIST_HEAD(clocksource_list);
  157. static DEFINE_MUTEX(clocksource_mutex);
  158. static char override_name[32];
  159. static int finished_booting;
  160. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  161. static void clocksource_watchdog_work(struct work_struct *work);
  162. static LIST_HEAD(watchdog_list);
  163. static struct clocksource *watchdog;
  164. static struct timer_list watchdog_timer;
  165. static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
  166. static DEFINE_SPINLOCK(watchdog_lock);
  167. static cycle_t watchdog_last;
  168. static int watchdog_running;
  169. static int clocksource_watchdog_kthread(void *data);
  170. static void __clocksource_change_rating(struct clocksource *cs, int rating);
  171. /*
  172. * Interval: 0.5sec Threshold: 0.0625s
  173. */
  174. #define WATCHDOG_INTERVAL (HZ >> 1)
  175. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
  176. static void clocksource_watchdog_work(struct work_struct *work)
  177. {
  178. /*
  179. * If kthread_run fails the next watchdog scan over the
  180. * watchdog_list will find the unstable clock again.
  181. */
  182. kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
  183. }
  184. static void __clocksource_unstable(struct clocksource *cs)
  185. {
  186. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  187. cs->flags |= CLOCK_SOURCE_UNSTABLE;
  188. if (finished_booting)
  189. schedule_work(&watchdog_work);
  190. }
  191. static void clocksource_unstable(struct clocksource *cs, int64_t delta)
  192. {
  193. printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
  194. cs->name, delta);
  195. __clocksource_unstable(cs);
  196. }
  197. /**
  198. * clocksource_mark_unstable - mark clocksource unstable via watchdog
  199. * @cs: clocksource to be marked unstable
  200. *
  201. * This function is called instead of clocksource_change_rating from
  202. * cpu hotplug code to avoid a deadlock between the clocksource mutex
  203. * and the cpu hotplug mutex. It defers the update of the clocksource
  204. * to the watchdog thread.
  205. */
  206. void clocksource_mark_unstable(struct clocksource *cs)
  207. {
  208. unsigned long flags;
  209. spin_lock_irqsave(&watchdog_lock, flags);
  210. if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
  211. if (list_empty(&cs->wd_list))
  212. list_add(&cs->wd_list, &watchdog_list);
  213. __clocksource_unstable(cs);
  214. }
  215. spin_unlock_irqrestore(&watchdog_lock, flags);
  216. }
  217. static void clocksource_watchdog(unsigned long data)
  218. {
  219. struct clocksource *cs;
  220. cycle_t csnow, wdnow;
  221. int64_t wd_nsec, cs_nsec;
  222. int next_cpu;
  223. spin_lock(&watchdog_lock);
  224. if (!watchdog_running)
  225. goto out;
  226. wdnow = watchdog->read(watchdog);
  227. wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
  228. watchdog->mult, watchdog->shift);
  229. watchdog_last = wdnow;
  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. csnow = cs->read(cs);
  238. /* Clocksource initialized ? */
  239. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
  240. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  241. cs->wd_last = csnow;
  242. continue;
  243. }
  244. /* Check the deviation from the watchdog clocksource. */
  245. cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) &
  246. cs->mask, cs->mult, cs->shift);
  247. cs->wd_last = csnow;
  248. if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
  249. clocksource_unstable(cs, cs_nsec - wd_nsec);
  250. continue;
  251. }
  252. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  253. (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  254. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  255. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  256. /*
  257. * We just marked the clocksource as highres-capable,
  258. * notify the rest of the system as well so that we
  259. * transition into high-res mode:
  260. */
  261. tick_clock_notify();
  262. }
  263. }
  264. /*
  265. * Cycle through CPUs to check if the CPUs stay synchronized
  266. * to each other.
  267. */
  268. next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  269. if (next_cpu >= nr_cpu_ids)
  270. next_cpu = cpumask_first(cpu_online_mask);
  271. watchdog_timer.expires += WATCHDOG_INTERVAL;
  272. add_timer_on(&watchdog_timer, next_cpu);
  273. out:
  274. spin_unlock(&watchdog_lock);
  275. }
  276. static inline void clocksource_start_watchdog(void)
  277. {
  278. if (watchdog_running || !watchdog || list_empty(&watchdog_list))
  279. return;
  280. init_timer(&watchdog_timer);
  281. watchdog_timer.function = clocksource_watchdog;
  282. watchdog_last = watchdog->read(watchdog);
  283. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  284. add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
  285. watchdog_running = 1;
  286. }
  287. static inline void clocksource_stop_watchdog(void)
  288. {
  289. if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
  290. return;
  291. del_timer(&watchdog_timer);
  292. watchdog_running = 0;
  293. }
  294. static inline void clocksource_reset_watchdog(void)
  295. {
  296. struct clocksource *cs;
  297. list_for_each_entry(cs, &watchdog_list, wd_list)
  298. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  299. }
  300. static void clocksource_resume_watchdog(void)
  301. {
  302. unsigned long flags;
  303. /*
  304. * We use trylock here to avoid a potential dead lock when
  305. * kgdb calls this code after the kernel has been stopped with
  306. * watchdog_lock held. When watchdog_lock is held we just
  307. * return and accept, that the watchdog might trigger and mark
  308. * the monitored clock source (usually TSC) unstable.
  309. *
  310. * This does not affect the other caller clocksource_resume()
  311. * because at this point the kernel is UP, interrupts are
  312. * disabled and nothing can hold watchdog_lock.
  313. */
  314. if (!spin_trylock_irqsave(&watchdog_lock, flags))
  315. return;
  316. clocksource_reset_watchdog();
  317. spin_unlock_irqrestore(&watchdog_lock, flags);
  318. }
  319. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  320. {
  321. unsigned long flags;
  322. spin_lock_irqsave(&watchdog_lock, flags);
  323. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  324. /* cs is a clocksource to be watched. */
  325. list_add(&cs->wd_list, &watchdog_list);
  326. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  327. } else {
  328. /* cs is a watchdog. */
  329. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  330. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  331. /* Pick the best watchdog. */
  332. if (!watchdog || cs->rating > watchdog->rating) {
  333. watchdog = cs;
  334. /* Reset watchdog cycles */
  335. clocksource_reset_watchdog();
  336. }
  337. }
  338. /* Check if the watchdog timer needs to be started. */
  339. clocksource_start_watchdog();
  340. spin_unlock_irqrestore(&watchdog_lock, flags);
  341. }
  342. static void clocksource_dequeue_watchdog(struct clocksource *cs)
  343. {
  344. struct clocksource *tmp;
  345. unsigned long flags;
  346. spin_lock_irqsave(&watchdog_lock, flags);
  347. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  348. /* cs is a watched clocksource. */
  349. list_del_init(&cs->wd_list);
  350. } else if (cs == watchdog) {
  351. /* Reset watchdog cycles */
  352. clocksource_reset_watchdog();
  353. /* Current watchdog is removed. Find an alternative. */
  354. watchdog = NULL;
  355. list_for_each_entry(tmp, &clocksource_list, list) {
  356. if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
  357. continue;
  358. if (!watchdog || tmp->rating > watchdog->rating)
  359. watchdog = tmp;
  360. }
  361. }
  362. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  363. /* Check if the watchdog timer needs to be stopped. */
  364. clocksource_stop_watchdog();
  365. spin_unlock_irqrestore(&watchdog_lock, flags);
  366. }
  367. static int clocksource_watchdog_kthread(void *data)
  368. {
  369. struct clocksource *cs, *tmp;
  370. unsigned long flags;
  371. LIST_HEAD(unstable);
  372. mutex_lock(&clocksource_mutex);
  373. spin_lock_irqsave(&watchdog_lock, flags);
  374. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
  375. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  376. list_del_init(&cs->wd_list);
  377. list_add(&cs->wd_list, &unstable);
  378. }
  379. /* Check if the watchdog timer needs to be stopped. */
  380. clocksource_stop_watchdog();
  381. spin_unlock_irqrestore(&watchdog_lock, flags);
  382. /* Needs to be done outside of watchdog lock */
  383. list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
  384. list_del_init(&cs->wd_list);
  385. __clocksource_change_rating(cs, 0);
  386. }
  387. mutex_unlock(&clocksource_mutex);
  388. return 0;
  389. }
  390. #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
  391. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  392. {
  393. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  394. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  395. }
  396. static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
  397. static inline void clocksource_resume_watchdog(void) { }
  398. static inline int clocksource_watchdog_kthread(void *data) { return 0; }
  399. #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
  400. /**
  401. * clocksource_suspend - suspend the clocksource(s)
  402. */
  403. void clocksource_suspend(void)
  404. {
  405. struct clocksource *cs;
  406. list_for_each_entry_reverse(cs, &clocksource_list, list)
  407. if (cs->suspend)
  408. cs->suspend(cs);
  409. }
  410. /**
  411. * clocksource_resume - resume the clocksource(s)
  412. */
  413. void clocksource_resume(void)
  414. {
  415. struct clocksource *cs;
  416. list_for_each_entry(cs, &clocksource_list, list)
  417. if (cs->resume)
  418. cs->resume(cs);
  419. clocksource_resume_watchdog();
  420. }
  421. /**
  422. * clocksource_touch_watchdog - Update watchdog
  423. *
  424. * Update the watchdog after exception contexts such as kgdb so as not
  425. * to incorrectly trip the watchdog. This might fail when the kernel
  426. * was stopped in code which holds watchdog_lock.
  427. */
  428. void clocksource_touch_watchdog(void)
  429. {
  430. clocksource_resume_watchdog();
  431. }
  432. /**
  433. * clocksource_max_deferment - Returns max time the clocksource can be deferred
  434. * @cs: Pointer to clocksource
  435. *
  436. */
  437. static u64 clocksource_max_deferment(struct clocksource *cs)
  438. {
  439. u64 max_nsecs, max_cycles;
  440. /*
  441. * Calculate the maximum number of cycles that we can pass to the
  442. * cyc2ns function without overflowing a 64-bit signed result. The
  443. * maximum number of cycles is equal to ULLONG_MAX/cs->mult which
  444. * is equivalent to the below.
  445. * max_cycles < (2^63)/cs->mult
  446. * max_cycles < 2^(log2((2^63)/cs->mult))
  447. * max_cycles < 2^(log2(2^63) - log2(cs->mult))
  448. * max_cycles < 2^(63 - log2(cs->mult))
  449. * max_cycles < 1 << (63 - log2(cs->mult))
  450. * Please note that we add 1 to the result of the log2 to account for
  451. * any rounding errors, ensure the above inequality is satisfied and
  452. * no overflow will occur.
  453. */
  454. max_cycles = 1ULL << (63 - (ilog2(cs->mult) + 1));
  455. /*
  456. * The actual maximum number of cycles we can defer the clocksource is
  457. * determined by the minimum of max_cycles and cs->mask.
  458. */
  459. max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
  460. max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult, cs->shift);
  461. /*
  462. * To ensure that the clocksource does not wrap whilst we are idle,
  463. * limit the time the clocksource can be deferred by 12.5%. Please
  464. * note a margin of 12.5% is used because this can be computed with
  465. * a shift, versus say 10% which would require division.
  466. */
  467. return max_nsecs - (max_nsecs >> 5);
  468. }
  469. #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
  470. /**
  471. * clocksource_select - Select the best clocksource available
  472. *
  473. * Private function. Must hold clocksource_mutex when called.
  474. *
  475. * Select the clocksource with the best rating, or the clocksource,
  476. * which is selected by userspace override.
  477. */
  478. static void clocksource_select(void)
  479. {
  480. struct clocksource *best, *cs;
  481. if (!finished_booting || list_empty(&clocksource_list))
  482. return;
  483. /* First clocksource on the list has the best rating. */
  484. best = list_first_entry(&clocksource_list, struct clocksource, list);
  485. /* Check for the override clocksource. */
  486. list_for_each_entry(cs, &clocksource_list, list) {
  487. if (strcmp(cs->name, override_name) != 0)
  488. continue;
  489. /*
  490. * Check to make sure we don't switch to a non-highres
  491. * capable clocksource if the tick code is in oneshot
  492. * mode (highres or nohz)
  493. */
  494. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  495. tick_oneshot_mode_active()) {
  496. /* Override clocksource cannot be used. */
  497. printk(KERN_WARNING "Override clocksource %s is not "
  498. "HRT compatible. Cannot switch while in "
  499. "HRT/NOHZ mode\n", cs->name);
  500. override_name[0] = 0;
  501. } else
  502. /* Override clocksource can be used. */
  503. best = cs;
  504. break;
  505. }
  506. if (curr_clocksource != best) {
  507. printk(KERN_INFO "Switching to clocksource %s\n", best->name);
  508. curr_clocksource = best;
  509. timekeeping_notify(curr_clocksource);
  510. }
  511. }
  512. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  513. static inline void clocksource_select(void) { }
  514. #endif
  515. /*
  516. * clocksource_done_booting - Called near the end of core bootup
  517. *
  518. * Hack to avoid lots of clocksource churn at boot time.
  519. * We use fs_initcall because we want this to start before
  520. * device_initcall but after subsys_initcall.
  521. */
  522. static int __init clocksource_done_booting(void)
  523. {
  524. mutex_lock(&clocksource_mutex);
  525. curr_clocksource = clocksource_default_clock();
  526. mutex_unlock(&clocksource_mutex);
  527. finished_booting = 1;
  528. /*
  529. * Run the watchdog first to eliminate unstable clock sources
  530. */
  531. clocksource_watchdog_kthread(NULL);
  532. mutex_lock(&clocksource_mutex);
  533. clocksource_select();
  534. mutex_unlock(&clocksource_mutex);
  535. return 0;
  536. }
  537. fs_initcall(clocksource_done_booting);
  538. /*
  539. * Enqueue the clocksource sorted by rating
  540. */
  541. static void clocksource_enqueue(struct clocksource *cs)
  542. {
  543. struct list_head *entry = &clocksource_list;
  544. struct clocksource *tmp;
  545. list_for_each_entry(tmp, &clocksource_list, list)
  546. /* Keep track of the place, where to insert */
  547. if (tmp->rating >= cs->rating)
  548. entry = &tmp->list;
  549. list_add(&cs->list, entry);
  550. }
  551. /*
  552. * Maximum time we expect to go between ticks. This includes idle
  553. * tickless time. It provides the trade off between selecting a
  554. * mult/shift pair that is very precise but can only handle a short
  555. * period of time, vs. a mult/shift pair that can handle long periods
  556. * of time but isn't as precise.
  557. *
  558. * This is a subsystem constant, and actual hardware limitations
  559. * may override it (ie: clocksources that wrap every 3 seconds).
  560. */
  561. #define MAX_UPDATE_LENGTH 5 /* Seconds */
  562. /**
  563. * __clocksource_updatefreq_scale - Used update clocksource with new freq
  564. * @t: clocksource to be registered
  565. * @scale: Scale factor multiplied against freq to get clocksource hz
  566. * @freq: clocksource frequency (cycles per second) divided by scale
  567. *
  568. * This should only be called from the clocksource->enable() method.
  569. *
  570. * This *SHOULD NOT* be called directly! Please use the
  571. * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
  572. */
  573. void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
  574. {
  575. /*
  576. * Ideally we want to use some of the limits used in
  577. * clocksource_max_deferment, to provide a more informed
  578. * MAX_UPDATE_LENGTH. But for now this just gets the
  579. * register interface working properly.
  580. */
  581. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  582. NSEC_PER_SEC/scale,
  583. MAX_UPDATE_LENGTH*scale);
  584. cs->max_idle_ns = clocksource_max_deferment(cs);
  585. }
  586. EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
  587. /**
  588. * __clocksource_register_scale - Used to install new clocksources
  589. * @t: clocksource to be registered
  590. * @scale: Scale factor multiplied against freq to get clocksource hz
  591. * @freq: clocksource frequency (cycles per second) divided by scale
  592. *
  593. * Returns -EBUSY if registration fails, zero otherwise.
  594. *
  595. * This *SHOULD NOT* be called directly! Please use the
  596. * clocksource_register_hz() or clocksource_register_khz helper functions.
  597. */
  598. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  599. {
  600. /* Intialize mult/shift and max_idle_ns */
  601. __clocksource_updatefreq_scale(cs, scale, freq);
  602. /* Add clocksource to the clcoksource list */
  603. mutex_lock(&clocksource_mutex);
  604. clocksource_enqueue(cs);
  605. clocksource_select();
  606. clocksource_enqueue_watchdog(cs);
  607. mutex_unlock(&clocksource_mutex);
  608. return 0;
  609. }
  610. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  611. /**
  612. * clocksource_register - Used to install new clocksources
  613. * @t: clocksource to be registered
  614. *
  615. * Returns -EBUSY if registration fails, zero otherwise.
  616. */
  617. int clocksource_register(struct clocksource *cs)
  618. {
  619. /* calculate max idle time permitted for this clocksource */
  620. cs->max_idle_ns = clocksource_max_deferment(cs);
  621. mutex_lock(&clocksource_mutex);
  622. clocksource_enqueue(cs);
  623. clocksource_select();
  624. clocksource_enqueue_watchdog(cs);
  625. mutex_unlock(&clocksource_mutex);
  626. return 0;
  627. }
  628. EXPORT_SYMBOL(clocksource_register);
  629. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  630. {
  631. list_del(&cs->list);
  632. cs->rating = rating;
  633. clocksource_enqueue(cs);
  634. clocksource_select();
  635. }
  636. /**
  637. * clocksource_change_rating - Change the rating of a registered clocksource
  638. */
  639. void clocksource_change_rating(struct clocksource *cs, int rating)
  640. {
  641. mutex_lock(&clocksource_mutex);
  642. __clocksource_change_rating(cs, rating);
  643. mutex_unlock(&clocksource_mutex);
  644. }
  645. EXPORT_SYMBOL(clocksource_change_rating);
  646. /**
  647. * clocksource_unregister - remove a registered clocksource
  648. */
  649. void clocksource_unregister(struct clocksource *cs)
  650. {
  651. mutex_lock(&clocksource_mutex);
  652. clocksource_dequeue_watchdog(cs);
  653. list_del(&cs->list);
  654. clocksource_select();
  655. mutex_unlock(&clocksource_mutex);
  656. }
  657. EXPORT_SYMBOL(clocksource_unregister);
  658. #ifdef CONFIG_SYSFS
  659. /**
  660. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  661. * @dev: unused
  662. * @buf: char buffer to be filled with clocksource list
  663. *
  664. * Provides sysfs interface for listing current clocksource.
  665. */
  666. static ssize_t
  667. sysfs_show_current_clocksources(struct sys_device *dev,
  668. struct sysdev_attribute *attr, char *buf)
  669. {
  670. ssize_t count = 0;
  671. mutex_lock(&clocksource_mutex);
  672. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  673. mutex_unlock(&clocksource_mutex);
  674. return count;
  675. }
  676. /**
  677. * sysfs_override_clocksource - interface for manually overriding clocksource
  678. * @dev: unused
  679. * @buf: name of override clocksource
  680. * @count: length of buffer
  681. *
  682. * Takes input from sysfs interface for manually overriding the default
  683. * clocksource selection.
  684. */
  685. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  686. struct sysdev_attribute *attr,
  687. const char *buf, size_t count)
  688. {
  689. size_t ret = count;
  690. /* strings from sysfs write are not 0 terminated! */
  691. if (count >= sizeof(override_name))
  692. return -EINVAL;
  693. /* strip of \n: */
  694. if (buf[count-1] == '\n')
  695. count--;
  696. mutex_lock(&clocksource_mutex);
  697. if (count > 0)
  698. memcpy(override_name, buf, count);
  699. override_name[count] = 0;
  700. clocksource_select();
  701. mutex_unlock(&clocksource_mutex);
  702. return ret;
  703. }
  704. /**
  705. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  706. * @dev: unused
  707. * @buf: char buffer to be filled with clocksource list
  708. *
  709. * Provides sysfs interface for listing registered clocksources
  710. */
  711. static ssize_t
  712. sysfs_show_available_clocksources(struct sys_device *dev,
  713. struct sysdev_attribute *attr,
  714. char *buf)
  715. {
  716. struct clocksource *src;
  717. ssize_t count = 0;
  718. mutex_lock(&clocksource_mutex);
  719. list_for_each_entry(src, &clocksource_list, list) {
  720. /*
  721. * Don't show non-HRES clocksource if the tick code is
  722. * in one shot mode (highres=on or nohz=on)
  723. */
  724. if (!tick_oneshot_mode_active() ||
  725. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  726. count += snprintf(buf + count,
  727. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  728. "%s ", src->name);
  729. }
  730. mutex_unlock(&clocksource_mutex);
  731. count += snprintf(buf + count,
  732. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  733. return count;
  734. }
  735. /*
  736. * Sysfs setup bits:
  737. */
  738. static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  739. sysfs_override_clocksource);
  740. static SYSDEV_ATTR(available_clocksource, 0444,
  741. sysfs_show_available_clocksources, NULL);
  742. static struct sysdev_class clocksource_sysclass = {
  743. .name = "clocksource",
  744. };
  745. static struct sys_device device_clocksource = {
  746. .id = 0,
  747. .cls = &clocksource_sysclass,
  748. };
  749. static int __init init_clocksource_sysfs(void)
  750. {
  751. int error = sysdev_class_register(&clocksource_sysclass);
  752. if (!error)
  753. error = sysdev_register(&device_clocksource);
  754. if (!error)
  755. error = sysdev_create_file(
  756. &device_clocksource,
  757. &attr_current_clocksource);
  758. if (!error)
  759. error = sysdev_create_file(
  760. &device_clocksource,
  761. &attr_available_clocksource);
  762. return error;
  763. }
  764. device_initcall(init_clocksource_sysfs);
  765. #endif /* CONFIG_SYSFS */
  766. /**
  767. * boot_override_clocksource - boot clock override
  768. * @str: override name
  769. *
  770. * Takes a clocksource= boot argument and uses it
  771. * as the clocksource override name.
  772. */
  773. static int __init boot_override_clocksource(char* str)
  774. {
  775. mutex_lock(&clocksource_mutex);
  776. if (str)
  777. strlcpy(override_name, str, sizeof(override_name));
  778. mutex_unlock(&clocksource_mutex);
  779. return 1;
  780. }
  781. __setup("clocksource=", boot_override_clocksource);
  782. /**
  783. * boot_override_clock - Compatibility layer for deprecated boot option
  784. * @str: override name
  785. *
  786. * DEPRECATED! Takes a clock= boot argument and uses it
  787. * as the clocksource override name
  788. */
  789. static int __init boot_override_clock(char* str)
  790. {
  791. if (!strcmp(str, "pmtmr")) {
  792. printk("Warning: clock=pmtmr is deprecated. "
  793. "Use clocksource=acpi_pm.\n");
  794. return boot_override_clocksource("acpi_pm");
  795. }
  796. printk("Warning! clock= boot option is deprecated. "
  797. "Use clocksource=xyz\n");
  798. return boot_override_clocksource(str);
  799. }
  800. __setup("clock=", boot_override_clock);