clocksource.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /* XXX - Would like a better way for initializing curr_clocksource */
  31. extern struct clocksource clocksource_jiffies;
  32. /*[Clocksource internal variables]---------
  33. * curr_clocksource:
  34. * currently selected clocksource. Initialized to clocksource_jiffies.
  35. * next_clocksource:
  36. * pending next selected clocksource.
  37. * clocksource_list:
  38. * linked list with the registered clocksources
  39. * clocksource_lock:
  40. * protects manipulations to curr_clocksource and next_clocksource
  41. * and the clocksource_list
  42. * override_name:
  43. * Name of the user-specified clocksource.
  44. */
  45. static struct clocksource *curr_clocksource = &clocksource_jiffies;
  46. static struct clocksource *next_clocksource;
  47. static LIST_HEAD(clocksource_list);
  48. static DEFINE_SPINLOCK(clocksource_lock);
  49. static char override_name[32];
  50. static int finished_booting;
  51. /* clocksource_done_booting - Called near the end of bootup
  52. *
  53. * Hack to avoid lots of clocksource churn at boot time
  54. */
  55. static int __init clocksource_done_booting(void)
  56. {
  57. finished_booting = 1;
  58. return 0;
  59. }
  60. late_initcall(clocksource_done_booting);
  61. /**
  62. * clocksource_get_next - Returns the selected clocksource
  63. *
  64. */
  65. struct clocksource *clocksource_get_next(void)
  66. {
  67. unsigned long flags;
  68. spin_lock_irqsave(&clocksource_lock, flags);
  69. if (next_clocksource && finished_booting) {
  70. curr_clocksource = next_clocksource;
  71. next_clocksource = NULL;
  72. }
  73. spin_unlock_irqrestore(&clocksource_lock, flags);
  74. return curr_clocksource;
  75. }
  76. /**
  77. * select_clocksource - Finds the best registered clocksource.
  78. *
  79. * Private function. Must hold clocksource_lock when called.
  80. *
  81. * Looks through the list of registered clocksources, returning
  82. * the one with the highest rating value. If there is a clocksource
  83. * name that matches the override string, it returns that clocksource.
  84. */
  85. static struct clocksource *select_clocksource(void)
  86. {
  87. struct clocksource *best = NULL;
  88. struct list_head *tmp;
  89. list_for_each(tmp, &clocksource_list) {
  90. struct clocksource *src;
  91. src = list_entry(tmp, struct clocksource, list);
  92. if (!best)
  93. best = src;
  94. /* check for override: */
  95. if (strlen(src->name) == strlen(override_name) &&
  96. !strcmp(src->name, override_name)) {
  97. best = src;
  98. break;
  99. }
  100. /* pick the highest rating: */
  101. if (src->rating > best->rating)
  102. best = src;
  103. }
  104. return best;
  105. }
  106. /**
  107. * is_registered_source - Checks if clocksource is registered
  108. * @c: pointer to a clocksource
  109. *
  110. * Private helper function. Must hold clocksource_lock when called.
  111. *
  112. * Returns one if the clocksource is already registered, zero otherwise.
  113. */
  114. static int is_registered_source(struct clocksource *c)
  115. {
  116. int len = strlen(c->name);
  117. struct list_head *tmp;
  118. list_for_each(tmp, &clocksource_list) {
  119. struct clocksource *src;
  120. src = list_entry(tmp, struct clocksource, list);
  121. if (strlen(src->name) == len && !strcmp(src->name, c->name))
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * clocksource_register - Used to install new clocksources
  128. * @t: clocksource to be registered
  129. *
  130. * Returns -EBUSY if registration fails, zero otherwise.
  131. */
  132. int clocksource_register(struct clocksource *c)
  133. {
  134. int ret = 0;
  135. unsigned long flags;
  136. spin_lock_irqsave(&clocksource_lock, flags);
  137. /* check if clocksource is already registered */
  138. if (is_registered_source(c)) {
  139. printk("register_clocksource: Cannot register %s. "
  140. "Already registered!", c->name);
  141. ret = -EBUSY;
  142. } else {
  143. /* register it */
  144. list_add(&c->list, &clocksource_list);
  145. /* scan the registered clocksources, and pick the best one */
  146. next_clocksource = select_clocksource();
  147. }
  148. spin_unlock_irqrestore(&clocksource_lock, flags);
  149. return ret;
  150. }
  151. EXPORT_SYMBOL(clocksource_register);
  152. /**
  153. * clocksource_reselect - Rescan list for next clocksource
  154. *
  155. * A quick helper function to be used if a clocksource changes its
  156. * rating. Forces the clocksource list to be re-scanned for the best
  157. * clocksource.
  158. */
  159. void clocksource_reselect(void)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&clocksource_lock, flags);
  163. next_clocksource = select_clocksource();
  164. spin_unlock_irqrestore(&clocksource_lock, flags);
  165. }
  166. EXPORT_SYMBOL(clocksource_reselect);
  167. #ifdef CONFIG_SYSFS
  168. /**
  169. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  170. * @dev: unused
  171. * @buf: char buffer to be filled with clocksource list
  172. *
  173. * Provides sysfs interface for listing current clocksource.
  174. */
  175. static ssize_t
  176. sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
  177. {
  178. char *curr = buf;
  179. spin_lock_irq(&clocksource_lock);
  180. curr += sprintf(curr, "%s ", curr_clocksource->name);
  181. spin_unlock_irq(&clocksource_lock);
  182. curr += sprintf(curr, "\n");
  183. return curr - buf;
  184. }
  185. /**
  186. * sysfs_override_clocksource - interface for manually overriding clocksource
  187. * @dev: unused
  188. * @buf: name of override clocksource
  189. * @count: length of buffer
  190. *
  191. * Takes input from sysfs interface for manually overriding the default
  192. * clocksource selction.
  193. */
  194. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  195. const char *buf, size_t count)
  196. {
  197. size_t ret = count;
  198. /* strings from sysfs write are not 0 terminated! */
  199. if (count >= sizeof(override_name))
  200. return -EINVAL;
  201. /* strip of \n: */
  202. if (buf[count-1] == '\n')
  203. count--;
  204. if (count < 1)
  205. return -EINVAL;
  206. spin_lock_irq(&clocksource_lock);
  207. /* copy the name given: */
  208. memcpy(override_name, buf, count);
  209. override_name[count] = 0;
  210. /* try to select it: */
  211. next_clocksource = select_clocksource();
  212. spin_unlock_irq(&clocksource_lock);
  213. return ret;
  214. }
  215. /**
  216. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  217. * @dev: unused
  218. * @buf: char buffer to be filled with clocksource list
  219. *
  220. * Provides sysfs interface for listing registered clocksources
  221. */
  222. static ssize_t
  223. sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
  224. {
  225. struct list_head *tmp;
  226. char *curr = buf;
  227. spin_lock_irq(&clocksource_lock);
  228. list_for_each(tmp, &clocksource_list) {
  229. struct clocksource *src;
  230. src = list_entry(tmp, struct clocksource, list);
  231. curr += sprintf(curr, "%s ", src->name);
  232. }
  233. spin_unlock_irq(&clocksource_lock);
  234. curr += sprintf(curr, "\n");
  235. return curr - buf;
  236. }
  237. /*
  238. * Sysfs setup bits:
  239. */
  240. static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
  241. sysfs_override_clocksource);
  242. static SYSDEV_ATTR(available_clocksource, 0600,
  243. sysfs_show_available_clocksources, NULL);
  244. static struct sysdev_class clocksource_sysclass = {
  245. set_kset_name("clocksource"),
  246. };
  247. static struct sys_device device_clocksource = {
  248. .id = 0,
  249. .cls = &clocksource_sysclass,
  250. };
  251. static int __init init_clocksource_sysfs(void)
  252. {
  253. int error = sysdev_class_register(&clocksource_sysclass);
  254. if (!error)
  255. error = sysdev_register(&device_clocksource);
  256. if (!error)
  257. error = sysdev_create_file(
  258. &device_clocksource,
  259. &attr_current_clocksource);
  260. if (!error)
  261. error = sysdev_create_file(
  262. &device_clocksource,
  263. &attr_available_clocksource);
  264. return error;
  265. }
  266. device_initcall(init_clocksource_sysfs);
  267. #endif /* CONFIG_SYSFS */
  268. /**
  269. * boot_override_clocksource - boot clock override
  270. * @str: override name
  271. *
  272. * Takes a clocksource= boot argument and uses it
  273. * as the clocksource override name.
  274. */
  275. static int __init boot_override_clocksource(char* str)
  276. {
  277. unsigned long flags;
  278. spin_lock_irqsave(&clocksource_lock, flags);
  279. if (str)
  280. strlcpy(override_name, str, sizeof(override_name));
  281. spin_unlock_irqrestore(&clocksource_lock, flags);
  282. return 1;
  283. }
  284. __setup("clocksource=", boot_override_clocksource);
  285. /**
  286. * boot_override_clock - Compatibility layer for deprecated boot option
  287. * @str: override name
  288. *
  289. * DEPRECATED! Takes a clock= boot argument and uses it
  290. * as the clocksource override name
  291. */
  292. static int __init boot_override_clock(char* str)
  293. {
  294. if (!strcmp(str, "pmtmr")) {
  295. printk("Warning: clock=pmtmr is deprecated. "
  296. "Use clocksource=acpi_pm.\n");
  297. return boot_override_clocksource("acpi_pm");
  298. }
  299. printk("Warning! clock= boot option is deprecated. "
  300. "Use clocksource=xyz\n");
  301. return boot_override_clocksource(str);
  302. }
  303. __setup("clock=", boot_override_clock);