clocksource.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. */
  25. #include <linux/clocksource.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
  30. #include <linux/tick.h>
  31. #include <linux/kthread.h>
  32. void timecounter_init(struct timecounter *tc,
  33. const struct cyclecounter *cc,
  34. u64 start_tstamp)
  35. {
  36. tc->cc = cc;
  37. tc->cycle_last = cc->read(cc);
  38. tc->nsec = start_tstamp;
  39. }
  40. EXPORT_SYMBOL(timecounter_init);
  41. /**
  42. * timecounter_read_delta - get nanoseconds since last call of this function
  43. * @tc: Pointer to time counter
  44. *
  45. * When the underlying cycle counter runs over, this will be handled
  46. * correctly as long as it does not run over more than once between
  47. * calls.
  48. *
  49. * The first call to this function for a new time counter initializes
  50. * the time tracking and returns an undefined result.
  51. */
  52. static u64 timecounter_read_delta(struct timecounter *tc)
  53. {
  54. cycle_t cycle_now, cycle_delta;
  55. u64 ns_offset;
  56. /* read cycle counter: */
  57. cycle_now = tc->cc->read(tc->cc);
  58. /* calculate the delta since the last timecounter_read_delta(): */
  59. cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
  60. /* convert to nanoseconds: */
  61. ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
  62. /* update time stamp of timecounter_read_delta() call: */
  63. tc->cycle_last = cycle_now;
  64. return ns_offset;
  65. }
  66. u64 timecounter_read(struct timecounter *tc)
  67. {
  68. u64 nsec;
  69. /* increment time by nanoseconds since last call */
  70. nsec = timecounter_read_delta(tc);
  71. nsec += tc->nsec;
  72. tc->nsec = nsec;
  73. return nsec;
  74. }
  75. EXPORT_SYMBOL(timecounter_read);
  76. u64 timecounter_cyc2time(struct timecounter *tc,
  77. cycle_t cycle_tstamp)
  78. {
  79. u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
  80. u64 nsec;
  81. /*
  82. * Instead of always treating cycle_tstamp as more recent
  83. * than tc->cycle_last, detect when it is too far in the
  84. * future and treat it as old time stamp instead.
  85. */
  86. if (cycle_delta > tc->cc->mask / 2) {
  87. cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
  88. nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
  89. } else {
  90. nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
  91. }
  92. return nsec;
  93. }
  94. EXPORT_SYMBOL(timecounter_cyc2time);
  95. /*[Clocksource internal variables]---------
  96. * curr_clocksource:
  97. * currently selected clocksource.
  98. * clocksource_list:
  99. * linked list with the registered clocksources
  100. * clocksource_mutex:
  101. * protects manipulations to curr_clocksource and the clocksource_list
  102. * override_name:
  103. * Name of the user-specified clocksource.
  104. */
  105. static struct clocksource *curr_clocksource;
  106. static LIST_HEAD(clocksource_list);
  107. static DEFINE_MUTEX(clocksource_mutex);
  108. static char override_name[32];
  109. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
  110. static void clocksource_watchdog_work(struct work_struct *work);
  111. static LIST_HEAD(watchdog_list);
  112. static struct clocksource *watchdog;
  113. static struct timer_list watchdog_timer;
  114. static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
  115. static DEFINE_SPINLOCK(watchdog_lock);
  116. static cycle_t watchdog_last;
  117. static int watchdog_running;
  118. static int clocksource_watchdog_kthread(void *data);
  119. static void __clocksource_change_rating(struct clocksource *cs, int rating);
  120. /*
  121. * Interval: 0.5sec Threshold: 0.0625s
  122. */
  123. #define WATCHDOG_INTERVAL (HZ >> 1)
  124. #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
  125. static void clocksource_watchdog_work(struct work_struct *work)
  126. {
  127. /*
  128. * If kthread_run fails the next watchdog scan over the
  129. * watchdog_list will find the unstable clock again.
  130. */
  131. kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
  132. }
  133. static void __clocksource_unstable(struct clocksource *cs)
  134. {
  135. cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
  136. cs->flags |= CLOCK_SOURCE_UNSTABLE;
  137. schedule_work(&watchdog_work);
  138. }
  139. static void clocksource_unstable(struct clocksource *cs, int64_t delta)
  140. {
  141. printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
  142. cs->name, delta);
  143. __clocksource_unstable(cs);
  144. }
  145. /**
  146. * clocksource_mark_unstable - mark clocksource unstable via watchdog
  147. * @cs: clocksource to be marked unstable
  148. *
  149. * This function is called instead of clocksource_change_rating from
  150. * cpu hotplug code to avoid a deadlock between the clocksource mutex
  151. * and the cpu hotplug mutex. It defers the update of the clocksource
  152. * to the watchdog thread.
  153. */
  154. void clocksource_mark_unstable(struct clocksource *cs)
  155. {
  156. unsigned long flags;
  157. spin_lock_irqsave(&watchdog_lock, flags);
  158. if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
  159. if (list_empty(&cs->wd_list))
  160. list_add(&cs->wd_list, &watchdog_list);
  161. __clocksource_unstable(cs);
  162. }
  163. spin_unlock_irqrestore(&watchdog_lock, flags);
  164. }
  165. static void clocksource_watchdog(unsigned long data)
  166. {
  167. struct clocksource *cs;
  168. cycle_t csnow, wdnow;
  169. int64_t wd_nsec, cs_nsec;
  170. int next_cpu;
  171. spin_lock(&watchdog_lock);
  172. if (!watchdog_running)
  173. goto out;
  174. wdnow = watchdog->read(watchdog);
  175. wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
  176. watchdog->mult, watchdog->shift);
  177. watchdog_last = wdnow;
  178. list_for_each_entry(cs, &watchdog_list, wd_list) {
  179. /* Clocksource already marked unstable? */
  180. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  181. schedule_work(&watchdog_work);
  182. continue;
  183. }
  184. csnow = cs->read(cs);
  185. /* Clocksource initialized ? */
  186. if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
  187. cs->flags |= CLOCK_SOURCE_WATCHDOG;
  188. cs->wd_last = csnow;
  189. continue;
  190. }
  191. /* Check the deviation from the watchdog clocksource. */
  192. cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) &
  193. cs->mask, cs->mult, cs->shift);
  194. cs->wd_last = csnow;
  195. if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
  196. clocksource_unstable(cs, cs_nsec - wd_nsec);
  197. continue;
  198. }
  199. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  200. (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
  201. (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
  202. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  203. /*
  204. * We just marked the clocksource as highres-capable,
  205. * notify the rest of the system as well so that we
  206. * transition into high-res mode:
  207. */
  208. tick_clock_notify();
  209. }
  210. }
  211. /*
  212. * Cycle through CPUs to check if the CPUs stay synchronized
  213. * to each other.
  214. */
  215. next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  216. if (next_cpu >= nr_cpu_ids)
  217. next_cpu = cpumask_first(cpu_online_mask);
  218. watchdog_timer.expires += WATCHDOG_INTERVAL;
  219. add_timer_on(&watchdog_timer, next_cpu);
  220. out:
  221. spin_unlock(&watchdog_lock);
  222. }
  223. static inline void clocksource_start_watchdog(void)
  224. {
  225. if (watchdog_running || !watchdog || list_empty(&watchdog_list))
  226. return;
  227. init_timer(&watchdog_timer);
  228. watchdog_timer.function = clocksource_watchdog;
  229. watchdog_last = watchdog->read(watchdog);
  230. watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
  231. add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
  232. watchdog_running = 1;
  233. }
  234. static inline void clocksource_stop_watchdog(void)
  235. {
  236. if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
  237. return;
  238. del_timer(&watchdog_timer);
  239. watchdog_running = 0;
  240. }
  241. static inline void clocksource_reset_watchdog(void)
  242. {
  243. struct clocksource *cs;
  244. list_for_each_entry(cs, &watchdog_list, wd_list)
  245. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  246. }
  247. static void clocksource_resume_watchdog(void)
  248. {
  249. unsigned long flags;
  250. spin_lock_irqsave(&watchdog_lock, flags);
  251. clocksource_reset_watchdog();
  252. spin_unlock_irqrestore(&watchdog_lock, flags);
  253. }
  254. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  255. {
  256. unsigned long flags;
  257. spin_lock_irqsave(&watchdog_lock, flags);
  258. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  259. /* cs is a clocksource to be watched. */
  260. list_add(&cs->wd_list, &watchdog_list);
  261. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  262. } else {
  263. /* cs is a watchdog. */
  264. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  265. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  266. /* Pick the best watchdog. */
  267. if (!watchdog || cs->rating > watchdog->rating) {
  268. watchdog = cs;
  269. /* Reset watchdog cycles */
  270. clocksource_reset_watchdog();
  271. }
  272. }
  273. /* Check if the watchdog timer needs to be started. */
  274. clocksource_start_watchdog();
  275. spin_unlock_irqrestore(&watchdog_lock, flags);
  276. }
  277. static void clocksource_dequeue_watchdog(struct clocksource *cs)
  278. {
  279. struct clocksource *tmp;
  280. unsigned long flags;
  281. spin_lock_irqsave(&watchdog_lock, flags);
  282. if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
  283. /* cs is a watched clocksource. */
  284. list_del_init(&cs->wd_list);
  285. } else if (cs == watchdog) {
  286. /* Reset watchdog cycles */
  287. clocksource_reset_watchdog();
  288. /* Current watchdog is removed. Find an alternative. */
  289. watchdog = NULL;
  290. list_for_each_entry(tmp, &clocksource_list, list) {
  291. if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
  292. continue;
  293. if (!watchdog || tmp->rating > watchdog->rating)
  294. watchdog = tmp;
  295. }
  296. }
  297. cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
  298. /* Check if the watchdog timer needs to be stopped. */
  299. clocksource_stop_watchdog();
  300. spin_unlock_irqrestore(&watchdog_lock, flags);
  301. }
  302. static int clocksource_watchdog_kthread(void *data)
  303. {
  304. struct clocksource *cs, *tmp;
  305. unsigned long flags;
  306. LIST_HEAD(unstable);
  307. mutex_lock(&clocksource_mutex);
  308. spin_lock_irqsave(&watchdog_lock, flags);
  309. list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
  310. if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
  311. list_del_init(&cs->wd_list);
  312. list_add(&cs->wd_list, &unstable);
  313. }
  314. /* Check if the watchdog timer needs to be stopped. */
  315. clocksource_stop_watchdog();
  316. spin_unlock_irqrestore(&watchdog_lock, flags);
  317. /* Needs to be done outside of watchdog lock */
  318. list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
  319. list_del_init(&cs->wd_list);
  320. __clocksource_change_rating(cs, 0);
  321. }
  322. mutex_unlock(&clocksource_mutex);
  323. return 0;
  324. }
  325. #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
  326. static void clocksource_enqueue_watchdog(struct clocksource *cs)
  327. {
  328. if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
  329. cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
  330. }
  331. static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
  332. static inline void clocksource_resume_watchdog(void) { }
  333. #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
  334. /**
  335. * clocksource_resume - resume the clocksource(s)
  336. */
  337. void clocksource_resume(void)
  338. {
  339. struct clocksource *cs;
  340. mutex_lock(&clocksource_mutex);
  341. list_for_each_entry(cs, &clocksource_list, list)
  342. if (cs->resume)
  343. cs->resume();
  344. clocksource_resume_watchdog();
  345. mutex_unlock(&clocksource_mutex);
  346. }
  347. /**
  348. * clocksource_touch_watchdog - Update watchdog
  349. *
  350. * Update the watchdog after exception contexts such as kgdb so as not
  351. * to incorrectly trip the watchdog.
  352. *
  353. */
  354. void clocksource_touch_watchdog(void)
  355. {
  356. clocksource_resume_watchdog();
  357. }
  358. #ifdef CONFIG_GENERIC_TIME
  359. static int finished_booting;
  360. /**
  361. * clocksource_select - Select the best clocksource available
  362. *
  363. * Private function. Must hold clocksource_mutex when called.
  364. *
  365. * Select the clocksource with the best rating, or the clocksource,
  366. * which is selected by userspace override.
  367. */
  368. static void clocksource_select(void)
  369. {
  370. struct clocksource *best, *cs;
  371. if (!finished_booting || list_empty(&clocksource_list))
  372. return;
  373. /* First clocksource on the list has the best rating. */
  374. best = list_first_entry(&clocksource_list, struct clocksource, list);
  375. /* Check for the override clocksource. */
  376. list_for_each_entry(cs, &clocksource_list, list) {
  377. if (strcmp(cs->name, override_name) != 0)
  378. continue;
  379. /*
  380. * Check to make sure we don't switch to a non-highres
  381. * capable clocksource if the tick code is in oneshot
  382. * mode (highres or nohz)
  383. */
  384. if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
  385. tick_oneshot_mode_active()) {
  386. /* Override clocksource cannot be used. */
  387. printk(KERN_WARNING "Override clocksource %s is not "
  388. "HRT compatible. Cannot switch while in "
  389. "HRT/NOHZ mode\n", cs->name);
  390. override_name[0] = 0;
  391. } else
  392. /* Override clocksource can be used. */
  393. best = cs;
  394. break;
  395. }
  396. if (curr_clocksource != best) {
  397. printk(KERN_INFO "Switching to clocksource %s\n", best->name);
  398. curr_clocksource = best;
  399. timekeeping_notify(curr_clocksource);
  400. }
  401. }
  402. /*
  403. * clocksource_done_booting - Called near the end of core bootup
  404. *
  405. * Hack to avoid lots of clocksource churn at boot time.
  406. * We use fs_initcall because we want this to start before
  407. * device_initcall but after subsys_initcall.
  408. */
  409. static int __init clocksource_done_booting(void)
  410. {
  411. finished_booting = 1;
  412. mutex_lock(&clocksource_mutex);
  413. clocksource_select();
  414. mutex_unlock(&clocksource_mutex);
  415. return 0;
  416. }
  417. fs_initcall(clocksource_done_booting);
  418. #else /* CONFIG_GENERIC_TIME */
  419. static inline void clocksource_select(void) { }
  420. #endif
  421. /*
  422. * Enqueue the clocksource sorted by rating
  423. */
  424. static void clocksource_enqueue(struct clocksource *cs)
  425. {
  426. struct list_head *entry = &clocksource_list;
  427. struct clocksource *tmp;
  428. list_for_each_entry(tmp, &clocksource_list, list)
  429. /* Keep track of the place, where to insert */
  430. if (tmp->rating >= cs->rating)
  431. entry = &tmp->list;
  432. list_add(&cs->list, entry);
  433. }
  434. /**
  435. * clocksource_register - Used to install new clocksources
  436. * @t: clocksource to be registered
  437. *
  438. * Returns -EBUSY if registration fails, zero otherwise.
  439. */
  440. int clocksource_register(struct clocksource *cs)
  441. {
  442. mutex_lock(&clocksource_mutex);
  443. clocksource_enqueue(cs);
  444. clocksource_select();
  445. clocksource_enqueue_watchdog(cs);
  446. mutex_unlock(&clocksource_mutex);
  447. return 0;
  448. }
  449. EXPORT_SYMBOL(clocksource_register);
  450. static void __clocksource_change_rating(struct clocksource *cs, int rating)
  451. {
  452. list_del(&cs->list);
  453. cs->rating = rating;
  454. clocksource_enqueue(cs);
  455. clocksource_select();
  456. }
  457. /**
  458. * clocksource_change_rating - Change the rating of a registered clocksource
  459. */
  460. void clocksource_change_rating(struct clocksource *cs, int rating)
  461. {
  462. mutex_lock(&clocksource_mutex);
  463. __clocksource_change_rating(cs, rating);
  464. mutex_unlock(&clocksource_mutex);
  465. }
  466. EXPORT_SYMBOL(clocksource_change_rating);
  467. /**
  468. * clocksource_unregister - remove a registered clocksource
  469. */
  470. void clocksource_unregister(struct clocksource *cs)
  471. {
  472. mutex_lock(&clocksource_mutex);
  473. clocksource_dequeue_watchdog(cs);
  474. list_del(&cs->list);
  475. clocksource_select();
  476. mutex_unlock(&clocksource_mutex);
  477. }
  478. EXPORT_SYMBOL(clocksource_unregister);
  479. #ifdef CONFIG_SYSFS
  480. /**
  481. * sysfs_show_current_clocksources - sysfs interface for current clocksource
  482. * @dev: unused
  483. * @buf: char buffer to be filled with clocksource list
  484. *
  485. * Provides sysfs interface for listing current clocksource.
  486. */
  487. static ssize_t
  488. sysfs_show_current_clocksources(struct sys_device *dev,
  489. struct sysdev_attribute *attr, char *buf)
  490. {
  491. ssize_t count = 0;
  492. mutex_lock(&clocksource_mutex);
  493. count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
  494. mutex_unlock(&clocksource_mutex);
  495. return count;
  496. }
  497. /**
  498. * sysfs_override_clocksource - interface for manually overriding clocksource
  499. * @dev: unused
  500. * @buf: name of override clocksource
  501. * @count: length of buffer
  502. *
  503. * Takes input from sysfs interface for manually overriding the default
  504. * clocksource selction.
  505. */
  506. static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  507. struct sysdev_attribute *attr,
  508. const char *buf, size_t count)
  509. {
  510. size_t ret = count;
  511. /* strings from sysfs write are not 0 terminated! */
  512. if (count >= sizeof(override_name))
  513. return -EINVAL;
  514. /* strip of \n: */
  515. if (buf[count-1] == '\n')
  516. count--;
  517. mutex_lock(&clocksource_mutex);
  518. if (count > 0)
  519. memcpy(override_name, buf, count);
  520. override_name[count] = 0;
  521. clocksource_select();
  522. mutex_unlock(&clocksource_mutex);
  523. return ret;
  524. }
  525. /**
  526. * sysfs_show_available_clocksources - sysfs interface for listing clocksource
  527. * @dev: unused
  528. * @buf: char buffer to be filled with clocksource list
  529. *
  530. * Provides sysfs interface for listing registered clocksources
  531. */
  532. static ssize_t
  533. sysfs_show_available_clocksources(struct sys_device *dev,
  534. struct sysdev_attribute *attr,
  535. char *buf)
  536. {
  537. struct clocksource *src;
  538. ssize_t count = 0;
  539. mutex_lock(&clocksource_mutex);
  540. list_for_each_entry(src, &clocksource_list, list) {
  541. /*
  542. * Don't show non-HRES clocksource if the tick code is
  543. * in one shot mode (highres=on or nohz=on)
  544. */
  545. if (!tick_oneshot_mode_active() ||
  546. (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
  547. count += snprintf(buf + count,
  548. max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
  549. "%s ", src->name);
  550. }
  551. mutex_unlock(&clocksource_mutex);
  552. count += snprintf(buf + count,
  553. max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
  554. return count;
  555. }
  556. /*
  557. * Sysfs setup bits:
  558. */
  559. static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
  560. sysfs_override_clocksource);
  561. static SYSDEV_ATTR(available_clocksource, 0444,
  562. sysfs_show_available_clocksources, NULL);
  563. static struct sysdev_class clocksource_sysclass = {
  564. .name = "clocksource",
  565. };
  566. static struct sys_device device_clocksource = {
  567. .id = 0,
  568. .cls = &clocksource_sysclass,
  569. };
  570. static int __init init_clocksource_sysfs(void)
  571. {
  572. int error = sysdev_class_register(&clocksource_sysclass);
  573. if (!error)
  574. error = sysdev_register(&device_clocksource);
  575. if (!error)
  576. error = sysdev_create_file(
  577. &device_clocksource,
  578. &attr_current_clocksource);
  579. if (!error)
  580. error = sysdev_create_file(
  581. &device_clocksource,
  582. &attr_available_clocksource);
  583. return error;
  584. }
  585. device_initcall(init_clocksource_sysfs);
  586. #endif /* CONFIG_SYSFS */
  587. /**
  588. * boot_override_clocksource - boot clock override
  589. * @str: override name
  590. *
  591. * Takes a clocksource= boot argument and uses it
  592. * as the clocksource override name.
  593. */
  594. static int __init boot_override_clocksource(char* str)
  595. {
  596. mutex_lock(&clocksource_mutex);
  597. if (str)
  598. strlcpy(override_name, str, sizeof(override_name));
  599. mutex_unlock(&clocksource_mutex);
  600. return 1;
  601. }
  602. __setup("clocksource=", boot_override_clocksource);
  603. /**
  604. * boot_override_clock - Compatibility layer for deprecated boot option
  605. * @str: override name
  606. *
  607. * DEPRECATED! Takes a clock= boot argument and uses it
  608. * as the clocksource override name
  609. */
  610. static int __init boot_override_clock(char* str)
  611. {
  612. if (!strcmp(str, "pmtmr")) {
  613. printk("Warning: clock=pmtmr is deprecated. "
  614. "Use clocksource=acpi_pm.\n");
  615. return boot_override_clocksource("acpi_pm");
  616. }
  617. printk("Warning! clock= boot option is deprecated. "
  618. "Use clocksource=xyz\n");
  619. return boot_override_clocksource(str);
  620. }
  621. __setup("clock=", boot_override_clock);