clocksource.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /**
  168. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  169. * @dev: unused
  170. * @buf: char buffer to be filled with clocksource list
  171. *
  172. * Provides sysfs interface for listing current clocksource.
  173. */
  174. static ssize_t
  175. sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
  176. {
  177. char *curr = buf;
  178. spin_lock_irq(&clocksource_lock);
  179. curr += sprintf(curr, "%s ", curr_clocksource->name);
  180. spin_unlock_irq(&clocksource_lock);
  181. curr += sprintf(curr, "\n");
  182. return curr - buf;
  183. }
  184. /**
  185. * sysfs_override_clocksource - interface for manually overriding clocksource
  186. * @dev: unused
  187. * @buf: name of override clocksource
  188. * @count: length of buffer
  189. *
  190. * Takes input from sysfs interface for manually overriding the default
  191. * clocksource selction.
  192. */
  193. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  194. const char *buf, size_t count)
  195. {
  196. size_t ret = count;
  197. /* strings from sysfs write are not 0 terminated! */
  198. if (count >= sizeof(override_name))
  199. return -EINVAL;
  200. /* strip of \n: */
  201. if (buf[count-1] == '\n')
  202. count--;
  203. if (count < 1)
  204. return -EINVAL;
  205. spin_lock_irq(&clocksource_lock);
  206. /* copy the name given: */
  207. memcpy(override_name, buf, count);
  208. override_name[count] = 0;
  209. /* try to select it: */
  210. next_clocksource = select_clocksource();
  211. spin_unlock_irq(&clocksource_lock);
  212. return ret;
  213. }
  214. /**
  215. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  216. * @dev: unused
  217. * @buf: char buffer to be filled with clocksource list
  218. *
  219. * Provides sysfs interface for listing registered clocksources
  220. */
  221. static ssize_t
  222. sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
  223. {
  224. struct list_head *tmp;
  225. char *curr = buf;
  226. spin_lock_irq(&clocksource_lock);
  227. list_for_each(tmp, &clocksource_list) {
  228. struct clocksource *src;
  229. src = list_entry(tmp, struct clocksource, list);
  230. curr += sprintf(curr, "%s ", src->name);
  231. }
  232. spin_unlock_irq(&clocksource_lock);
  233. curr += sprintf(curr, "\n");
  234. return curr - buf;
  235. }
  236. /*
  237. * Sysfs setup bits:
  238. */
  239. static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
  240. sysfs_override_clocksource);
  241. static SYSDEV_ATTR(available_clocksource, 0600,
  242. sysfs_show_available_clocksources, NULL);
  243. static struct sysdev_class clocksource_sysclass = {
  244. set_kset_name("clocksource"),
  245. };
  246. static struct sys_device device_clocksource = {
  247. .id = 0,
  248. .cls = &clocksource_sysclass,
  249. };
  250. static int __init init_clocksource_sysfs(void)
  251. {
  252. int error = sysdev_class_register(&clocksource_sysclass);
  253. if (!error)
  254. error = sysdev_register(&device_clocksource);
  255. if (!error)
  256. error = sysdev_create_file(
  257. &device_clocksource,
  258. &attr_current_clocksource);
  259. if (!error)
  260. error = sysdev_create_file(
  261. &device_clocksource,
  262. &attr_available_clocksource);
  263. return error;
  264. }
  265. device_initcall(init_clocksource_sysfs);
  266. /**
  267. * boot_override_clocksource - boot clock override
  268. * @str: override name
  269. *
  270. * Takes a clocksource= boot argument and uses it
  271. * as the clocksource override name.
  272. */
  273. static int __init boot_override_clocksource(char* str)
  274. {
  275. unsigned long flags;
  276. spin_lock_irqsave(&clocksource_lock, flags);
  277. if (str)
  278. strlcpy(override_name, str, sizeof(override_name));
  279. spin_unlock_irqrestore(&clocksource_lock, flags);
  280. return 1;
  281. }
  282. __setup("clocksource=", boot_override_clocksource);
  283. /**
  284. * boot_override_clock - Compatibility layer for deprecated boot option
  285. * @str: override name
  286. *
  287. * DEPRECATED! Takes a clock= boot argument and uses it
  288. * as the clocksource override name
  289. */
  290. static int __init boot_override_clock(char* str)
  291. {
  292. if (!strcmp(str, "pmtmr")) {
  293. printk("Warning: clock=pmtmr is deprecated. "
  294. "Use clocksource=acpi_pm.\n");
  295. return boot_override_clocksource("acpi_pm");
  296. }
  297. printk("Warning! clock= boot option is deprecated. "
  298. "Use clocksource=xyz\n");
  299. return boot_override_clocksource(str);
  300. }
  301. __setup("clock=", boot_override_clock);