clocksource.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. * clocksource_max_deferment - Returns max time the clocksource can be deferred
  471. * @cs: Pointer to clocksource
  472. *
  473. */
  474. static u64 clocksource_max_deferment(struct clocksource *cs)
  475. {
  476. u64 max_nsecs, max_cycles;
  477. /*
  478. * Calculate the maximum number of cycles that we can pass to the
  479. * cyc2ns function without overflowing a 64-bit signed result. The
  480. * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
  481. * which is equivalent to the below.
  482. * max_cycles < (2^63)/(cs->mult + cs->maxadj)
  483. * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
  484. * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
  485. * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
  486. * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
  487. * Please note that we add 1 to the result of the log2 to account for
  488. * any rounding errors, ensure the above inequality is satisfied and
  489. * no overflow will occur.
  490. */
  491. max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
  492. /*
  493. * The actual maximum number of cycles we can defer the clocksource is
  494. * determined by the minimum of max_cycles and cs->mask.
  495. * Note: Here we subtract the maxadj to make sure we don't sleep for
  496. * too long if there's a large negative adjustment.
  497. */
  498. max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
  499. max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
  500. cs->shift);
  501. /*
  502. * To ensure that the clocksource does not wrap whilst we are idle,
  503. * limit the time the clocksource can be deferred by 12.5%. Please
  504. * note a margin of 12.5% is used because this can be computed with
  505. * a shift, versus say 10% which would require division.
  506. */
  507. return max_nsecs - (max_nsecs >> 3);
  508. }
  509. #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
  510. static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
  511. {
  512. struct clocksource *cs;
  513. if (!finished_booting || list_empty(&clocksource_list))
  514. return NULL;
  515. /*
  516. * We pick the clocksource with the highest rating. If oneshot
  517. * mode is active, we pick the highres valid clocksource with
  518. * the best rating.
  519. */
  520. list_for_each_entry(cs, &clocksource_list, list) {
  521. if (skipcur && cs == curr_clocksource)
  522. continue;
  523. if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  524. continue;
  525. return cs;
  526. }
  527. return NULL;
  528. }
  529. static void __clocksource_select(bool skipcur)
  530. {
  531. bool oneshot = tick_oneshot_mode_active();
  532. struct clocksource *best, *cs;
  533. /* Find the best suitable clocksource */
  534. best = clocksource_find_best(oneshot, skipcur);
  535. if (!best)
  536. return;
  537. /* Check for the override clocksource. */
  538. list_for_each_entry(cs, &clocksource_list, list) {
  539. if (skipcur && cs == curr_clocksource)
  540. continue;
  541. if (strcmp(cs->name, override_name) != 0)
  542. continue;
  543. /*
  544. * Check to make sure we don't switch to a non-highres
  545. * capable clocksource if the tick code is in oneshot
  546. * mode (highres or nohz)
  547. */
  548. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
  549. /* Override clocksource cannot be used. */
  550. printk(KERN_WARNING "Override clocksource %s is not "
  551. "HRT compatible. Cannot switch while in "
  552. "HRT/NOHZ mode\n", cs->name);
  553. override_name[0] = 0;
  554. } else
  555. /* Override clocksource can be used. */
  556. best = cs;
  557. break;
  558. }
  559. if (curr_clocksource != best && !timekeeping_notify(best)) {
  560. pr_info("Switched to clocksource %s\n", best->name);
  561. curr_clocksource = best;
  562. }
  563. }
  564. /**
  565. * clocksource_select - Select the best clocksource available
  566. *
  567. * Private function. Must hold clocksource_mutex when called.
  568. *
  569. * Select the clocksource with the best rating, or the clocksource,
  570. * which is selected by userspace override.
  571. */
  572. static void clocksource_select(void)
  573. {
  574. return __clocksource_select(false);
  575. }
  576. static void clocksource_select_fallback(void)
  577. {
  578. return __clocksource_select(true);
  579. }
  580. #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
  581. static inline void clocksource_select(void) { }
  582. static inline void clocksource_select_fallback(void) { }
  583. #endif
  584. /*
  585. * clocksource_done_booting - Called near the end of core bootup
  586. *
  587. * Hack to avoid lots of clocksource churn at boot time.
  588. * We use fs_initcall because we want this to start before
  589. * device_initcall but after subsys_initcall.
  590. */
  591. static int __init clocksource_done_booting(void)
  592. {
  593. mutex_lock(&clocksource_mutex);
  594. curr_clocksource = clocksource_default_clock();
  595. finished_booting = 1;
  596. /*
  597. * Run the watchdog first to eliminate unstable clock sources
  598. */
  599. __clocksource_watchdog_kthread();
  600. clocksource_select();
  601. mutex_unlock(&clocksource_mutex);
  602. return 0;
  603. }
  604. fs_initcall(clocksource_done_booting);
  605. /*
  606. * Enqueue the clocksource sorted by rating
  607. */
  608. static void clocksource_enqueue(struct clocksource *cs)
  609. {
  610. struct list_head *entry = &clocksource_list;
  611. struct clocksource *tmp;
  612. list_for_each_entry(tmp, &clocksource_list, list)
  613. /* Keep track of the place, where to insert */
  614. if (tmp->rating >= cs->rating)
  615. entry = &tmp->list;
  616. list_add(&cs->list, entry);
  617. }
  618. /**
  619. * __clocksource_updatefreq_scale - Used update clocksource with new freq
  620. * @cs: clocksource to be registered
  621. * @scale: Scale factor multiplied against freq to get clocksource hz
  622. * @freq: clocksource frequency (cycles per second) divided by scale
  623. *
  624. * This should only be called from the clocksource->enable() method.
  625. *
  626. * This *SHOULD NOT* be called directly! Please use the
  627. * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
  628. */
  629. void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
  630. {
  631. u64 sec;
  632. /*
  633. * Calc the maximum number of seconds which we can run before
  634. * wrapping around. For clocksources which have a mask > 32bit
  635. * we need to limit the max sleep time to have a good
  636. * conversion precision. 10 minutes is still a reasonable
  637. * amount. That results in a shift value of 24 for a
  638. * clocksource with mask >= 40bit and f >= 4GHz. That maps to
  639. * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
  640. * margin as we do in clocksource_max_deferment()
  641. */
  642. sec = (cs->mask - (cs->mask >> 3));
  643. do_div(sec, freq);
  644. do_div(sec, scale);
  645. if (!sec)
  646. sec = 1;
  647. else if (sec > 600 && cs->mask > UINT_MAX)
  648. sec = 600;
  649. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  650. NSEC_PER_SEC / scale, sec * scale);
  651. /*
  652. * for clocksources that have large mults, to avoid overflow.
  653. * Since mult may be adjusted by ntp, add an safety extra margin
  654. *
  655. */
  656. cs->maxadj = clocksource_max_adjustment(cs);
  657. while ((cs->mult + cs->maxadj < cs->mult)
  658. || (cs->mult - cs->maxadj > cs->mult)) {
  659. cs->mult >>= 1;
  660. cs->shift--;
  661. cs->maxadj = clocksource_max_adjustment(cs);
  662. }
  663. cs->max_idle_ns = clocksource_max_deferment(cs);
  664. }
  665. EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
  666. /**
  667. * __clocksource_register_scale - Used to install new clocksources
  668. * @cs: clocksource to be registered
  669. * @scale: Scale factor multiplied against freq to get clocksource hz
  670. * @freq: clocksource frequency (cycles per second) divided by scale
  671. *
  672. * Returns -EBUSY if registration fails, zero otherwise.
  673. *
  674. * This *SHOULD NOT* be called directly! Please use the
  675. * clocksource_register_hz() or clocksource_register_khz helper functions.
  676. */
  677. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  678. {
  679. /* Initialize mult/shift and max_idle_ns */
  680. __clocksource_updatefreq_scale(cs, scale, freq);
  681. /* Add clocksource to the clcoksource list */
  682. mutex_lock(&clocksource_mutex);
  683. clocksource_enqueue(cs);
  684. clocksource_enqueue_watchdog(cs);
  685. clocksource_select();
  686. mutex_unlock(&clocksource_mutex);
  687. return 0;
  688. }
  689. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  690. /**
  691. * clocksource_register - Used to install new clocksources
  692. * @cs: clocksource to be registered
  693. *
  694. * Returns -EBUSY if registration fails, zero otherwise.
  695. */
  696. int clocksource_register(struct clocksource *cs)
  697. {
  698. /* calculate max adjustment for given mult/shift */
  699. cs->maxadj = clocksource_max_adjustment(cs);
  700. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  701. "Clocksource %s might overflow on 11%% adjustment\n",
  702. cs->name);
  703. /* calculate max idle time permitted for this clocksource */
  704. cs->max_idle_ns = clocksource_max_deferment(cs);
  705. mutex_lock(&clocksource_mutex);
  706. clocksource_enqueue(cs);
  707. clocksource_enqueue_watchdog(cs);
  708. clocksource_select();
  709. mutex_unlock(&clocksource_mutex);
  710. return 0;
  711. }
  712. EXPORT_SYMBOL(clocksource_register);
  713. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  714. {
  715. list_del(&cs->list);
  716. cs->rating = rating;
  717. clocksource_enqueue(cs);
  718. }
  719. /**
  720. * clocksource_change_rating - Change the rating of a registered clocksource
  721. * @cs: clocksource to be changed
  722. * @rating: new rating
  723. */
  724. void clocksource_change_rating(struct clocksource *cs, int rating)
  725. {
  726. mutex_lock(&clocksource_mutex);
  727. __clocksource_change_rating(cs, rating);
  728. clocksource_select();
  729. mutex_unlock(&clocksource_mutex);
  730. }
  731. EXPORT_SYMBOL(clocksource_change_rating);
  732. /*
  733. * Unbind clocksource @cs. Called with clocksource_mutex held
  734. */
  735. static int clocksource_unbind(struct clocksource *cs)
  736. {
  737. /*
  738. * I really can't convince myself to support this on hardware
  739. * designed by lobotomized monkeys.
  740. */
  741. if (clocksource_is_watchdog(cs))
  742. return -EBUSY;
  743. if (cs == curr_clocksource) {
  744. /* Select and try to install a replacement clock source */
  745. clocksource_select_fallback();
  746. if (curr_clocksource == cs)
  747. return -EBUSY;
  748. }
  749. clocksource_dequeue_watchdog(cs);
  750. list_del_init(&cs->list);
  751. return 0;
  752. }
  753. /**
  754. * clocksource_unregister - remove a registered clocksource
  755. * @cs: clocksource to be unregistered
  756. */
  757. int clocksource_unregister(struct clocksource *cs)
  758. {
  759. int ret = 0;
  760. mutex_lock(&clocksource_mutex);
  761. if (!list_empty(&cs->list))
  762. ret = clocksource_unbind(cs);
  763. mutex_unlock(&clocksource_mutex);
  764. return ret;
  765. }
  766. EXPORT_SYMBOL(clocksource_unregister);
  767. #ifdef CONFIG_SYSFS
  768. /**
  769. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  770. * @dev: unused
  771. * @attr: unused
  772. * @buf: char buffer to be filled with clocksource list
  773. *
  774. * Provides sysfs interface for listing current clocksource.
  775. */
  776. static ssize_t
  777. sysfs_show_current_clocksources(struct device *dev,
  778. struct device_attribute *attr, char *buf)
  779. {
  780. ssize_t count = 0;
  781. mutex_lock(&clocksource_mutex);
  782. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  783. mutex_unlock(&clocksource_mutex);
  784. return count;
  785. }
  786. size_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
  787. {
  788. size_t ret = cnt;
  789. /* strings from sysfs write are not 0 terminated! */
  790. if (!cnt || cnt >= CS_NAME_LEN)
  791. return -EINVAL;
  792. /* strip of \n: */
  793. if (buf[cnt-1] == '\n')
  794. cnt--;
  795. if (cnt > 0)
  796. memcpy(dst, buf, cnt);
  797. dst[cnt] = 0;
  798. return ret;
  799. }
  800. /**
  801. * sysfs_override_clocksource - interface for manually overriding clocksource
  802. * @dev: unused
  803. * @attr: unused
  804. * @buf: name of override clocksource
  805. * @count: length of buffer
  806. *
  807. * Takes input from sysfs interface for manually overriding the default
  808. * clocksource selection.
  809. */
  810. static ssize_t sysfs_override_clocksource(struct device *dev,
  811. struct device_attribute *attr,
  812. const char *buf, size_t count)
  813. {
  814. size_t ret;
  815. mutex_lock(&clocksource_mutex);
  816. ret = sysfs_get_uname(buf, override_name, count);
  817. if (ret >= 0)
  818. clocksource_select();
  819. mutex_unlock(&clocksource_mutex);
  820. return ret;
  821. }
  822. /**
  823. * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
  824. * @dev: unused
  825. * @attr: unused
  826. * @buf: unused
  827. * @count: length of buffer
  828. *
  829. * Takes input from sysfs interface for manually unbinding a clocksource.
  830. */
  831. static ssize_t sysfs_unbind_clocksource(struct device *dev,
  832. struct device_attribute *attr,
  833. const char *buf, size_t count)
  834. {
  835. struct clocksource *cs;
  836. char name[CS_NAME_LEN];
  837. size_t ret;
  838. ret = sysfs_get_uname(buf, name, count);
  839. if (ret < 0)
  840. return ret;
  841. ret = -ENODEV;
  842. mutex_lock(&clocksource_mutex);
  843. list_for_each_entry(cs, &clocksource_list, list) {
  844. if (strcmp(cs->name, name))
  845. continue;
  846. ret = clocksource_unbind(cs);
  847. break;
  848. }
  849. mutex_unlock(&clocksource_mutex);
  850. return ret ? ret : count;
  851. }
  852. /**
  853. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  854. * @dev: unused
  855. * @attr: unused
  856. * @buf: char buffer to be filled with clocksource list
  857. *
  858. * Provides sysfs interface for listing registered clocksources
  859. */
  860. static ssize_t
  861. sysfs_show_available_clocksources(struct device *dev,
  862. struct device_attribute *attr,
  863. char *buf)
  864. {
  865. struct clocksource *src;
  866. ssize_t count = 0;
  867. mutex_lock(&clocksource_mutex);
  868. list_for_each_entry(src, &clocksource_list, list) {
  869. /*
  870. * Don't show non-HRES clocksource if the tick code is
  871. * in one shot mode (highres=on or nohz=on)
  872. */
  873. if (!tick_oneshot_mode_active() ||
  874. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  875. count += snprintf(buf + count,
  876. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  877. "%s ", src->name);
  878. }
  879. mutex_unlock(&clocksource_mutex);
  880. count += snprintf(buf + count,
  881. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  882. return count;
  883. }
  884. /*
  885. * Sysfs setup bits:
  886. */
  887. static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  888. sysfs_override_clocksource);
  889. static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
  890. static DEVICE_ATTR(available_clocksource, 0444,
  891. sysfs_show_available_clocksources, NULL);
  892. static struct bus_type clocksource_subsys = {
  893. .name = "clocksource",
  894. .dev_name = "clocksource",
  895. };
  896. static struct device device_clocksource = {
  897. .id = 0,
  898. .bus = &clocksource_subsys,
  899. };
  900. static int __init init_clocksource_sysfs(void)
  901. {
  902. int error = subsys_system_register(&clocksource_subsys, NULL);
  903. if (!error)
  904. error = device_register(&device_clocksource);
  905. if (!error)
  906. error = device_create_file(
  907. &device_clocksource,
  908. &dev_attr_current_clocksource);
  909. if (!error)
  910. error = device_create_file(&device_clocksource,
  911. &dev_attr_unbind_clocksource);
  912. if (!error)
  913. error = device_create_file(
  914. &device_clocksource,
  915. &dev_attr_available_clocksource);
  916. return error;
  917. }
  918. device_initcall(init_clocksource_sysfs);
  919. #endif /* CONFIG_SYSFS */
  920. /**
  921. * boot_override_clocksource - boot clock override
  922. * @str: override name
  923. *
  924. * Takes a clocksource= boot argument and uses it
  925. * as the clocksource override name.
  926. */
  927. static int __init boot_override_clocksource(char* str)
  928. {
  929. mutex_lock(&clocksource_mutex);
  930. if (str)
  931. strlcpy(override_name, str, sizeof(override_name));
  932. mutex_unlock(&clocksource_mutex);
  933. return 1;
  934. }
  935. __setup("clocksource=", boot_override_clocksource);
  936. /**
  937. * boot_override_clock - Compatibility layer for deprecated boot option
  938. * @str: override name
  939. *
  940. * DEPRECATED! Takes a clock= boot argument and uses it
  941. * as the clocksource override name
  942. */
  943. static int __init boot_override_clock(char* str)
  944. {
  945. if (!strcmp(str, "pmtmr")) {
  946. printk("Warning: clock=pmtmr is deprecated. "
  947. "Use clocksource=acpi_pm.\n");
  948. return boot_override_clocksource("acpi_pm");
  949. }
  950. printk("Warning! clock= boot option is deprecated. "
  951. "Use clocksource=xyz\n");
  952. return boot_override_clocksource(str);
  953. }
  954. __setup("clock=", boot_override_clock);