clocksource.c 25 KB

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