clocksource.c 12 KB

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