clocksource.c 16 KB

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