clocksource.c 28 KB

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