clocksource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) {
  141. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  142. if (!watchdog || cs->rating > watchdog->rating) {
  143. if (watchdog)
  144. del_timer(&watchdog_timer);
  145. watchdog = cs;
  146. init_timer(&watchdog_timer);
  147. watchdog_timer.function = clocksource_watchdog;
  148. /* Reset watchdog cycles */
  149. list_for_each_entry(cse, &watchdog_list, wd_list)
  150. cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
  151. /* Start if list is not empty */
  152. if (!list_empty(&watchdog_list)) {
  153. watchdog_last = watchdog->read();
  154. watchdog_timer.expires =
  155. jiffies + WATCHDOG_INTERVAL;
  156. add_timer(&watchdog_timer);
  157. }
  158. }
  159. }
  160. spin_unlock_irqrestore(&watchdog_lock, flags);
  161. }
  162. #else
  163. static void clocksource_check_watchdog(struct clocksource *cs)
  164. {
  165. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  166. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  167. }
  168. #endif
  169. /**
  170. * clocksource_get_next - Returns the selected clocksource
  171. *
  172. */
  173. struct clocksource *clocksource_get_next(void)
  174. {
  175. unsigned long flags;
  176. spin_lock_irqsave(&clocksource_lock, flags);
  177. if (next_clocksource && finished_booting) {
  178. curr_clocksource = next_clocksource;
  179. next_clocksource = NULL;
  180. }
  181. spin_unlock_irqrestore(&clocksource_lock, flags);
  182. return curr_clocksource;
  183. }
  184. /**
  185. * select_clocksource - Selects the best registered clocksource.
  186. *
  187. * Private function. Must hold clocksource_lock when called.
  188. *
  189. * Select the clocksource with the best rating, or the clocksource,
  190. * which is selected by userspace override.
  191. */
  192. static struct clocksource *select_clocksource(void)
  193. {
  194. struct clocksource *next;
  195. if (list_empty(&clocksource_list))
  196. return NULL;
  197. if (clocksource_override)
  198. next = clocksource_override;
  199. else
  200. next = list_entry(clocksource_list.next, struct clocksource,
  201. list);
  202. if (next == curr_clocksource)
  203. return NULL;
  204. return next;
  205. }
  206. /*
  207. * Enqueue the clocksource sorted by rating
  208. */
  209. static int clocksource_enqueue(struct clocksource *c)
  210. {
  211. struct list_head *tmp, *entry = &clocksource_list;
  212. list_for_each(tmp, &clocksource_list) {
  213. struct clocksource *cs;
  214. cs = list_entry(tmp, struct clocksource, list);
  215. if (cs == c)
  216. return -EBUSY;
  217. /* Keep track of the place, where to insert */
  218. if (cs->rating >= c->rating)
  219. entry = tmp;
  220. }
  221. list_add(&c->list, entry);
  222. if (strlen(c->name) == strlen(override_name) &&
  223. !strcmp(c->name, override_name))
  224. clocksource_override = c;
  225. return 0;
  226. }
  227. /**
  228. * clocksource_register - Used to install new clocksources
  229. * @t: clocksource to be registered
  230. *
  231. * Returns -EBUSY if registration fails, zero otherwise.
  232. */
  233. int clocksource_register(struct clocksource *c)
  234. {
  235. unsigned long flags;
  236. int ret;
  237. spin_lock_irqsave(&clocksource_lock, flags);
  238. ret = clocksource_enqueue(c);
  239. if (!ret)
  240. next_clocksource = select_clocksource();
  241. spin_unlock_irqrestore(&clocksource_lock, flags);
  242. if (!ret)
  243. clocksource_check_watchdog(c);
  244. return ret;
  245. }
  246. EXPORT_SYMBOL(clocksource_register);
  247. /**
  248. * clocksource_change_rating - Change the rating of a registered clocksource
  249. *
  250. */
  251. void clocksource_change_rating(struct clocksource *cs, int rating)
  252. {
  253. unsigned long flags;
  254. spin_lock_irqsave(&clocksource_lock, flags);
  255. list_del(&cs->list);
  256. cs->rating = rating;
  257. clocksource_enqueue(cs);
  258. next_clocksource = select_clocksource();
  259. spin_unlock_irqrestore(&clocksource_lock, flags);
  260. }
  261. #ifdef CONFIG_SYSFS
  262. /**
  263. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  264. * @dev: unused
  265. * @buf: char buffer to be filled with clocksource list
  266. *
  267. * Provides sysfs interface for listing current clocksource.
  268. */
  269. static ssize_t
  270. sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
  271. {
  272. char *curr = buf;
  273. spin_lock_irq(&clocksource_lock);
  274. curr += sprintf(curr, "%s ", curr_clocksource->name);
  275. spin_unlock_irq(&clocksource_lock);
  276. curr += sprintf(curr, "\n");
  277. return curr - buf;
  278. }
  279. /**
  280. * sysfs_override_clocksource - interface for manually overriding clocksource
  281. * @dev: unused
  282. * @buf: name of override clocksource
  283. * @count: length of buffer
  284. *
  285. * Takes input from sysfs interface for manually overriding the default
  286. * clocksource selction.
  287. */
  288. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  289. const char *buf, size_t count)
  290. {
  291. struct clocksource *ovr = NULL;
  292. struct list_head *tmp;
  293. size_t ret = count;
  294. int len;
  295. /* strings from sysfs write are not 0 terminated! */
  296. if (count >= sizeof(override_name))
  297. return -EINVAL;
  298. /* strip of \n: */
  299. if (buf[count-1] == '\n')
  300. count--;
  301. spin_lock_irq(&clocksource_lock);
  302. if (count > 0)
  303. memcpy(override_name, buf, count);
  304. override_name[count] = 0;
  305. len = strlen(override_name);
  306. if (len) {
  307. ovr = clocksource_override;
  308. /* try to select it: */
  309. list_for_each(tmp, &clocksource_list) {
  310. struct clocksource *cs;
  311. cs = list_entry(tmp, struct clocksource, list);
  312. if (strlen(cs->name) == len &&
  313. !strcmp(cs->name, override_name))
  314. ovr = cs;
  315. }
  316. }
  317. /* Reselect, when the override name has changed */
  318. if (ovr != clocksource_override) {
  319. clocksource_override = ovr;
  320. next_clocksource = select_clocksource();
  321. }
  322. spin_unlock_irq(&clocksource_lock);
  323. return ret;
  324. }
  325. /**
  326. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  327. * @dev: unused
  328. * @buf: char buffer to be filled with clocksource list
  329. *
  330. * Provides sysfs interface for listing registered clocksources
  331. */
  332. static ssize_t
  333. sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
  334. {
  335. struct list_head *tmp;
  336. char *curr = buf;
  337. spin_lock_irq(&clocksource_lock);
  338. list_for_each(tmp, &clocksource_list) {
  339. struct clocksource *src;
  340. src = list_entry(tmp, struct clocksource, list);
  341. curr += sprintf(curr, "%s ", src->name);
  342. }
  343. spin_unlock_irq(&clocksource_lock);
  344. curr += sprintf(curr, "\n");
  345. return curr - buf;
  346. }
  347. /*
  348. * Sysfs setup bits:
  349. */
  350. static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
  351. sysfs_override_clocksource);
  352. static SYSDEV_ATTR(available_clocksource, 0600,
  353. sysfs_show_available_clocksources, NULL);
  354. static struct sysdev_class clocksource_sysclass = {
  355. set_kset_name("clocksource"),
  356. };
  357. static struct sys_device device_clocksource = {
  358. .id = 0,
  359. .cls = &clocksource_sysclass,
  360. };
  361. static int __init init_clocksource_sysfs(void)
  362. {
  363. int error = sysdev_class_register(&clocksource_sysclass);
  364. if (!error)
  365. error = sysdev_register(&device_clocksource);
  366. if (!error)
  367. error = sysdev_create_file(
  368. &device_clocksource,
  369. &attr_current_clocksource);
  370. if (!error)
  371. error = sysdev_create_file(
  372. &device_clocksource,
  373. &attr_available_clocksource);
  374. return error;
  375. }
  376. device_initcall(init_clocksource_sysfs);
  377. #endif /* CONFIG_SYSFS */
  378. /**
  379. * boot_override_clocksource - boot clock override
  380. * @str: override name
  381. *
  382. * Takes a clocksource= boot argument and uses it
  383. * as the clocksource override name.
  384. */
  385. static int __init boot_override_clocksource(char* str)
  386. {
  387. unsigned long flags;
  388. spin_lock_irqsave(&clocksource_lock, flags);
  389. if (str)
  390. strlcpy(override_name, str, sizeof(override_name));
  391. spin_unlock_irqrestore(&clocksource_lock, flags);
  392. return 1;
  393. }
  394. __setup("clocksource=", boot_override_clocksource);
  395. /**
  396. * boot_override_clock - Compatibility layer for deprecated boot option
  397. * @str: override name
  398. *
  399. * DEPRECATED! Takes a clock= boot argument and uses it
  400. * as the clocksource override name
  401. */
  402. static int __init boot_override_clock(char* str)
  403. {
  404. if (!strcmp(str, "pmtmr")) {
  405. printk("Warning: clock=pmtmr is deprecated. "
  406. "Use clocksource=acpi_pm.\n");
  407. return boot_override_clocksource("acpi_pm");
  408. }
  409. printk("Warning! clock= boot option is deprecated. "
  410. "Use clocksource=xyz\n");
  411. return boot_override_clocksource(str);
  412. }
  413. __setup("clock=", boot_override_clock);