clocksource.c 29 KB

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