clocksource.c 33 KB

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