clocksource.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file contains the functions which manage clocksource drivers.
  4. *
  5. * Copyright (C) 2004, 2005 IBM, John Stultz ([email protected])
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/device.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
  13. #include <linux/tick.h>
  14. #include <linux/kthread.h>
  15. #include <linux/prandom.h>
  16. #include <linux/cpu.h>
  17. #include "tick-internal.h"
  18. #include "timekeeping_internal.h"
  19. /**
  20. * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
  21. * @mult: pointer to mult variable
  22. * @shift: pointer to shift variable
  23. * @from: frequency to convert from
  24. * @to: frequency to convert to
  25. * @maxsec: guaranteed runtime conversion range in seconds
  26. *
  27. * The function evaluates the shift/mult pair for the scaled math
  28. * operations of clocksources and clockevents.
  29. *
  30. * @to and @from are frequency values in HZ. For clock sources @to is
  31. * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
  32. * event @to is the counter frequency and @from is NSEC_PER_SEC.
  33. *
  34. * The @maxsec conversion range argument controls the time frame in
  35. * seconds which must be covered by the runtime conversion with the
  36. * calculated mult and shift factors. This guarantees that no 64bit
  37. * overflow happens when the input value of the conversion is
  38. * multiplied with the calculated mult factor. Larger ranges may
  39. * reduce the conversion accuracy by choosing smaller mult and shift
  40. * factors.
  41. */
  42. void
  43. clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
  44. {
  45. u64 tmp;
  46. u32 sft, sftacc= 32;
  47. /*
  48. * Calculate the shift factor which is limiting the conversion
  49. * range:
  50. */
  51. tmp = ((u64)maxsec * from) >> 32;
  52. while (tmp) {
  53. tmp >>=1;
  54. sftacc--;
  55. }
  56. /*
  57. * Find the conversion shift/mult pair which has the best
  58. * accuracy and fits the maxsec conversion range:
  59. */
  60. for (sft = 32; sft > 0; sft--) {
  61. tmp = (u64) to << sft;
  62. tmp += from / 2;
  63. do_div(tmp, from);
  64. if ((tmp >> sftacc) == 0)
  65. break;
  66. }
  67. *mult = tmp;
  68. *shift = sft;
  69. }
  70. EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
  71. /*[Clocksource internal variables]---------
  72. * curr_clocksource:
  73. * currently selected clocksource.
  74. * suspend_clocksource:
  75. * used to calculate the suspend time.
  76. * clocksource_list:
  77. * linked list with the registered clocksources
  78. * clocksource_mutex:
  79. * protects manipulations to curr_clocksource and the clocksource_list
  80. * override_name:
  81. * Name of the user-specified clocksource.
  82. */
  83. static struct clocksource *curr_clocksource;
  84. static struct clocksource *suspend_clocksource;
  85. static LIST_HEAD(clocksource_list);
  86. static DEFINE_MUTEX(clocksource_mutex);
  87. static char override_name[CS_NAME_LEN];
  88. static int finished_booting;
  89. static u64 suspend_start;
  90. /*
  91. * Threshold: 0.0312s, when doubled: 0.0625s.
  92. * Also a default for cs->uncertainty_margin when registering clocks.
  93. */
  94. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
  95. /*
  96. * Maximum permissible delay between two readouts of the watchdog
  97. * clocksource surrounding a read of the clocksource being validated.
  98. * This delay could be due to SMIs, NMIs, or to VCPU preemptions. Used as
  99. * a lower bound for cs->uncertainty_margin values when registering clocks.
  100. */
  101. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
  102. #define MAX_SKEW_USEC CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
  103. #else
  104. #define MAX_SKEW_USEC 100
  105. #endif
  106. #define WATCHDOG_MAX_SKEW (MAX_SKEW_USEC * NSEC_PER_USEC)
  107. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  108. static void clocksource_watchdog_work(struct work_struct *work);
  109. static void clocksource_select(void);
  110. static LIST_HEAD(watchdog_list);
  111. static struct clocksource *watchdog;
  112. static struct timer_list watchdog_timer;
  113. static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
  114. static DEFINE_SPINLOCK(watchdog_lock);
  115. static int watchdog_running;
  116. static atomic_t watchdog_reset_pending;
  117. static inline void clocksource_watchdog_lock(unsigned long *flags)
  118. {
  119. spin_lock_irqsave(&watchdog_lock, *flags);
  120. }
  121. static inline void clocksource_watchdog_unlock(unsigned long *flags)
  122. {
  123. spin_unlock_irqrestore(&watchdog_lock, *flags);
  124. }
  125. static int clocksource_watchdog_kthread(void *data);
  126. static void __clocksource_change_rating(struct clocksource *cs, int rating);
  127. /*
  128. * Interval: 0.5sec.
  129. */
  130. #define WATCHDOG_INTERVAL (HZ >> 1)
  131. static void clocksource_watchdog_work(struct work_struct *work)
  132. {
  133. /*
  134. * We cannot directly run clocksource_watchdog_kthread() here, because
  135. * clocksource_select() calls timekeeping_notify() which uses
  136. * stop_machine(). One cannot use stop_machine() from a workqueue() due
  137. * lock inversions wrt CPU hotplug.
  138. *
  139. * Also, we only ever run this work once or twice during the lifetime
  140. * of the kernel, so there is no point in creating a more permanent
  141. * kthread for this.
  142. *
  143. * If kthread_run fails the next watchdog scan over the
  144. * watchdog_list will find the unstable clock again.
  145. */
  146. kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
  147. }
  148. static void __clocksource_unstable(struct clocksource *cs)
  149. {
  150. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  151. cs->flags |= CLOCK_SOURCE_UNSTABLE;
  152. /*
  153. * If the clocksource is registered clocksource_watchdog_kthread() will
  154. * re-rate and re-select.
  155. */
  156. if (list_empty(&cs->list)) {
  157. cs->rating = 0;
  158. return;
  159. }
  160. if (cs->mark_unstable)
  161. cs->mark_unstable(cs);
  162. /* kick clocksource_watchdog_kthread() */
  163. if (finished_booting)
  164. schedule_work(&watchdog_work);
  165. }
  166. /**
  167. * clocksource_mark_unstable - mark clocksource unstable via watchdog
  168. * @cs: clocksource to be marked unstable
  169. *
  170. * This function is called by the x86 TSC code to mark clocksources as unstable;
  171. * it defers demotion and re-selection to a kthread.
  172. */
  173. void clocksource_mark_unstable(struct clocksource *cs)
  174. {
  175. unsigned long flags;
  176. spin_lock_irqsave(&watchdog_lock, flags);
  177. if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
  178. if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
  179. list_add(&cs->wd_list, &watchdog_list);
  180. __clocksource_unstable(cs);
  181. }
  182. spin_unlock_irqrestore(&watchdog_lock, flags);
  183. }
  184. ulong max_cswd_read_retries = 2;
  185. module_param(max_cswd_read_retries, ulong, 0644);
  186. EXPORT_SYMBOL_GPL(max_cswd_read_retries);
  187. static int verify_n_cpus = 8;
  188. module_param(verify_n_cpus, int, 0644);
  189. enum wd_read_status {
  190. WD_READ_SUCCESS,
  191. WD_READ_UNSTABLE,
  192. WD_READ_SKIP
  193. };
  194. static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
  195. {
  196. unsigned int nretries;
  197. u64 wd_end, wd_end2, wd_delta;
  198. int64_t wd_delay, wd_seq_delay;
  199. for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
  200. local_irq_disable();
  201. *wdnow = watchdog->read(watchdog);
  202. *csnow = cs->read(cs);
  203. wd_end = watchdog->read(watchdog);
  204. wd_end2 = watchdog->read(watchdog);
  205. local_irq_enable();
  206. wd_delta = clocksource_delta(wd_end, *wdnow, watchdog->mask);
  207. wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
  208. watchdog->shift);
  209. if (wd_delay <= WATCHDOG_MAX_SKEW) {
  210. if (nretries > 1 || nretries >= max_cswd_read_retries) {
  211. pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
  212. smp_processor_id(), watchdog->name, nretries);
  213. }
  214. return WD_READ_SUCCESS;
  215. }
  216. /*
  217. * Now compute delay in consecutive watchdog read to see if
  218. * there is too much external interferences that cause
  219. * significant delay in reading both clocksource and watchdog.
  220. *
  221. * If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2,
  222. * report system busy, reinit the watchdog and skip the current
  223. * watchdog test.
  224. */
  225. wd_delta = clocksource_delta(wd_end2, wd_end, watchdog->mask);
  226. wd_seq_delay = clocksource_cyc2ns(wd_delta, watchdog->mult, watchdog->shift);
  227. if (wd_seq_delay > WATCHDOG_MAX_SKEW/2)
  228. goto skip_test;
  229. }
  230. pr_warn("timekeeping watchdog on CPU%d: %s read-back delay of %lldns, attempt %d, marking unstable\n",
  231. smp_processor_id(), watchdog->name, wd_delay, nretries);
  232. return WD_READ_UNSTABLE;
  233. skip_test:
  234. pr_info("timekeeping watchdog on CPU%d: %s wd-wd read-back delay of %lldns\n",
  235. smp_processor_id(), watchdog->name, wd_seq_delay);
  236. pr_info("wd-%s-wd read-back delay of %lldns, clock-skew test skipped!\n",
  237. cs->name, wd_delay);
  238. return WD_READ_SKIP;
  239. }
  240. static u64 csnow_mid;
  241. static cpumask_t cpus_ahead;
  242. static cpumask_t cpus_behind;
  243. static cpumask_t cpus_chosen;
  244. static void clocksource_verify_choose_cpus(void)
  245. {
  246. int cpu, i, n = verify_n_cpus;
  247. if (n < 0) {
  248. /* Check all of the CPUs. */
  249. cpumask_copy(&cpus_chosen, cpu_online_mask);
  250. cpumask_clear_cpu(smp_processor_id(), &cpus_chosen);
  251. return;
  252. }
  253. /* If no checking desired, or no other CPU to check, leave. */
  254. cpumask_clear(&cpus_chosen);
  255. if (n == 0 || num_online_cpus() <= 1)
  256. return;
  257. /* Make sure to select at least one CPU other than the current CPU. */
  258. cpu = cpumask_first(cpu_online_mask);
  259. if (cpu == smp_processor_id())
  260. cpu = cpumask_next(cpu, cpu_online_mask);
  261. if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
  262. return;
  263. cpumask_set_cpu(cpu, &cpus_chosen);
  264. /* Force a sane value for the boot parameter. */
  265. if (n > nr_cpu_ids)
  266. n = nr_cpu_ids;
  267. /*
  268. * Randomly select the specified number of CPUs. If the same
  269. * CPU is selected multiple times, that CPU is checked only once,
  270. * and no replacement CPU is selected. This gracefully handles
  271. * situations where verify_n_cpus is greater than the number of
  272. * CPUs that are currently online.
  273. */
  274. for (i = 1; i < n; i++) {
  275. cpu = prandom_u32_max(nr_cpu_ids);
  276. cpu = cpumask_next(cpu - 1, cpu_online_mask);
  277. if (cpu >= nr_cpu_ids)
  278. cpu = cpumask_first(cpu_online_mask);
  279. if (!WARN_ON_ONCE(cpu >= nr_cpu_ids))
  280. cpumask_set_cpu(cpu, &cpus_chosen);
  281. }
  282. /* Don't verify ourselves. */
  283. cpumask_clear_cpu(smp_processor_id(), &cpus_chosen);
  284. }
  285. static void clocksource_verify_one_cpu(void *csin)
  286. {
  287. struct clocksource *cs = (struct clocksource *)csin;
  288. csnow_mid = cs->read(cs);
  289. }
  290. void clocksource_verify_percpu(struct clocksource *cs)
  291. {
  292. int64_t cs_nsec, cs_nsec_max = 0, cs_nsec_min = LLONG_MAX;
  293. u64 csnow_begin, csnow_end;
  294. int cpu, testcpu;
  295. s64 delta;
  296. if (verify_n_cpus == 0)
  297. return;
  298. cpumask_clear(&cpus_ahead);
  299. cpumask_clear(&cpus_behind);
  300. cpus_read_lock();
  301. preempt_disable();
  302. clocksource_verify_choose_cpus();
  303. if (cpumask_empty(&cpus_chosen)) {
  304. preempt_enable();
  305. cpus_read_unlock();
  306. pr_warn("Not enough CPUs to check clocksource '%s'.\n", cs->name);
  307. return;
  308. }
  309. testcpu = smp_processor_id();
  310. pr_warn("Checking clocksource %s synchronization from CPU %d to CPUs %*pbl.\n", cs->name, testcpu, cpumask_pr_args(&cpus_chosen));
  311. for_each_cpu(cpu, &cpus_chosen) {
  312. if (cpu == testcpu)
  313. continue;
  314. csnow_begin = cs->read(cs);
  315. smp_call_function_single(cpu, clocksource_verify_one_cpu, cs, 1);
  316. csnow_end = cs->read(cs);
  317. delta = (s64)((csnow_mid - csnow_begin) & cs->mask);
  318. if (delta < 0)
  319. cpumask_set_cpu(cpu, &cpus_behind);
  320. delta = (csnow_end - csnow_mid) & cs->mask;
  321. if (delta < 0)
  322. cpumask_set_cpu(cpu, &cpus_ahead);
  323. delta = clocksource_delta(csnow_end, csnow_begin, cs->mask);
  324. cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
  325. if (cs_nsec > cs_nsec_max)
  326. cs_nsec_max = cs_nsec;
  327. if (cs_nsec < cs_nsec_min)
  328. cs_nsec_min = cs_nsec;
  329. }
  330. preempt_enable();
  331. cpus_read_unlock();
  332. if (!cpumask_empty(&cpus_ahead))
  333. pr_warn(" CPUs %*pbl ahead of CPU %d for clocksource %s.\n",
  334. cpumask_pr_args(&cpus_ahead), testcpu, cs->name);
  335. if (!cpumask_empty(&cpus_behind))
  336. pr_warn(" CPUs %*pbl behind CPU %d for clocksource %s.\n",
  337. cpumask_pr_args(&cpus_behind), testcpu, cs->name);
  338. if (!cpumask_empty(&cpus_ahead) || !cpumask_empty(&cpus_behind))
  339. pr_warn(" CPU %d check durations %lldns - %lldns for clocksource %s.\n",
  340. testcpu, cs_nsec_min, cs_nsec_max, cs->name);
  341. }
  342. EXPORT_SYMBOL_GPL(clocksource_verify_percpu);
  343. static inline void clocksource_reset_watchdog(void)
  344. {
  345. struct clocksource *cs;
  346. list_for_each_entry(cs, &watchdog_list, wd_list)
  347. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  348. }
  349. static void clocksource_watchdog(struct timer_list *unused)
  350. {
  351. u64 csnow, wdnow, cslast, wdlast, delta;
  352. int next_cpu, reset_pending;
  353. int64_t wd_nsec, cs_nsec;
  354. struct clocksource *cs;
  355. enum wd_read_status read_ret;
  356. unsigned long extra_wait = 0;
  357. u32 md;
  358. spin_lock(&watchdog_lock);
  359. if (!watchdog_running)
  360. goto out;
  361. reset_pending = atomic_read(&watchdog_reset_pending);
  362. list_for_each_entry(cs, &watchdog_list, wd_list) {
  363. /* Clocksource already marked unstable? */
  364. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  365. if (finished_booting)
  366. schedule_work(&watchdog_work);
  367. continue;
  368. }
  369. read_ret = cs_watchdog_read(cs, &csnow, &wdnow);
  370. if (read_ret == WD_READ_UNSTABLE) {
  371. /* Clock readout unreliable, so give it up. */
  372. __clocksource_unstable(cs);
  373. continue;
  374. }
  375. /*
  376. * When WD_READ_SKIP is returned, it means the system is likely
  377. * under very heavy load, where the latency of reading
  378. * watchdog/clocksource is very big, and affect the accuracy of
  379. * watchdog check. So give system some space and suspend the
  380. * watchdog check for 5 minutes.
  381. */
  382. if (read_ret == WD_READ_SKIP) {
  383. /*
  384. * As the watchdog timer will be suspended, and
  385. * cs->last could keep unchanged for 5 minutes, reset
  386. * the counters.
  387. */
  388. clocksource_reset_watchdog();
  389. extra_wait = HZ * 300;
  390. break;
  391. }
  392. /* Clocksource initialized ? */
  393. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
  394. atomic_read(&watchdog_reset_pending)) {
  395. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  396. cs->wd_last = wdnow;
  397. cs->cs_last = csnow;
  398. continue;
  399. }
  400. delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
  401. wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
  402. watchdog->shift);
  403. delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
  404. cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
  405. wdlast = cs->wd_last; /* save these in case we print them */
  406. cslast = cs->cs_last;
  407. cs->cs_last = csnow;
  408. cs->wd_last = wdnow;
  409. if (atomic_read(&watchdog_reset_pending))
  410. continue;
  411. /* Check the deviation from the watchdog clocksource. */
  412. md = cs->uncertainty_margin + watchdog->uncertainty_margin;
  413. if (abs(cs_nsec - wd_nsec) > md) {
  414. pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
  415. smp_processor_id(), cs->name);
  416. pr_warn(" '%s' wd_nsec: %lld wd_now: %llx wd_last: %llx mask: %llx\n",
  417. watchdog->name, wd_nsec, wdnow, wdlast, watchdog->mask);
  418. pr_warn(" '%s' cs_nsec: %lld cs_now: %llx cs_last: %llx mask: %llx\n",
  419. cs->name, cs_nsec, csnow, cslast, cs->mask);
  420. if (curr_clocksource == cs)
  421. pr_warn(" '%s' is current clocksource.\n", cs->name);
  422. else if (curr_clocksource)
  423. pr_warn(" '%s' (not '%s') is current clocksource.\n", curr_clocksource->name, cs->name);
  424. else
  425. pr_warn(" No current clocksource.\n");
  426. __clocksource_unstable(cs);
  427. continue;
  428. }
  429. if (cs == curr_clocksource && cs->tick_stable)
  430. cs->tick_stable(cs);
  431. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  432. (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  433. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  434. /* Mark it valid for high-res. */
  435. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  436. /*
  437. * clocksource_done_booting() will sort it if
  438. * finished_booting is not set yet.
  439. */
  440. if (!finished_booting)
  441. continue;
  442. /*
  443. * If this is not the current clocksource let
  444. * the watchdog thread reselect it. Due to the
  445. * change to high res this clocksource might
  446. * be preferred now. If it is the current
  447. * clocksource let the tick code know about
  448. * that change.
  449. */
  450. if (cs != curr_clocksource) {
  451. cs->flags |= CLOCK_SOURCE_RESELECT;
  452. schedule_work(&watchdog_work);
  453. } else {
  454. tick_clock_notify();
  455. }
  456. }
  457. }
  458. /*
  459. * We only clear the watchdog_reset_pending, when we did a
  460. * full cycle through all clocksources.
  461. */
  462. if (reset_pending)
  463. atomic_dec(&watchdog_reset_pending);
  464. /*
  465. * Cycle through CPUs to check if the CPUs stay synchronized
  466. * to each other.
  467. */
  468. next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  469. if (next_cpu >= nr_cpu_ids)
  470. next_cpu = cpumask_first(cpu_online_mask);
  471. /*
  472. * Arm timer if not already pending: could race with concurrent
  473. * pair clocksource_stop_watchdog() clocksource_start_watchdog().
  474. */
  475. if (!timer_pending(&watchdog_timer)) {
  476. watchdog_timer.expires += WATCHDOG_INTERVAL + extra_wait;
  477. add_timer_on(&watchdog_timer, next_cpu);
  478. }
  479. out:
  480. spin_unlock(&watchdog_lock);
  481. }
  482. static inline void clocksource_start_watchdog(void)
  483. {
  484. if (watchdog_running || !watchdog || list_empty(&watchdog_list))
  485. return;
  486. timer_setup(&watchdog_timer, clocksource_watchdog, 0);
  487. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  488. add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
  489. watchdog_running = 1;
  490. }
  491. static inline void clocksource_stop_watchdog(void)
  492. {
  493. if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
  494. return;
  495. del_timer(&watchdog_timer);
  496. watchdog_running = 0;
  497. }
  498. static void clocksource_resume_watchdog(void)
  499. {
  500. atomic_inc(&watchdog_reset_pending);
  501. }
  502. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  503. {
  504. INIT_LIST_HEAD(&cs->wd_list);
  505. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  506. /* cs is a clocksource to be watched. */
  507. list_add(&cs->wd_list, &watchdog_list);
  508. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  509. } else {
  510. /* cs is a watchdog. */
  511. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  512. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  513. }
  514. }
  515. static void clocksource_select_watchdog(bool fallback)
  516. {
  517. struct clocksource *cs, *old_wd;
  518. unsigned long flags;
  519. spin_lock_irqsave(&watchdog_lock, flags);
  520. /* save current watchdog */
  521. old_wd = watchdog;
  522. if (fallback)
  523. watchdog = NULL;
  524. list_for_each_entry(cs, &clocksource_list, list) {
  525. /* cs is a clocksource to be watched. */
  526. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
  527. continue;
  528. /* Skip current if we were requested for a fallback. */
  529. if (fallback && cs == old_wd)
  530. continue;
  531. /* Pick the best watchdog. */
  532. if (!watchdog || cs->rating > watchdog->rating)
  533. watchdog = cs;
  534. }
  535. /* If we failed to find a fallback restore the old one. */
  536. if (!watchdog)
  537. watchdog = old_wd;
  538. /* If we changed the watchdog we need to reset cycles. */
  539. if (watchdog != old_wd)
  540. clocksource_reset_watchdog();
  541. /* Check if the watchdog timer needs to be started. */
  542. clocksource_start_watchdog();
  543. spin_unlock_irqrestore(&watchdog_lock, flags);
  544. }
  545. static void clocksource_dequeue_watchdog(struct clocksource *cs)
  546. {
  547. if (cs != watchdog) {
  548. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  549. /* cs is a watched clocksource. */
  550. list_del_init(&cs->wd_list);
  551. /* Check if the watchdog timer needs to be stopped. */
  552. clocksource_stop_watchdog();
  553. }
  554. }
  555. }
  556. static int __clocksource_watchdog_kthread(void)
  557. {
  558. struct clocksource *cs, *tmp;
  559. unsigned long flags;
  560. int select = 0;
  561. /* Do any required per-CPU skew verification. */
  562. if (curr_clocksource &&
  563. curr_clocksource->flags & CLOCK_SOURCE_UNSTABLE &&
  564. curr_clocksource->flags & CLOCK_SOURCE_VERIFY_PERCPU)
  565. clocksource_verify_percpu(curr_clocksource);
  566. spin_lock_irqsave(&watchdog_lock, flags);
  567. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
  568. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  569. list_del_init(&cs->wd_list);
  570. __clocksource_change_rating(cs, 0);
  571. select = 1;
  572. }
  573. if (cs->flags & CLOCK_SOURCE_RESELECT) {
  574. cs->flags &= ~CLOCK_SOURCE_RESELECT;
  575. select = 1;
  576. }
  577. }
  578. /* Check if the watchdog timer needs to be stopped. */
  579. clocksource_stop_watchdog();
  580. spin_unlock_irqrestore(&watchdog_lock, flags);
  581. return select;
  582. }
  583. static int clocksource_watchdog_kthread(void *data)
  584. {
  585. mutex_lock(&clocksource_mutex);
  586. if (__clocksource_watchdog_kthread())
  587. clocksource_select();
  588. mutex_unlock(&clocksource_mutex);
  589. return 0;
  590. }
  591. static bool clocksource_is_watchdog(struct clocksource *cs)
  592. {
  593. return cs == watchdog;
  594. }
  595. #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
  596. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  597. {
  598. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  599. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  600. }
  601. static void clocksource_select_watchdog(bool fallback) { }
  602. static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
  603. static inline void clocksource_resume_watchdog(void) { }
  604. static inline int __clocksource_watchdog_kthread(void) { return 0; }
  605. static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
  606. void clocksource_mark_unstable(struct clocksource *cs) { }
  607. static inline void clocksource_watchdog_lock(unsigned long *flags) { }
  608. static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
  609. #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
  610. static bool clocksource_is_suspend(struct clocksource *cs)
  611. {
  612. return cs == suspend_clocksource;
  613. }
  614. static void __clocksource_suspend_select(struct clocksource *cs)
  615. {
  616. /*
  617. * Skip the clocksource which will be stopped in suspend state.
  618. */
  619. if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP))
  620. return;
  621. /*
  622. * The nonstop clocksource can be selected as the suspend clocksource to
  623. * calculate the suspend time, so it should not supply suspend/resume
  624. * interfaces to suspend the nonstop clocksource when system suspends.
  625. */
  626. if (cs->suspend || cs->resume) {
  627. pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n",
  628. cs->name);
  629. }
  630. /* Pick the best rating. */
  631. if (!suspend_clocksource || cs->rating > suspend_clocksource->rating)
  632. suspend_clocksource = cs;
  633. }
  634. /**
  635. * clocksource_suspend_select - Select the best clocksource for suspend timing
  636. * @fallback: if select a fallback clocksource
  637. */
  638. static void clocksource_suspend_select(bool fallback)
  639. {
  640. struct clocksource *cs, *old_suspend;
  641. old_suspend = suspend_clocksource;
  642. if (fallback)
  643. suspend_clocksource = NULL;
  644. list_for_each_entry(cs, &clocksource_list, list) {
  645. /* Skip current if we were requested for a fallback. */
  646. if (fallback && cs == old_suspend)
  647. continue;
  648. __clocksource_suspend_select(cs);
  649. }
  650. }
  651. /**
  652. * clocksource_start_suspend_timing - Start measuring the suspend timing
  653. * @cs: current clocksource from timekeeping
  654. * @start_cycles: current cycles from timekeeping
  655. *
  656. * This function will save the start cycle values of suspend timer to calculate
  657. * the suspend time when resuming system.
  658. *
  659. * This function is called late in the suspend process from timekeeping_suspend(),
  660. * that means processes are frozen, non-boot cpus and interrupts are disabled
  661. * now. It is therefore possible to start the suspend timer without taking the
  662. * clocksource mutex.
  663. */
  664. void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
  665. {
  666. if (!suspend_clocksource)
  667. return;
  668. /*
  669. * If current clocksource is the suspend timer, we should use the
  670. * tkr_mono.cycle_last value as suspend_start to avoid same reading
  671. * from suspend timer.
  672. */
  673. if (clocksource_is_suspend(cs)) {
  674. suspend_start = start_cycles;
  675. return;
  676. }
  677. if (suspend_clocksource->enable &&
  678. suspend_clocksource->enable(suspend_clocksource)) {
  679. pr_warn_once("Failed to enable the non-suspend-able clocksource.\n");
  680. return;
  681. }
  682. suspend_start = suspend_clocksource->read(suspend_clocksource);
  683. }
  684. /**
  685. * clocksource_stop_suspend_timing - Stop measuring the suspend timing
  686. * @cs: current clocksource from timekeeping
  687. * @cycle_now: current cycles from timekeeping
  688. *
  689. * This function will calculate the suspend time from suspend timer.
  690. *
  691. * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource.
  692. *
  693. * This function is called early in the resume process from timekeeping_resume(),
  694. * that means there is only one cpu, no processes are running and the interrupts
  695. * are disabled. It is therefore possible to stop the suspend timer without
  696. * taking the clocksource mutex.
  697. */
  698. u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
  699. {
  700. u64 now, delta, nsec = 0;
  701. if (!suspend_clocksource)
  702. return 0;
  703. /*
  704. * If current clocksource is the suspend timer, we should use the
  705. * tkr_mono.cycle_last value from timekeeping as current cycle to
  706. * avoid same reading from suspend timer.
  707. */
  708. if (clocksource_is_suspend(cs))
  709. now = cycle_now;
  710. else
  711. now = suspend_clocksource->read(suspend_clocksource);
  712. if (now > suspend_start) {
  713. delta = clocksource_delta(now, suspend_start,
  714. suspend_clocksource->mask);
  715. nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
  716. suspend_clocksource->shift);
  717. }
  718. /*
  719. * Disable the suspend timer to save power if current clocksource is
  720. * not the suspend timer.
  721. */
  722. if (!clocksource_is_suspend(cs) && suspend_clocksource->disable)
  723. suspend_clocksource->disable(suspend_clocksource);
  724. return nsec;
  725. }
  726. /**
  727. * clocksource_suspend - suspend the clocksource(s)
  728. */
  729. void clocksource_suspend(void)
  730. {
  731. struct clocksource *cs;
  732. list_for_each_entry_reverse(cs, &clocksource_list, list)
  733. if (cs->suspend)
  734. cs->suspend(cs);
  735. }
  736. /**
  737. * clocksource_resume - resume the clocksource(s)
  738. */
  739. void clocksource_resume(void)
  740. {
  741. struct clocksource *cs;
  742. list_for_each_entry(cs, &clocksource_list, list)
  743. if (cs->resume)
  744. cs->resume(cs);
  745. clocksource_resume_watchdog();
  746. }
  747. /**
  748. * clocksource_touch_watchdog - Update watchdog
  749. *
  750. * Update the watchdog after exception contexts such as kgdb so as not
  751. * to incorrectly trip the watchdog. This might fail when the kernel
  752. * was stopped in code which holds watchdog_lock.
  753. */
  754. void clocksource_touch_watchdog(void)
  755. {
  756. clocksource_resume_watchdog();
  757. }
  758. /**
  759. * clocksource_max_adjustment- Returns max adjustment amount
  760. * @cs: Pointer to clocksource
  761. *
  762. */
  763. static u32 clocksource_max_adjustment(struct clocksource *cs)
  764. {
  765. u64 ret;
  766. /*
  767. * We won't try to correct for more than 11% adjustments (110,000 ppm),
  768. */
  769. ret = (u64)cs->mult * 11;
  770. do_div(ret,100);
  771. return (u32)ret;
  772. }
  773. /**
  774. * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
  775. * @mult: cycle to nanosecond multiplier
  776. * @shift: cycle to nanosecond divisor (power of two)
  777. * @maxadj: maximum adjustment value to mult (~11%)
  778. * @mask: bitmask for two's complement subtraction of non 64 bit counters
  779. * @max_cyc: maximum cycle value before potential overflow (does not include
  780. * any safety margin)
  781. *
  782. * NOTE: This function includes a safety margin of 50%, in other words, we
  783. * return half the number of nanoseconds the hardware counter can technically
  784. * cover. This is done so that we can potentially detect problems caused by
  785. * delayed timers or bad hardware, which might result in time intervals that
  786. * are larger than what the math used can handle without overflows.
  787. */
  788. u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
  789. {
  790. u64 max_nsecs, max_cycles;
  791. /*
  792. * Calculate the maximum number of cycles that we can pass to the
  793. * cyc2ns() function without overflowing a 64-bit result.
  794. */
  795. max_cycles = ULLONG_MAX;
  796. do_div(max_cycles, mult+maxadj);
  797. /*
  798. * The actual maximum number of cycles we can defer the clocksource is
  799. * determined by the minimum of max_cycles and mask.
  800. * Note: Here we subtract the maxadj to make sure we don't sleep for
  801. * too long if there's a large negative adjustment.
  802. */
  803. max_cycles = min(max_cycles, mask);
  804. max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
  805. /* return the max_cycles value as well if requested */
  806. if (max_cyc)
  807. *max_cyc = max_cycles;
  808. /* Return 50% of the actual maximum, so we can detect bad values */
  809. max_nsecs >>= 1;
  810. return max_nsecs;
  811. }
  812. /**
  813. * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
  814. * @cs: Pointer to clocksource to be updated
  815. *
  816. */
  817. static inline void clocksource_update_max_deferment(struct clocksource *cs)
  818. {
  819. cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
  820. cs->maxadj, cs->mask,
  821. &cs->max_cycles);
  822. }
  823. static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
  824. {
  825. struct clocksource *cs;
  826. if (!finished_booting || list_empty(&clocksource_list))
  827. return NULL;
  828. /*
  829. * We pick the clocksource with the highest rating. If oneshot
  830. * mode is active, we pick the highres valid clocksource with
  831. * the best rating.
  832. */
  833. list_for_each_entry(cs, &clocksource_list, list) {
  834. if (skipcur && cs == curr_clocksource)
  835. continue;
  836. if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  837. continue;
  838. return cs;
  839. }
  840. return NULL;
  841. }
  842. static void __clocksource_select(bool skipcur)
  843. {
  844. bool oneshot = tick_oneshot_mode_active();
  845. struct clocksource *best, *cs;
  846. /* Find the best suitable clocksource */
  847. best = clocksource_find_best(oneshot, skipcur);
  848. if (!best)
  849. return;
  850. if (!strlen(override_name))
  851. goto found;
  852. /* Check for the override clocksource. */
  853. list_for_each_entry(cs, &clocksource_list, list) {
  854. if (skipcur && cs == curr_clocksource)
  855. continue;
  856. if (strcmp(cs->name, override_name) != 0)
  857. continue;
  858. /*
  859. * Check to make sure we don't switch to a non-highres
  860. * capable clocksource if the tick code is in oneshot
  861. * mode (highres or nohz)
  862. */
  863. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
  864. /* Override clocksource cannot be used. */
  865. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  866. pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
  867. cs->name);
  868. override_name[0] = 0;
  869. } else {
  870. /*
  871. * The override cannot be currently verified.
  872. * Deferring to let the watchdog check.
  873. */
  874. pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
  875. cs->name);
  876. }
  877. } else
  878. /* Override clocksource can be used. */
  879. best = cs;
  880. break;
  881. }
  882. found:
  883. if (curr_clocksource != best && !timekeeping_notify(best)) {
  884. pr_info("Switched to clocksource %s\n", best->name);
  885. curr_clocksource = best;
  886. }
  887. }
  888. /**
  889. * clocksource_select - Select the best clocksource available
  890. *
  891. * Private function. Must hold clocksource_mutex when called.
  892. *
  893. * Select the clocksource with the best rating, or the clocksource,
  894. * which is selected by userspace override.
  895. */
  896. static void clocksource_select(void)
  897. {
  898. __clocksource_select(false);
  899. }
  900. static void clocksource_select_fallback(void)
  901. {
  902. __clocksource_select(true);
  903. }
  904. /*
  905. * clocksource_done_booting - Called near the end of core bootup
  906. *
  907. * Hack to avoid lots of clocksource churn at boot time.
  908. * We use fs_initcall because we want this to start before
  909. * device_initcall but after subsys_initcall.
  910. */
  911. static int __init clocksource_done_booting(void)
  912. {
  913. mutex_lock(&clocksource_mutex);
  914. curr_clocksource = clocksource_default_clock();
  915. finished_booting = 1;
  916. /*
  917. * Run the watchdog first to eliminate unstable clock sources
  918. */
  919. __clocksource_watchdog_kthread();
  920. clocksource_select();
  921. mutex_unlock(&clocksource_mutex);
  922. return 0;
  923. }
  924. fs_initcall(clocksource_done_booting);
  925. /*
  926. * Enqueue the clocksource sorted by rating
  927. */
  928. static void clocksource_enqueue(struct clocksource *cs)
  929. {
  930. struct list_head *entry = &clocksource_list;
  931. struct clocksource *tmp;
  932. list_for_each_entry(tmp, &clocksource_list, list) {
  933. /* Keep track of the place, where to insert */
  934. if (tmp->rating < cs->rating)
  935. break;
  936. entry = &tmp->list;
  937. }
  938. list_add(&cs->list, entry);
  939. }
  940. /**
  941. * __clocksource_update_freq_scale - Used update clocksource with new freq
  942. * @cs: clocksource to be registered
  943. * @scale: Scale factor multiplied against freq to get clocksource hz
  944. * @freq: clocksource frequency (cycles per second) divided by scale
  945. *
  946. * This should only be called from the clocksource->enable() method.
  947. *
  948. * This *SHOULD NOT* be called directly! Please use the
  949. * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
  950. * functions.
  951. */
  952. void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
  953. {
  954. u64 sec;
  955. /*
  956. * Default clocksources are *special* and self-define their mult/shift.
  957. * But, you're not special, so you should specify a freq value.
  958. */
  959. if (freq) {
  960. /*
  961. * Calc the maximum number of seconds which we can run before
  962. * wrapping around. For clocksources which have a mask > 32-bit
  963. * we need to limit the max sleep time to have a good
  964. * conversion precision. 10 minutes is still a reasonable
  965. * amount. That results in a shift value of 24 for a
  966. * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
  967. * ~ 0.06ppm granularity for NTP.
  968. */
  969. sec = cs->mask;
  970. do_div(sec, freq);
  971. do_div(sec, scale);
  972. if (!sec)
  973. sec = 1;
  974. else if (sec > 600 && cs->mask > UINT_MAX)
  975. sec = 600;
  976. clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
  977. NSEC_PER_SEC / scale, sec * scale);
  978. }
  979. /*
  980. * If the uncertainty margin is not specified, calculate it.
  981. * If both scale and freq are non-zero, calculate the clock
  982. * period, but bound below at 2*WATCHDOG_MAX_SKEW. However,
  983. * if either of scale or freq is zero, be very conservative and
  984. * take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the
  985. * uncertainty margin. Allow stupidly small uncertainty margins
  986. * to be specified by the caller for testing purposes, but warn
  987. * to discourage production use of this capability.
  988. */
  989. if (scale && freq && !cs->uncertainty_margin) {
  990. cs->uncertainty_margin = NSEC_PER_SEC / (scale * freq);
  991. if (cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW)
  992. cs->uncertainty_margin = 2 * WATCHDOG_MAX_SKEW;
  993. } else if (!cs->uncertainty_margin) {
  994. cs->uncertainty_margin = WATCHDOG_THRESHOLD;
  995. }
  996. WARN_ON_ONCE(cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW);
  997. /*
  998. * Ensure clocksources that have large 'mult' values don't overflow
  999. * when adjusted.
  1000. */
  1001. cs->maxadj = clocksource_max_adjustment(cs);
  1002. while (freq && ((cs->mult + cs->maxadj < cs->mult)
  1003. || (cs->mult - cs->maxadj > cs->mult))) {
  1004. cs->mult >>= 1;
  1005. cs->shift--;
  1006. cs->maxadj = clocksource_max_adjustment(cs);
  1007. }
  1008. /*
  1009. * Only warn for *special* clocksources that self-define
  1010. * their mult/shift values and don't specify a freq.
  1011. */
  1012. WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
  1013. "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
  1014. cs->name);
  1015. clocksource_update_max_deferment(cs);
  1016. pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
  1017. cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
  1018. }
  1019. EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
  1020. /**
  1021. * __clocksource_register_scale - Used to install new clocksources
  1022. * @cs: clocksource to be registered
  1023. * @scale: Scale factor multiplied against freq to get clocksource hz
  1024. * @freq: clocksource frequency (cycles per second) divided by scale
  1025. *
  1026. * Returns -EBUSY if registration fails, zero otherwise.
  1027. *
  1028. * This *SHOULD NOT* be called directly! Please use the
  1029. * clocksource_register_hz() or clocksource_register_khz helper functions.
  1030. */
  1031. int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
  1032. {
  1033. unsigned long flags;
  1034. clocksource_arch_init(cs);
  1035. if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX))
  1036. cs->id = CSID_GENERIC;
  1037. if (cs->vdso_clock_mode < 0 ||
  1038. cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) {
  1039. pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n",
  1040. cs->name, cs->vdso_clock_mode);
  1041. cs->vdso_clock_mode = VDSO_CLOCKMODE_NONE;
  1042. }
  1043. /* Initialize mult/shift and max_idle_ns */
  1044. __clocksource_update_freq_scale(cs, scale, freq);
  1045. /* Add clocksource to the clocksource list */
  1046. mutex_lock(&clocksource_mutex);
  1047. clocksource_watchdog_lock(&flags);
  1048. clocksource_enqueue(cs);
  1049. clocksource_enqueue_watchdog(cs);
  1050. clocksource_watchdog_unlock(&flags);
  1051. clocksource_select();
  1052. clocksource_select_watchdog(false);
  1053. __clocksource_suspend_select(cs);
  1054. mutex_unlock(&clocksource_mutex);
  1055. return 0;
  1056. }
  1057. EXPORT_SYMBOL_GPL(__clocksource_register_scale);
  1058. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  1059. {
  1060. list_del(&cs->list);
  1061. cs->rating = rating;
  1062. clocksource_enqueue(cs);
  1063. }
  1064. /**
  1065. * clocksource_change_rating - Change the rating of a registered clocksource
  1066. * @cs: clocksource to be changed
  1067. * @rating: new rating
  1068. */
  1069. void clocksource_change_rating(struct clocksource *cs, int rating)
  1070. {
  1071. unsigned long flags;
  1072. mutex_lock(&clocksource_mutex);
  1073. clocksource_watchdog_lock(&flags);
  1074. __clocksource_change_rating(cs, rating);
  1075. clocksource_watchdog_unlock(&flags);
  1076. clocksource_select();
  1077. clocksource_select_watchdog(false);
  1078. clocksource_suspend_select(false);
  1079. mutex_unlock(&clocksource_mutex);
  1080. }
  1081. EXPORT_SYMBOL(clocksource_change_rating);
  1082. /*
  1083. * Unbind clocksource @cs. Called with clocksource_mutex held
  1084. */
  1085. static int clocksource_unbind(struct clocksource *cs)
  1086. {
  1087. unsigned long flags;
  1088. if (clocksource_is_watchdog(cs)) {
  1089. /* Select and try to install a replacement watchdog. */
  1090. clocksource_select_watchdog(true);
  1091. if (clocksource_is_watchdog(cs))
  1092. return -EBUSY;
  1093. }
  1094. if (cs == curr_clocksource) {
  1095. /* Select and try to install a replacement clock source */
  1096. clocksource_select_fallback();
  1097. if (curr_clocksource == cs)
  1098. return -EBUSY;
  1099. }
  1100. if (clocksource_is_suspend(cs)) {
  1101. /*
  1102. * Select and try to install a replacement suspend clocksource.
  1103. * If no replacement suspend clocksource, we will just let the
  1104. * clocksource go and have no suspend clocksource.
  1105. */
  1106. clocksource_suspend_select(true);
  1107. }
  1108. clocksource_watchdog_lock(&flags);
  1109. clocksource_dequeue_watchdog(cs);
  1110. list_del_init(&cs->list);
  1111. clocksource_watchdog_unlock(&flags);
  1112. return 0;
  1113. }
  1114. /**
  1115. * clocksource_unregister - remove a registered clocksource
  1116. * @cs: clocksource to be unregistered
  1117. */
  1118. int clocksource_unregister(struct clocksource *cs)
  1119. {
  1120. int ret = 0;
  1121. mutex_lock(&clocksource_mutex);
  1122. if (!list_empty(&cs->list))
  1123. ret = clocksource_unbind(cs);
  1124. mutex_unlock(&clocksource_mutex);
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL(clocksource_unregister);
  1128. #ifdef CONFIG_SYSFS
  1129. /**
  1130. * current_clocksource_show - sysfs interface for current clocksource
  1131. * @dev: unused
  1132. * @attr: unused
  1133. * @buf: char buffer to be filled with clocksource list
  1134. *
  1135. * Provides sysfs interface for listing current clocksource.
  1136. */
  1137. static ssize_t current_clocksource_show(struct device *dev,
  1138. struct device_attribute *attr,
  1139. char *buf)
  1140. {
  1141. ssize_t count = 0;
  1142. mutex_lock(&clocksource_mutex);
  1143. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  1144. mutex_unlock(&clocksource_mutex);
  1145. return count;
  1146. }
  1147. ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
  1148. {
  1149. size_t ret = cnt;
  1150. /* strings from sysfs write are not 0 terminated! */
  1151. if (!cnt || cnt >= CS_NAME_LEN)
  1152. return -EINVAL;
  1153. /* strip of \n: */
  1154. if (buf[cnt-1] == '\n')
  1155. cnt--;
  1156. if (cnt > 0)
  1157. memcpy(dst, buf, cnt);
  1158. dst[cnt] = 0;
  1159. return ret;
  1160. }
  1161. /**
  1162. * current_clocksource_store - interface for manually overriding clocksource
  1163. * @dev: unused
  1164. * @attr: unused
  1165. * @buf: name of override clocksource
  1166. * @count: length of buffer
  1167. *
  1168. * Takes input from sysfs interface for manually overriding the default
  1169. * clocksource selection.
  1170. */
  1171. static ssize_t current_clocksource_store(struct device *dev,
  1172. struct device_attribute *attr,
  1173. const char *buf, size_t count)
  1174. {
  1175. ssize_t ret;
  1176. mutex_lock(&clocksource_mutex);
  1177. ret = sysfs_get_uname(buf, override_name, count);
  1178. if (ret >= 0)
  1179. clocksource_select();
  1180. mutex_unlock(&clocksource_mutex);
  1181. return ret;
  1182. }
  1183. static DEVICE_ATTR_RW(current_clocksource);
  1184. /**
  1185. * unbind_clocksource_store - interface for manually unbinding clocksource
  1186. * @dev: unused
  1187. * @attr: unused
  1188. * @buf: unused
  1189. * @count: length of buffer
  1190. *
  1191. * Takes input from sysfs interface for manually unbinding a clocksource.
  1192. */
  1193. static ssize_t unbind_clocksource_store(struct device *dev,
  1194. struct device_attribute *attr,
  1195. const char *buf, size_t count)
  1196. {
  1197. struct clocksource *cs;
  1198. char name[CS_NAME_LEN];
  1199. ssize_t ret;
  1200. ret = sysfs_get_uname(buf, name, count);
  1201. if (ret < 0)
  1202. return ret;
  1203. ret = -ENODEV;
  1204. mutex_lock(&clocksource_mutex);
  1205. list_for_each_entry(cs, &clocksource_list, list) {
  1206. if (strcmp(cs->name, name))
  1207. continue;
  1208. ret = clocksource_unbind(cs);
  1209. break;
  1210. }
  1211. mutex_unlock(&clocksource_mutex);
  1212. return ret ? ret : count;
  1213. }
  1214. static DEVICE_ATTR_WO(unbind_clocksource);
  1215. /**
  1216. * available_clocksource_show - sysfs interface for listing clocksource
  1217. * @dev: unused
  1218. * @attr: unused
  1219. * @buf: char buffer to be filled with clocksource list
  1220. *
  1221. * Provides sysfs interface for listing registered clocksources
  1222. */
  1223. static ssize_t available_clocksource_show(struct device *dev,
  1224. struct device_attribute *attr,
  1225. char *buf)
  1226. {
  1227. struct clocksource *src;
  1228. ssize_t count = 0;
  1229. mutex_lock(&clocksource_mutex);
  1230. list_for_each_entry(src, &clocksource_list, list) {
  1231. /*
  1232. * Don't show non-HRES clocksource if the tick code is
  1233. * in one shot mode (highres=on or nohz=on)
  1234. */
  1235. if (!tick_oneshot_mode_active() ||
  1236. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  1237. count += snprintf(buf + count,
  1238. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  1239. "%s ", src->name);
  1240. }
  1241. mutex_unlock(&clocksource_mutex);
  1242. count += snprintf(buf + count,
  1243. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  1244. return count;
  1245. }
  1246. static DEVICE_ATTR_RO(available_clocksource);
  1247. static struct attribute *clocksource_attrs[] = {
  1248. &dev_attr_current_clocksource.attr,
  1249. &dev_attr_unbind_clocksource.attr,
  1250. &dev_attr_available_clocksource.attr,
  1251. NULL
  1252. };
  1253. ATTRIBUTE_GROUPS(clocksource);
  1254. static struct bus_type clocksource_subsys = {
  1255. .name = "clocksource",
  1256. .dev_name = "clocksource",
  1257. };
  1258. static struct device device_clocksource = {
  1259. .id = 0,
  1260. .bus = &clocksource_subsys,
  1261. .groups = clocksource_groups,
  1262. };
  1263. static int __init init_clocksource_sysfs(void)
  1264. {
  1265. int error = subsys_system_register(&clocksource_subsys, NULL);
  1266. if (!error)
  1267. error = device_register(&device_clocksource);
  1268. return error;
  1269. }
  1270. device_initcall(init_clocksource_sysfs);
  1271. #endif /* CONFIG_SYSFS */
  1272. /**
  1273. * boot_override_clocksource - boot clock override
  1274. * @str: override name
  1275. *
  1276. * Takes a clocksource= boot argument and uses it
  1277. * as the clocksource override name.
  1278. */
  1279. static int __init boot_override_clocksource(char* str)
  1280. {
  1281. mutex_lock(&clocksource_mutex);
  1282. if (str)
  1283. strlcpy(override_name, str, sizeof(override_name));
  1284. mutex_unlock(&clocksource_mutex);
  1285. return 1;
  1286. }
  1287. __setup("clocksource=", boot_override_clocksource);
  1288. /**
  1289. * boot_override_clock - Compatibility layer for deprecated boot option
  1290. * @str: override name
  1291. *
  1292. * DEPRECATED! Takes a clock= boot argument and uses it
  1293. * as the clocksource override name
  1294. */
  1295. static int __init boot_override_clock(char* str)
  1296. {
  1297. if (!strcmp(str, "pmtmr")) {
  1298. pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
  1299. return boot_override_clocksource("acpi_pm");
  1300. }
  1301. pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
  1302. return boot_override_clocksource(str);
  1303. }
  1304. __setup("clock=", boot_override_clock);