clocksource.c 27 KB

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