clocksource.c 29 KB

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