clocksource.c 14 KB

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