clocksource.c 30 KB

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