clocksource.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * linux/kernel/time/clocksource.c
  3. *
  4. * This file contains the functions which manage clocksource drivers.
  5. *
  6. * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * TODO WishList:
  23. * o Allow clocksource drivers to be unregistered
  24. */
  25. #include <linux/clocksource.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
  30. #include <linux/tick.h>
  31. void timecounter_init(struct timecounter *tc,
  32. const struct cyclecounter *cc,
  33. u64 start_tstamp)
  34. {
  35. tc->cc = cc;
  36. tc->cycle_last = cc->read(cc);
  37. tc->nsec = start_tstamp;
  38. }
  39. EXPORT_SYMBOL(timecounter_init);
  40. /**
  41. * timecounter_read_delta - get nanoseconds since last call of this function
  42. * @tc: Pointer to time counter
  43. *
  44. * When the underlying cycle counter runs over, this will be handled
  45. * correctly as long as it does not run over more than once between
  46. * calls.
  47. *
  48. * The first call to this function for a new time counter initializes
  49. * the time tracking and returns an undefined result.
  50. */
  51. static u64 timecounter_read_delta(struct timecounter *tc)
  52. {
  53. cycle_t cycle_now, cycle_delta;
  54. u64 ns_offset;
  55. /* read cycle counter: */
  56. cycle_now = tc->cc->read(tc->cc);
  57. /* calculate the delta since the last timecounter_read_delta(): */
  58. cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
  59. /* convert to nanoseconds: */
  60. ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
  61. /* update time stamp of timecounter_read_delta() call: */
  62. tc->cycle_last = cycle_now;
  63. return ns_offset;
  64. }
  65. u64 timecounter_read(struct timecounter *tc)
  66. {
  67. u64 nsec;
  68. /* increment time by nanoseconds since last call */
  69. nsec = timecounter_read_delta(tc);
  70. nsec += tc->nsec;
  71. tc->nsec = nsec;
  72. return nsec;
  73. }
  74. EXPORT_SYMBOL(timecounter_read);
  75. u64 timecounter_cyc2time(struct timecounter *tc,
  76. cycle_t cycle_tstamp)
  77. {
  78. u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
  79. u64 nsec;
  80. /*
  81. * Instead of always treating cycle_tstamp as more recent
  82. * than tc->cycle_last, detect when it is too far in the
  83. * future and treat it as old time stamp instead.
  84. */
  85. if (cycle_delta > tc->cc->mask / 2) {
  86. cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
  87. nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
  88. } else {
  89. nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
  90. }
  91. return nsec;
  92. }
  93. EXPORT_SYMBOL(timecounter_cyc2time);
  94. /*[Clocksource internal variables]---------
  95. * curr_clocksource:
  96. * currently selected clocksource.
  97. * next_clocksource:
  98. * pending next selected clocksource.
  99. * clocksource_list:
  100. * linked list with the registered clocksources
  101. * clocksource_lock:
  102. * protects manipulations to curr_clocksource and next_clocksource
  103. * and the clocksource_list
  104. * override_name:
  105. * Name of the user-specified clocksource.
  106. */
  107. static struct clocksource *curr_clocksource;
  108. static struct clocksource *next_clocksource;
  109. static LIST_HEAD(clocksource_list);
  110. static DEFINE_SPINLOCK(clocksource_lock);
  111. static char override_name[32];
  112. static int finished_booting;
  113. /* clocksource_done_booting - Called near the end of core bootup
  114. *
  115. * Hack to avoid lots of clocksource churn at boot time.
  116. * We use fs_initcall because we want this to start before
  117. * device_initcall but after subsys_initcall.
  118. */
  119. static int __init clocksource_done_booting(void)
  120. {
  121. finished_booting = 1;
  122. return 0;
  123. }
  124. fs_initcall(clocksource_done_booting);
  125. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  126. static LIST_HEAD(watchdog_list);
  127. static struct clocksource *watchdog;
  128. static struct timer_list watchdog_timer;
  129. static DEFINE_SPINLOCK(watchdog_lock);
  130. static cycle_t watchdog_last;
  131. static unsigned long watchdog_resumed;
  132. /*
  133. * Interval: 0.5sec Threshold: 0.0625s
  134. */
  135. #define WATCHDOG_INTERVAL (HZ >> 1)
  136. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
  137. static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
  138. {
  139. if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
  140. return;
  141. printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
  142. cs->name, delta);
  143. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  144. clocksource_change_rating(cs, 0);
  145. list_del(&cs->wd_list);
  146. }
  147. static void clocksource_watchdog(unsigned long data)
  148. {
  149. struct clocksource *cs, *tmp;
  150. cycle_t csnow, wdnow;
  151. int64_t wd_nsec, cs_nsec;
  152. int resumed;
  153. spin_lock(&watchdog_lock);
  154. resumed = test_and_clear_bit(0, &watchdog_resumed);
  155. wdnow = watchdog->read(watchdog);
  156. wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
  157. watchdog_last = wdnow;
  158. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
  159. csnow = cs->read(cs);
  160. if (unlikely(resumed)) {
  161. cs->wd_last = csnow;
  162. continue;
  163. }
  164. /* Initialized ? */
  165. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
  166. if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  167. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  168. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  169. /*
  170. * We just marked the clocksource as
  171. * highres-capable, notify the rest of the
  172. * system as well so that we transition
  173. * into high-res mode:
  174. */
  175. tick_clock_notify();
  176. }
  177. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  178. cs->wd_last = csnow;
  179. } else {
  180. cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
  181. cs->wd_last = csnow;
  182. /* Check the delta. Might remove from the list ! */
  183. clocksource_ratewd(cs, cs_nsec - wd_nsec);
  184. }
  185. }
  186. if (!list_empty(&watchdog_list)) {
  187. /*
  188. * Cycle through CPUs to check if the CPUs stay
  189. * synchronized to each other.
  190. */
  191. int next_cpu = cpumask_next(raw_smp_processor_id(),
  192. cpu_online_mask);
  193. if (next_cpu >= nr_cpu_ids)
  194. next_cpu = cpumask_first(cpu_online_mask);
  195. watchdog_timer.expires += WATCHDOG_INTERVAL;
  196. add_timer_on(&watchdog_timer, next_cpu);
  197. }
  198. spin_unlock(&watchdog_lock);
  199. }
  200. static void clocksource_resume_watchdog(void)
  201. {
  202. set_bit(0, &watchdog_resumed);
  203. }
  204. static void clocksource_check_watchdog(struct clocksource *cs)
  205. {
  206. struct clocksource *cse;
  207. unsigned long flags;
  208. spin_lock_irqsave(&watchdog_lock, flags);
  209. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  210. int started = !list_empty(&watchdog_list);
  211. list_add(&cs->wd_list, &watchdog_list);
  212. if (!started && watchdog) {
  213. watchdog_last = watchdog->read(watchdog);
  214. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  215. add_timer_on(&watchdog_timer,
  216. cpumask_first(cpu_online_mask));
  217. }
  218. } else {
  219. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  220. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  221. if (!watchdog || cs->rating > watchdog->rating) {
  222. if (watchdog)
  223. del_timer(&watchdog_timer);
  224. watchdog = cs;
  225. init_timer(&watchdog_timer);
  226. watchdog_timer.function = clocksource_watchdog;
  227. /* Reset watchdog cycles */
  228. list_for_each_entry(cse, &watchdog_list, wd_list)
  229. cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
  230. /* Start if list is not empty */
  231. if (!list_empty(&watchdog_list)) {
  232. watchdog_last = watchdog->read(watchdog);
  233. watchdog_timer.expires =
  234. jiffies + WATCHDOG_INTERVAL;
  235. add_timer_on(&watchdog_timer,
  236. cpumask_first(cpu_online_mask));
  237. }
  238. }
  239. }
  240. spin_unlock_irqrestore(&watchdog_lock, flags);
  241. }
  242. #else
  243. static void clocksource_check_watchdog(struct clocksource *cs)
  244. {
  245. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  246. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  247. }
  248. static inline void clocksource_resume_watchdog(void) { }
  249. #endif
  250. /**
  251. * clocksource_resume - resume the clocksource(s)
  252. */
  253. void clocksource_resume(void)
  254. {
  255. struct clocksource *cs;
  256. unsigned long flags;
  257. spin_lock_irqsave(&clocksource_lock, flags);
  258. list_for_each_entry(cs, &clocksource_list, list) {
  259. if (cs->resume)
  260. cs->resume();
  261. }
  262. clocksource_resume_watchdog();
  263. spin_unlock_irqrestore(&clocksource_lock, flags);
  264. }
  265. /**
  266. * clocksource_touch_watchdog - Update watchdog
  267. *
  268. * Update the watchdog after exception contexts such as kgdb so as not
  269. * to incorrectly trip the watchdog.
  270. *
  271. */
  272. void clocksource_touch_watchdog(void)
  273. {
  274. clocksource_resume_watchdog();
  275. }
  276. #ifdef CONFIG_GENERIC_TIME
  277. /**
  278. * clocksource_get_next - Returns the selected clocksource
  279. *
  280. */
  281. struct clocksource *clocksource_get_next(void)
  282. {
  283. unsigned long flags;
  284. spin_lock_irqsave(&clocksource_lock, flags);
  285. if (next_clocksource && finished_booting) {
  286. curr_clocksource = next_clocksource;
  287. next_clocksource = NULL;
  288. }
  289. spin_unlock_irqrestore(&clocksource_lock, flags);
  290. return curr_clocksource;
  291. }
  292. /**
  293. * clocksource_select - Select the best clocksource available
  294. *
  295. * Private function. Must hold clocksource_lock when called.
  296. *
  297. * Select the clocksource with the best rating, or the clocksource,
  298. * which is selected by userspace override.
  299. */
  300. static void clocksource_select(void)
  301. {
  302. struct clocksource *best, *cs;
  303. if (list_empty(&clocksource_list))
  304. return;
  305. /* First clocksource on the list has the best rating. */
  306. best = list_first_entry(&clocksource_list, struct clocksource, list);
  307. /* Check for the override clocksource. */
  308. list_for_each_entry(cs, &clocksource_list, list) {
  309. if (strcmp(cs->name, override_name) != 0)
  310. continue;
  311. /*
  312. * Check to make sure we don't switch to a non-highres
  313. * capable clocksource if the tick code is in oneshot
  314. * mode (highres or nohz)
  315. */
  316. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  317. tick_oneshot_mode_active()) {
  318. /* Override clocksource cannot be used. */
  319. printk(KERN_WARNING "Override clocksource %s is not "
  320. "HRT compatible. Cannot switch while in "
  321. "HRT/NOHZ mode\n", cs->name);
  322. override_name[0] = 0;
  323. } else
  324. /* Override clocksource can be used. */
  325. best = cs;
  326. break;
  327. }
  328. if (curr_clocksource != best)
  329. next_clocksource = best;
  330. }
  331. #else /* CONFIG_GENERIC_TIME */
  332. static void clocksource_select(void) { }
  333. #endif
  334. /*
  335. * Enqueue the clocksource sorted by rating
  336. */
  337. static void clocksource_enqueue(struct clocksource *cs)
  338. {
  339. struct list_head *entry = &clocksource_list;
  340. struct clocksource *tmp;
  341. list_for_each_entry(tmp, &clocksource_list, list)
  342. /* Keep track of the place, where to insert */
  343. if (tmp->rating >= cs->rating)
  344. entry = &tmp->list;
  345. list_add(&cs->list, entry);
  346. }
  347. /**
  348. * clocksource_register - Used to install new clocksources
  349. * @t: clocksource to be registered
  350. *
  351. * Returns -EBUSY if registration fails, zero otherwise.
  352. */
  353. int clocksource_register(struct clocksource *cs)
  354. {
  355. unsigned long flags;
  356. spin_lock_irqsave(&clocksource_lock, flags);
  357. clocksource_enqueue(cs);
  358. clocksource_select();
  359. spin_unlock_irqrestore(&clocksource_lock, flags);
  360. clocksource_check_watchdog(cs);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(clocksource_register);
  364. /**
  365. * clocksource_change_rating - Change the rating of a registered clocksource
  366. *
  367. */
  368. void clocksource_change_rating(struct clocksource *cs, int rating)
  369. {
  370. unsigned long flags;
  371. spin_lock_irqsave(&clocksource_lock, flags);
  372. list_del(&cs->list);
  373. cs->rating = rating;
  374. clocksource_enqueue(cs);
  375. clocksource_select();
  376. spin_unlock_irqrestore(&clocksource_lock, flags);
  377. }
  378. /**
  379. * clocksource_unregister - remove a registered clocksource
  380. */
  381. void clocksource_unregister(struct clocksource *cs)
  382. {
  383. unsigned long flags;
  384. spin_lock_irqsave(&clocksource_lock, flags);
  385. list_del(&cs->list);
  386. clocksource_select();
  387. spin_unlock_irqrestore(&clocksource_lock, flags);
  388. }
  389. #ifdef CONFIG_SYSFS
  390. /**
  391. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  392. * @dev: unused
  393. * @buf: char buffer to be filled with clocksource list
  394. *
  395. * Provides sysfs interface for listing current clocksource.
  396. */
  397. static ssize_t
  398. sysfs_show_current_clocksources(struct sys_device *dev,
  399. struct sysdev_attribute *attr, char *buf)
  400. {
  401. ssize_t count = 0;
  402. spin_lock_irq(&clocksource_lock);
  403. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  404. spin_unlock_irq(&clocksource_lock);
  405. return count;
  406. }
  407. /**
  408. * sysfs_override_clocksource - interface for manually overriding clocksource
  409. * @dev: unused
  410. * @buf: name of override clocksource
  411. * @count: length of buffer
  412. *
  413. * Takes input from sysfs interface for manually overriding the default
  414. * clocksource selction.
  415. */
  416. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  417. struct sysdev_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. size_t ret = count;
  421. /* strings from sysfs write are not 0 terminated! */
  422. if (count >= sizeof(override_name))
  423. return -EINVAL;
  424. /* strip of \n: */
  425. if (buf[count-1] == '\n')
  426. count--;
  427. spin_lock_irq(&clocksource_lock);
  428. if (count > 0)
  429. memcpy(override_name, buf, count);
  430. override_name[count] = 0;
  431. clocksource_select();
  432. spin_unlock_irq(&clocksource_lock);
  433. return ret;
  434. }
  435. /**
  436. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  437. * @dev: unused
  438. * @buf: char buffer to be filled with clocksource list
  439. *
  440. * Provides sysfs interface for listing registered clocksources
  441. */
  442. static ssize_t
  443. sysfs_show_available_clocksources(struct sys_device *dev,
  444. struct sysdev_attribute *attr,
  445. char *buf)
  446. {
  447. struct clocksource *src;
  448. ssize_t count = 0;
  449. spin_lock_irq(&clocksource_lock);
  450. list_for_each_entry(src, &clocksource_list, list) {
  451. /*
  452. * Don't show non-HRES clocksource if the tick code is
  453. * in one shot mode (highres=on or nohz=on)
  454. */
  455. if (!tick_oneshot_mode_active() ||
  456. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  457. count += snprintf(buf + count,
  458. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  459. "%s ", src->name);
  460. }
  461. spin_unlock_irq(&clocksource_lock);
  462. count += snprintf(buf + count,
  463. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  464. return count;
  465. }
  466. /*
  467. * Sysfs setup bits:
  468. */
  469. static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  470. sysfs_override_clocksource);
  471. static SYSDEV_ATTR(available_clocksource, 0444,
  472. sysfs_show_available_clocksources, NULL);
  473. static struct sysdev_class clocksource_sysclass = {
  474. .name = "clocksource",
  475. };
  476. static struct sys_device device_clocksource = {
  477. .id = 0,
  478. .cls = &clocksource_sysclass,
  479. };
  480. static int __init init_clocksource_sysfs(void)
  481. {
  482. int error = sysdev_class_register(&clocksource_sysclass);
  483. if (!error)
  484. error = sysdev_register(&device_clocksource);
  485. if (!error)
  486. error = sysdev_create_file(
  487. &device_clocksource,
  488. &attr_current_clocksource);
  489. if (!error)
  490. error = sysdev_create_file(
  491. &device_clocksource,
  492. &attr_available_clocksource);
  493. return error;
  494. }
  495. device_initcall(init_clocksource_sysfs);
  496. #endif /* CONFIG_SYSFS */
  497. /**
  498. * boot_override_clocksource - boot clock override
  499. * @str: override name
  500. *
  501. * Takes a clocksource= boot argument and uses it
  502. * as the clocksource override name.
  503. */
  504. static int __init boot_override_clocksource(char* str)
  505. {
  506. unsigned long flags;
  507. spin_lock_irqsave(&clocksource_lock, flags);
  508. if (str)
  509. strlcpy(override_name, str, sizeof(override_name));
  510. spin_unlock_irqrestore(&clocksource_lock, flags);
  511. return 1;
  512. }
  513. __setup("clocksource=", boot_override_clocksource);
  514. /**
  515. * boot_override_clock - Compatibility layer for deprecated boot option
  516. * @str: override name
  517. *
  518. * DEPRECATED! Takes a clock= boot argument and uses it
  519. * as the clocksource override name
  520. */
  521. static int __init boot_override_clock(char* str)
  522. {
  523. if (!strcmp(str, "pmtmr")) {
  524. printk("Warning: clock=pmtmr is deprecated. "
  525. "Use clocksource=acpi_pm.\n");
  526. return boot_override_clocksource("acpi_pm");
  527. }
  528. printk("Warning! clock= boot option is deprecated. "
  529. "Use clocksource=xyz\n");
  530. return boot_override_clocksource(str);
  531. }
  532. __setup("clock=", boot_override_clock);