clocksource.c 30 KB

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