clocksource.c 13 KB

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