clocksource.c 16 KB

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