clocksource.c 29 KB

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