clocksource.c 29 KB

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