clocksource.c 12 KB

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