clocksource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /* XXX - Would like a better way for initializing curr_clocksource */
  33. extern struct clocksource clocksource_jiffies;
  34. /*[Clocksource internal variables]---------
  35. * curr_clocksource:
  36. * currently selected clocksource. Initialized to clocksource_jiffies.
  37. * next_clocksource:
  38. * pending next selected clocksource.
  39. * clocksource_list:
  40. * linked list with the registered clocksources
  41. * clocksource_lock:
  42. * protects manipulations to curr_clocksource and next_clocksource
  43. * and the clocksource_list
  44. * override_name:
  45. * Name of the user-specified clocksource.
  46. */
  47. static struct clocksource *curr_clocksource = &clocksource_jiffies;
  48. static struct clocksource *next_clocksource;
  49. static struct clocksource *clocksource_override;
  50. static LIST_HEAD(clocksource_list);
  51. static DEFINE_SPINLOCK(clocksource_lock);
  52. static char override_name[32];
  53. static int finished_booting;
  54. /* clocksource_done_booting - Called near the end of core bootup
  55. *
  56. * Hack to avoid lots of clocksource churn at boot time.
  57. * We use fs_initcall because we want this to start before
  58. * device_initcall but after subsys_initcall.
  59. */
  60. static int __init clocksource_done_booting(void)
  61. {
  62. finished_booting = 1;
  63. return 0;
  64. }
  65. fs_initcall(clocksource_done_booting);
  66. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  67. static LIST_HEAD(watchdog_list);
  68. static struct clocksource *watchdog;
  69. static struct timer_list watchdog_timer;
  70. static DEFINE_SPINLOCK(watchdog_lock);
  71. static cycle_t watchdog_last;
  72. /*
  73. * Interval: 0.5sec Treshold: 0.0625s
  74. */
  75. #define WATCHDOG_INTERVAL (HZ >> 1)
  76. #define WATCHDOG_TRESHOLD (NSEC_PER_SEC >> 4)
  77. static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
  78. {
  79. if (delta > -WATCHDOG_TRESHOLD && delta < WATCHDOG_TRESHOLD)
  80. return;
  81. printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
  82. cs->name, delta);
  83. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  84. clocksource_change_rating(cs, 0);
  85. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  86. list_del(&cs->wd_list);
  87. }
  88. static void clocksource_watchdog(unsigned long data)
  89. {
  90. struct clocksource *cs, *tmp;
  91. cycle_t csnow, wdnow;
  92. int64_t wd_nsec, cs_nsec;
  93. spin_lock(&watchdog_lock);
  94. wdnow = watchdog->read();
  95. wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
  96. watchdog_last = wdnow;
  97. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
  98. csnow = cs->read();
  99. /* Initialized ? */
  100. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
  101. if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  102. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  103. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  104. /*
  105. * We just marked the clocksource as
  106. * highres-capable, notify the rest of the
  107. * system as well so that we transition
  108. * into high-res mode:
  109. */
  110. tick_clock_notify();
  111. }
  112. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  113. cs->wd_last = csnow;
  114. } else {
  115. cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
  116. cs->wd_last = csnow;
  117. /* Check the delta. Might remove from the list ! */
  118. clocksource_ratewd(cs, cs_nsec - wd_nsec);
  119. }
  120. }
  121. if (!list_empty(&watchdog_list)) {
  122. __mod_timer(&watchdog_timer,
  123. watchdog_timer.expires + WATCHDOG_INTERVAL);
  124. }
  125. spin_unlock(&watchdog_lock);
  126. }
  127. static void clocksource_check_watchdog(struct clocksource *cs)
  128. {
  129. struct clocksource *cse;
  130. unsigned long flags;
  131. spin_lock_irqsave(&watchdog_lock, flags);
  132. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  133. int started = !list_empty(&watchdog_list);
  134. list_add(&cs->wd_list, &watchdog_list);
  135. if (!started && watchdog) {
  136. watchdog_last = watchdog->read();
  137. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  138. add_timer(&watchdog_timer);
  139. }
  140. } else {
  141. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  142. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  143. if (!watchdog || cs->rating > watchdog->rating) {
  144. if (watchdog)
  145. del_timer(&watchdog_timer);
  146. watchdog = cs;
  147. init_timer(&watchdog_timer);
  148. watchdog_timer.function = clocksource_watchdog;
  149. /* Reset watchdog cycles */
  150. list_for_each_entry(cse, &watchdog_list, wd_list)
  151. cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
  152. /* Start if list is not empty */
  153. if (!list_empty(&watchdog_list)) {
  154. watchdog_last = watchdog->read();
  155. watchdog_timer.expires =
  156. jiffies + WATCHDOG_INTERVAL;
  157. add_timer(&watchdog_timer);
  158. }
  159. }
  160. }
  161. spin_unlock_irqrestore(&watchdog_lock, flags);
  162. }
  163. #else
  164. static void clocksource_check_watchdog(struct clocksource *cs)
  165. {
  166. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  167. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  168. }
  169. #endif
  170. /**
  171. * clocksource_get_next - Returns the selected clocksource
  172. *
  173. */
  174. struct clocksource *clocksource_get_next(void)
  175. {
  176. unsigned long flags;
  177. spin_lock_irqsave(&clocksource_lock, flags);
  178. if (next_clocksource && finished_booting) {
  179. curr_clocksource = next_clocksource;
  180. next_clocksource = NULL;
  181. }
  182. spin_unlock_irqrestore(&clocksource_lock, flags);
  183. return curr_clocksource;
  184. }
  185. /**
  186. * select_clocksource - Selects the best registered clocksource.
  187. *
  188. * Private function. Must hold clocksource_lock when called.
  189. *
  190. * Select the clocksource with the best rating, or the clocksource,
  191. * which is selected by userspace override.
  192. */
  193. static struct clocksource *select_clocksource(void)
  194. {
  195. struct clocksource *next;
  196. if (list_empty(&clocksource_list))
  197. return NULL;
  198. if (clocksource_override)
  199. next = clocksource_override;
  200. else
  201. next = list_entry(clocksource_list.next, struct clocksource,
  202. list);
  203. if (next == curr_clocksource)
  204. return NULL;
  205. return next;
  206. }
  207. /*
  208. * Enqueue the clocksource sorted by rating
  209. */
  210. static int clocksource_enqueue(struct clocksource *c)
  211. {
  212. struct list_head *tmp, *entry = &clocksource_list;
  213. list_for_each(tmp, &clocksource_list) {
  214. struct clocksource *cs;
  215. cs = list_entry(tmp, struct clocksource, list);
  216. if (cs == c)
  217. return -EBUSY;
  218. /* Keep track of the place, where to insert */
  219. if (cs->rating >= c->rating)
  220. entry = tmp;
  221. }
  222. list_add(&c->list, entry);
  223. if (strlen(c->name) == strlen(override_name) &&
  224. !strcmp(c->name, override_name))
  225. clocksource_override = c;
  226. return 0;
  227. }
  228. /**
  229. * clocksource_register - Used to install new clocksources
  230. * @t: clocksource to be registered
  231. *
  232. * Returns -EBUSY if registration fails, zero otherwise.
  233. */
  234. int clocksource_register(struct clocksource *c)
  235. {
  236. unsigned long flags;
  237. int ret;
  238. spin_lock_irqsave(&clocksource_lock, flags);
  239. ret = clocksource_enqueue(c);
  240. if (!ret)
  241. next_clocksource = select_clocksource();
  242. spin_unlock_irqrestore(&clocksource_lock, flags);
  243. if (!ret)
  244. clocksource_check_watchdog(c);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(clocksource_register);
  248. /**
  249. * clocksource_change_rating - Change the rating of a registered clocksource
  250. *
  251. */
  252. void clocksource_change_rating(struct clocksource *cs, int rating)
  253. {
  254. unsigned long flags;
  255. spin_lock_irqsave(&clocksource_lock, flags);
  256. list_del(&cs->list);
  257. cs->rating = rating;
  258. clocksource_enqueue(cs);
  259. next_clocksource = select_clocksource();
  260. spin_unlock_irqrestore(&clocksource_lock, flags);
  261. }
  262. #ifdef CONFIG_SYSFS
  263. /**
  264. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  265. * @dev: unused
  266. * @buf: char buffer to be filled with clocksource list
  267. *
  268. * Provides sysfs interface for listing current clocksource.
  269. */
  270. static ssize_t
  271. sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
  272. {
  273. char *curr = buf;
  274. spin_lock_irq(&clocksource_lock);
  275. curr += sprintf(curr, "%s ", curr_clocksource->name);
  276. spin_unlock_irq(&clocksource_lock);
  277. curr += sprintf(curr, "\n");
  278. return curr - buf;
  279. }
  280. /**
  281. * sysfs_override_clocksource - interface for manually overriding clocksource
  282. * @dev: unused
  283. * @buf: name of override clocksource
  284. * @count: length of buffer
  285. *
  286. * Takes input from sysfs interface for manually overriding the default
  287. * clocksource selction.
  288. */
  289. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  290. const char *buf, size_t count)
  291. {
  292. struct clocksource *ovr = NULL;
  293. struct list_head *tmp;
  294. size_t ret = count;
  295. int len;
  296. /* strings from sysfs write are not 0 terminated! */
  297. if (count >= sizeof(override_name))
  298. return -EINVAL;
  299. /* strip of \n: */
  300. if (buf[count-1] == '\n')
  301. count--;
  302. spin_lock_irq(&clocksource_lock);
  303. if (count > 0)
  304. memcpy(override_name, buf, count);
  305. override_name[count] = 0;
  306. len = strlen(override_name);
  307. if (len) {
  308. ovr = clocksource_override;
  309. /* try to select it: */
  310. list_for_each(tmp, &clocksource_list) {
  311. struct clocksource *cs;
  312. cs = list_entry(tmp, struct clocksource, list);
  313. if (strlen(cs->name) == len &&
  314. !strcmp(cs->name, override_name))
  315. ovr = cs;
  316. }
  317. }
  318. /* Reselect, when the override name has changed */
  319. if (ovr != clocksource_override) {
  320. clocksource_override = ovr;
  321. next_clocksource = select_clocksource();
  322. }
  323. spin_unlock_irq(&clocksource_lock);
  324. return ret;
  325. }
  326. /**
  327. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  328. * @dev: unused
  329. * @buf: char buffer to be filled with clocksource list
  330. *
  331. * Provides sysfs interface for listing registered clocksources
  332. */
  333. static ssize_t
  334. sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
  335. {
  336. struct list_head *tmp;
  337. char *curr = buf;
  338. spin_lock_irq(&clocksource_lock);
  339. list_for_each(tmp, &clocksource_list) {
  340. struct clocksource *src;
  341. src = list_entry(tmp, struct clocksource, list);
  342. curr += sprintf(curr, "%s ", src->name);
  343. }
  344. spin_unlock_irq(&clocksource_lock);
  345. curr += sprintf(curr, "\n");
  346. return curr - buf;
  347. }
  348. /*
  349. * Sysfs setup bits:
  350. */
  351. static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
  352. sysfs_override_clocksource);
  353. static SYSDEV_ATTR(available_clocksource, 0600,
  354. sysfs_show_available_clocksources, NULL);
  355. static struct sysdev_class clocksource_sysclass = {
  356. set_kset_name("clocksource"),
  357. };
  358. static struct sys_device device_clocksource = {
  359. .id = 0,
  360. .cls = &clocksource_sysclass,
  361. };
  362. static int __init init_clocksource_sysfs(void)
  363. {
  364. int error = sysdev_class_register(&clocksource_sysclass);
  365. if (!error)
  366. error = sysdev_register(&device_clocksource);
  367. if (!error)
  368. error = sysdev_create_file(
  369. &device_clocksource,
  370. &attr_current_clocksource);
  371. if (!error)
  372. error = sysdev_create_file(
  373. &device_clocksource,
  374. &attr_available_clocksource);
  375. return error;
  376. }
  377. device_initcall(init_clocksource_sysfs);
  378. #endif /* CONFIG_SYSFS */
  379. /**
  380. * boot_override_clocksource - boot clock override
  381. * @str: override name
  382. *
  383. * Takes a clocksource= boot argument and uses it
  384. * as the clocksource override name.
  385. */
  386. static int __init boot_override_clocksource(char* str)
  387. {
  388. unsigned long flags;
  389. spin_lock_irqsave(&clocksource_lock, flags);
  390. if (str)
  391. strlcpy(override_name, str, sizeof(override_name));
  392. spin_unlock_irqrestore(&clocksource_lock, flags);
  393. return 1;
  394. }
  395. __setup("clocksource=", boot_override_clocksource);
  396. /**
  397. * boot_override_clock - Compatibility layer for deprecated boot option
  398. * @str: override name
  399. *
  400. * DEPRECATED! Takes a clock= boot argument and uses it
  401. * as the clocksource override name
  402. */
  403. static int __init boot_override_clock(char* str)
  404. {
  405. if (!strcmp(str, "pmtmr")) {
  406. printk("Warning: clock=pmtmr is deprecated. "
  407. "Use clocksource=acpi_pm.\n");
  408. return boot_override_clocksource("acpi_pm");
  409. }
  410. printk("Warning! clock= boot option is deprecated. "
  411. "Use clocksource=xyz\n");
  412. return boot_override_clocksource(str);
  413. }
  414. __setup("clock=", boot_override_clock);