clockevents.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * linux/kernel/time/clockevents.c
  3. *
  4. * This file contains functions which manage clock event devices.
  5. *
  6. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  8. * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  9. *
  10. * This code is licenced under the GPL version 2. For details see
  11. * kernel-base/COPYING.
  12. */
  13. #include <linux/clockchips.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/smp.h>
  18. #include <linux/device.h>
  19. #include "tick-internal.h"
  20. /* The registered clock event devices */
  21. static LIST_HEAD(clockevent_devices);
  22. static LIST_HEAD(clockevents_released);
  23. /* Protection for the above */
  24. static DEFINE_RAW_SPINLOCK(clockevents_lock);
  25. /* Protection for unbind operations */
  26. static DEFINE_MUTEX(clockevents_mutex);
  27. struct ce_unbind {
  28. struct clock_event_device *ce;
  29. int res;
  30. };
  31. static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
  32. bool ismax)
  33. {
  34. u64 clc = (u64) latch << evt->shift;
  35. u64 rnd;
  36. if (unlikely(!evt->mult)) {
  37. evt->mult = 1;
  38. WARN_ON(1);
  39. }
  40. rnd = (u64) evt->mult - 1;
  41. /*
  42. * Upper bound sanity check. If the backwards conversion is
  43. * not equal latch, we know that the above shift overflowed.
  44. */
  45. if ((clc >> evt->shift) != (u64)latch)
  46. clc = ~0ULL;
  47. /*
  48. * Scaled math oddities:
  49. *
  50. * For mult <= (1 << shift) we can safely add mult - 1 to
  51. * prevent integer rounding loss. So the backwards conversion
  52. * from nsec to device ticks will be correct.
  53. *
  54. * For mult > (1 << shift), i.e. device frequency is > 1GHz we
  55. * need to be careful. Adding mult - 1 will result in a value
  56. * which when converted back to device ticks can be larger
  57. * than latch by up to (mult - 1) >> shift. For the min_delta
  58. * calculation we still want to apply this in order to stay
  59. * above the minimum device ticks limit. For the upper limit
  60. * we would end up with a latch value larger than the upper
  61. * limit of the device, so we omit the add to stay below the
  62. * device upper boundary.
  63. *
  64. * Also omit the add if it would overflow the u64 boundary.
  65. */
  66. if ((~0ULL - clc > rnd) &&
  67. (!ismax || evt->mult <= (1ULL << evt->shift)))
  68. clc += rnd;
  69. do_div(clc, evt->mult);
  70. /* Deltas less than 1usec are pointless noise */
  71. return clc > 1000 ? clc : 1000;
  72. }
  73. /**
  74. * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
  75. * @latch: value to convert
  76. * @evt: pointer to clock event device descriptor
  77. *
  78. * Math helper, returns latch value converted to nanoseconds (bound checked)
  79. */
  80. u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
  81. {
  82. return cev_delta2ns(latch, evt, false);
  83. }
  84. EXPORT_SYMBOL_GPL(clockevent_delta2ns);
  85. static int __clockevents_switch_state(struct clock_event_device *dev,
  86. enum clock_event_state state)
  87. {
  88. if (dev->features & CLOCK_EVT_FEAT_DUMMY)
  89. return 0;
  90. /* Transition with new state-specific callbacks */
  91. switch (state) {
  92. case CLOCK_EVT_STATE_DETACHED:
  93. /* The clockevent device is getting replaced. Shut it down. */
  94. case CLOCK_EVT_STATE_SHUTDOWN:
  95. if (dev->set_state_shutdown)
  96. return dev->set_state_shutdown(dev);
  97. return 0;
  98. case CLOCK_EVT_STATE_PERIODIC:
  99. /* Core internal bug */
  100. if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
  101. return -ENOSYS;
  102. if (dev->set_state_periodic)
  103. return dev->set_state_periodic(dev);
  104. return 0;
  105. case CLOCK_EVT_STATE_ONESHOT:
  106. /* Core internal bug */
  107. if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  108. return -ENOSYS;
  109. if (dev->set_state_oneshot)
  110. return dev->set_state_oneshot(dev);
  111. return 0;
  112. case CLOCK_EVT_STATE_ONESHOT_STOPPED:
  113. /* Core internal bug */
  114. if (WARN_ONCE(!clockevent_state_oneshot(dev),
  115. "Current state: %d\n",
  116. clockevent_get_state(dev)))
  117. return -EINVAL;
  118. if (dev->set_state_oneshot_stopped)
  119. return dev->set_state_oneshot_stopped(dev);
  120. else
  121. return -ENOSYS;
  122. default:
  123. return -ENOSYS;
  124. }
  125. }
  126. /**
  127. * clockevents_switch_state - set the operating state of a clock event device
  128. * @dev: device to modify
  129. * @state: new state
  130. *
  131. * Must be called with interrupts disabled !
  132. */
  133. void clockevents_switch_state(struct clock_event_device *dev,
  134. enum clock_event_state state)
  135. {
  136. if (clockevent_get_state(dev) != state) {
  137. if (__clockevents_switch_state(dev, state))
  138. return;
  139. clockevent_set_state(dev, state);
  140. /*
  141. * A nsec2cyc multiplicator of 0 is invalid and we'd crash
  142. * on it, so fix it up and emit a warning:
  143. */
  144. if (clockevent_state_oneshot(dev)) {
  145. if (unlikely(!dev->mult)) {
  146. dev->mult = 1;
  147. WARN_ON(1);
  148. }
  149. }
  150. }
  151. }
  152. /**
  153. * clockevents_shutdown - shutdown the device and clear next_event
  154. * @dev: device to shutdown
  155. */
  156. void clockevents_shutdown(struct clock_event_device *dev)
  157. {
  158. clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
  159. dev->next_event = KTIME_MAX;
  160. }
  161. /**
  162. * clockevents_tick_resume - Resume the tick device before using it again
  163. * @dev: device to resume
  164. */
  165. int clockevents_tick_resume(struct clock_event_device *dev)
  166. {
  167. int ret = 0;
  168. if (dev->tick_resume)
  169. ret = dev->tick_resume(dev);
  170. return ret;
  171. }
  172. #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
  173. /* Limit min_delta to a jiffie */
  174. #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
  175. /**
  176. * clockevents_increase_min_delta - raise minimum delta of a clock event device
  177. * @dev: device to increase the minimum delta
  178. *
  179. * Returns 0 on success, -ETIME when the minimum delta reached the limit.
  180. */
  181. static int clockevents_increase_min_delta(struct clock_event_device *dev)
  182. {
  183. /* Nothing to do if we already reached the limit */
  184. if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
  185. printk_deferred(KERN_WARNING
  186. "CE: Reprogramming failure. Giving up\n");
  187. dev->next_event = KTIME_MAX;
  188. return -ETIME;
  189. }
  190. if (dev->min_delta_ns < 5000)
  191. dev->min_delta_ns = 5000;
  192. else
  193. dev->min_delta_ns += dev->min_delta_ns >> 1;
  194. if (dev->min_delta_ns > MIN_DELTA_LIMIT)
  195. dev->min_delta_ns = MIN_DELTA_LIMIT;
  196. printk_deferred(KERN_WARNING
  197. "CE: %s increased min_delta_ns to %llu nsec\n",
  198. dev->name ? dev->name : "?",
  199. (unsigned long long) dev->min_delta_ns);
  200. return 0;
  201. }
  202. /**
  203. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  204. * @dev: device to program
  205. *
  206. * Returns 0 on success, -ETIME when the retry loop failed.
  207. */
  208. static int clockevents_program_min_delta(struct clock_event_device *dev)
  209. {
  210. unsigned long long clc;
  211. int64_t delta;
  212. int i;
  213. for (i = 0;;) {
  214. delta = dev->min_delta_ns;
  215. dev->next_event = ktime_add_ns(ktime_get(), delta);
  216. if (clockevent_state_shutdown(dev))
  217. return 0;
  218. dev->retries++;
  219. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  220. if (dev->set_next_event((unsigned long) clc, dev) == 0)
  221. return 0;
  222. if (++i > 2) {
  223. /*
  224. * We tried 3 times to program the device with the
  225. * given min_delta_ns. Try to increase the minimum
  226. * delta, if that fails as well get out of here.
  227. */
  228. if (clockevents_increase_min_delta(dev))
  229. return -ETIME;
  230. i = 0;
  231. }
  232. }
  233. }
  234. #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  235. /**
  236. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  237. * @dev: device to program
  238. *
  239. * Returns 0 on success, -ETIME when the retry loop failed.
  240. */
  241. static int clockevents_program_min_delta(struct clock_event_device *dev)
  242. {
  243. unsigned long long clc;
  244. int64_t delta = 0;
  245. int i;
  246. for (i = 0; i < 10; i++) {
  247. delta += dev->min_delta_ns;
  248. dev->next_event = ktime_add_ns(ktime_get(), delta);
  249. if (clockevent_state_shutdown(dev))
  250. return 0;
  251. dev->retries++;
  252. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  253. if (dev->set_next_event((unsigned long) clc, dev) == 0)
  254. return 0;
  255. }
  256. return -ETIME;
  257. }
  258. #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  259. /**
  260. * clockevents_program_event - Reprogram the clock event device.
  261. * @dev: device to program
  262. * @expires: absolute expiry time (monotonic clock)
  263. * @force: program minimum delay if expires can not be set
  264. *
  265. * Returns 0 on success, -ETIME when the event is in the past.
  266. */
  267. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  268. bool force)
  269. {
  270. unsigned long long clc;
  271. int64_t delta;
  272. int rc;
  273. if (unlikely(expires < 0)) {
  274. WARN_ON_ONCE(1);
  275. return -ETIME;
  276. }
  277. dev->next_event = expires;
  278. if (clockevent_state_shutdown(dev))
  279. return 0;
  280. /* We must be in ONESHOT state here */
  281. WARN_ONCE(!clockevent_state_oneshot(dev), "Current state: %d\n",
  282. clockevent_get_state(dev));
  283. /* Shortcut for clockevent devices that can deal with ktime. */
  284. if (dev->features & CLOCK_EVT_FEAT_KTIME)
  285. return dev->set_next_ktime(expires, dev);
  286. delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
  287. if (delta <= 0)
  288. return force ? clockevents_program_min_delta(dev) : -ETIME;
  289. delta = min(delta, (int64_t) dev->max_delta_ns);
  290. delta = max(delta, (int64_t) dev->min_delta_ns);
  291. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  292. rc = dev->set_next_event((unsigned long) clc, dev);
  293. return (rc && force) ? clockevents_program_min_delta(dev) : rc;
  294. }
  295. /*
  296. * Called after a notify add to make devices available which were
  297. * released from the notifier call.
  298. */
  299. static void clockevents_notify_released(void)
  300. {
  301. struct clock_event_device *dev;
  302. while (!list_empty(&clockevents_released)) {
  303. dev = list_entry(clockevents_released.next,
  304. struct clock_event_device, list);
  305. list_del(&dev->list);
  306. list_add(&dev->list, &clockevent_devices);
  307. tick_check_new_device(dev);
  308. }
  309. }
  310. /*
  311. * Try to install a replacement clock event device
  312. */
  313. static int clockevents_replace(struct clock_event_device *ced)
  314. {
  315. struct clock_event_device *dev, *newdev = NULL;
  316. list_for_each_entry(dev, &clockevent_devices, list) {
  317. if (dev == ced || !clockevent_state_detached(dev))
  318. continue;
  319. if (!tick_check_replacement(newdev, dev))
  320. continue;
  321. if (!try_module_get(dev->owner))
  322. continue;
  323. if (newdev)
  324. module_put(newdev->owner);
  325. newdev = dev;
  326. }
  327. if (newdev) {
  328. tick_install_replacement(newdev);
  329. list_del_init(&ced->list);
  330. }
  331. return newdev ? 0 : -EBUSY;
  332. }
  333. /*
  334. * Called with clockevents_mutex and clockevents_lock held
  335. */
  336. static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
  337. {
  338. /* Fast track. Device is unused */
  339. if (clockevent_state_detached(ced)) {
  340. list_del_init(&ced->list);
  341. return 0;
  342. }
  343. return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
  344. }
  345. /*
  346. * SMP function call to unbind a device
  347. */
  348. static void __clockevents_unbind(void *arg)
  349. {
  350. struct ce_unbind *cu = arg;
  351. int res;
  352. raw_spin_lock(&clockevents_lock);
  353. res = __clockevents_try_unbind(cu->ce, smp_processor_id());
  354. if (res == -EAGAIN)
  355. res = clockevents_replace(cu->ce);
  356. cu->res = res;
  357. raw_spin_unlock(&clockevents_lock);
  358. }
  359. /*
  360. * Issues smp function call to unbind a per cpu device. Called with
  361. * clockevents_mutex held.
  362. */
  363. static int clockevents_unbind(struct clock_event_device *ced, int cpu)
  364. {
  365. struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
  366. smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
  367. return cu.res;
  368. }
  369. /*
  370. * Unbind a clockevents device.
  371. */
  372. int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
  373. {
  374. int ret;
  375. mutex_lock(&clockevents_mutex);
  376. ret = clockevents_unbind(ced, cpu);
  377. mutex_unlock(&clockevents_mutex);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(clockevents_unbind_device);
  381. /**
  382. * clockevents_register_device - register a clock event device
  383. * @dev: device to register
  384. */
  385. void clockevents_register_device(struct clock_event_device *dev)
  386. {
  387. unsigned long flags;
  388. /* Initialize state to DETACHED */
  389. clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
  390. if (!dev->cpumask) {
  391. WARN_ON(num_possible_cpus() > 1);
  392. dev->cpumask = cpumask_of(smp_processor_id());
  393. }
  394. if (dev->cpumask == cpu_all_mask) {
  395. WARN(1, "%s cpumask == cpu_all_mask, using cpu_possible_mask instead\n",
  396. dev->name);
  397. dev->cpumask = cpu_possible_mask;
  398. }
  399. raw_spin_lock_irqsave(&clockevents_lock, flags);
  400. list_add(&dev->list, &clockevent_devices);
  401. tick_check_new_device(dev);
  402. clockevents_notify_released();
  403. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  404. }
  405. EXPORT_SYMBOL_GPL(clockevents_register_device);
  406. static void clockevents_config(struct clock_event_device *dev, u32 freq)
  407. {
  408. u64 sec;
  409. if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  410. return;
  411. /*
  412. * Calculate the maximum number of seconds we can sleep. Limit
  413. * to 10 minutes for hardware which can program more than
  414. * 32bit ticks so we still get reasonable conversion values.
  415. */
  416. sec = dev->max_delta_ticks;
  417. do_div(sec, freq);
  418. if (!sec)
  419. sec = 1;
  420. else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
  421. sec = 600;
  422. clockevents_calc_mult_shift(dev, freq, sec);
  423. dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
  424. dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
  425. }
  426. /**
  427. * clockevents_config_and_register - Configure and register a clock event device
  428. * @dev: device to register
  429. * @freq: The clock frequency
  430. * @min_delta: The minimum clock ticks to program in oneshot mode
  431. * @max_delta: The maximum clock ticks to program in oneshot mode
  432. *
  433. * min/max_delta can be 0 for devices which do not support oneshot mode.
  434. */
  435. void clockevents_config_and_register(struct clock_event_device *dev,
  436. u32 freq, unsigned long min_delta,
  437. unsigned long max_delta)
  438. {
  439. dev->min_delta_ticks = min_delta;
  440. dev->max_delta_ticks = max_delta;
  441. clockevents_config(dev, freq);
  442. clockevents_register_device(dev);
  443. }
  444. EXPORT_SYMBOL_GPL(clockevents_config_and_register);
  445. int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
  446. {
  447. clockevents_config(dev, freq);
  448. if (clockevent_state_oneshot(dev))
  449. return clockevents_program_event(dev, dev->next_event, false);
  450. if (clockevent_state_periodic(dev))
  451. return __clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
  452. return 0;
  453. }
  454. /**
  455. * clockevents_update_freq - Update frequency and reprogram a clock event device.
  456. * @dev: device to modify
  457. * @freq: new device frequency
  458. *
  459. * Reconfigure and reprogram a clock event device in oneshot
  460. * mode. Must be called on the cpu for which the device delivers per
  461. * cpu timer events. If called for the broadcast device the core takes
  462. * care of serialization.
  463. *
  464. * Returns 0 on success, -ETIME when the event is in the past.
  465. */
  466. int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
  467. {
  468. unsigned long flags;
  469. int ret;
  470. local_irq_save(flags);
  471. ret = tick_broadcast_update_freq(dev, freq);
  472. if (ret == -ENODEV)
  473. ret = __clockevents_update_freq(dev, freq);
  474. local_irq_restore(flags);
  475. return ret;
  476. }
  477. /*
  478. * Noop handler when we shut down an event device
  479. */
  480. void clockevents_handle_noop(struct clock_event_device *dev)
  481. {
  482. }
  483. /**
  484. * clockevents_exchange_device - release and request clock devices
  485. * @old: device to release (can be NULL)
  486. * @new: device to request (can be NULL)
  487. *
  488. * Called from various tick functions with clockevents_lock held and
  489. * interrupts disabled.
  490. */
  491. void clockevents_exchange_device(struct clock_event_device *old,
  492. struct clock_event_device *new)
  493. {
  494. /*
  495. * Caller releases a clock event device. We queue it into the
  496. * released list and do a notify add later.
  497. */
  498. if (old) {
  499. module_put(old->owner);
  500. clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED);
  501. list_del(&old->list);
  502. list_add(&old->list, &clockevents_released);
  503. }
  504. if (new) {
  505. BUG_ON(!clockevent_state_detached(new));
  506. clockevents_shutdown(new);
  507. }
  508. }
  509. /**
  510. * clockevents_suspend - suspend clock devices
  511. */
  512. void clockevents_suspend(void)
  513. {
  514. struct clock_event_device *dev;
  515. list_for_each_entry_reverse(dev, &clockevent_devices, list)
  516. if (dev->suspend && !clockevent_state_detached(dev))
  517. dev->suspend(dev);
  518. }
  519. /**
  520. * clockevents_resume - resume clock devices
  521. */
  522. void clockevents_resume(void)
  523. {
  524. struct clock_event_device *dev;
  525. list_for_each_entry(dev, &clockevent_devices, list)
  526. if (dev->resume && !clockevent_state_detached(dev))
  527. dev->resume(dev);
  528. }
  529. #ifdef CONFIG_HOTPLUG_CPU
  530. /**
  531. * tick_cleanup_dead_cpu - Cleanup the tick and clockevents of a dead cpu
  532. */
  533. void tick_cleanup_dead_cpu(int cpu)
  534. {
  535. struct clock_event_device *dev, *tmp;
  536. unsigned long flags;
  537. raw_spin_lock_irqsave(&clockevents_lock, flags);
  538. tick_shutdown_broadcast_oneshot(cpu);
  539. tick_shutdown_broadcast(cpu);
  540. tick_shutdown(cpu);
  541. /*
  542. * Unregister the clock event devices which were
  543. * released from the users in the notify chain.
  544. */
  545. list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
  546. list_del(&dev->list);
  547. /*
  548. * Now check whether the CPU has left unused per cpu devices
  549. */
  550. list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
  551. if (cpumask_test_cpu(cpu, dev->cpumask) &&
  552. cpumask_weight(dev->cpumask) == 1 &&
  553. !tick_is_broadcast_device(dev)) {
  554. BUG_ON(!clockevent_state_detached(dev));
  555. list_del(&dev->list);
  556. }
  557. }
  558. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  559. }
  560. #endif
  561. #ifdef CONFIG_SYSFS
  562. static struct bus_type clockevents_subsys = {
  563. .name = "clockevents",
  564. .dev_name = "clockevent",
  565. };
  566. static DEFINE_PER_CPU(struct device, tick_percpu_dev);
  567. static struct tick_device *tick_get_tick_dev(struct device *dev);
  568. static ssize_t sysfs_show_current_tick_dev(struct device *dev,
  569. struct device_attribute *attr,
  570. char *buf)
  571. {
  572. struct tick_device *td;
  573. ssize_t count = 0;
  574. raw_spin_lock_irq(&clockevents_lock);
  575. td = tick_get_tick_dev(dev);
  576. if (td && td->evtdev)
  577. count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
  578. raw_spin_unlock_irq(&clockevents_lock);
  579. return count;
  580. }
  581. static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
  582. /* We don't support the abomination of removable broadcast devices */
  583. static ssize_t sysfs_unbind_tick_dev(struct device *dev,
  584. struct device_attribute *attr,
  585. const char *buf, size_t count)
  586. {
  587. char name[CS_NAME_LEN];
  588. ssize_t ret = sysfs_get_uname(buf, name, count);
  589. struct clock_event_device *ce;
  590. if (ret < 0)
  591. return ret;
  592. ret = -ENODEV;
  593. mutex_lock(&clockevents_mutex);
  594. raw_spin_lock_irq(&clockevents_lock);
  595. list_for_each_entry(ce, &clockevent_devices, list) {
  596. if (!strcmp(ce->name, name)) {
  597. ret = __clockevents_try_unbind(ce, dev->id);
  598. break;
  599. }
  600. }
  601. raw_spin_unlock_irq(&clockevents_lock);
  602. /*
  603. * We hold clockevents_mutex, so ce can't go away
  604. */
  605. if (ret == -EAGAIN)
  606. ret = clockevents_unbind(ce, dev->id);
  607. mutex_unlock(&clockevents_mutex);
  608. return ret ? ret : count;
  609. }
  610. static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
  611. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  612. static struct device tick_bc_dev = {
  613. .init_name = "broadcast",
  614. .id = 0,
  615. .bus = &clockevents_subsys,
  616. };
  617. static struct tick_device *tick_get_tick_dev(struct device *dev)
  618. {
  619. return dev == &tick_bc_dev ? tick_get_broadcast_device() :
  620. &per_cpu(tick_cpu_device, dev->id);
  621. }
  622. static __init int tick_broadcast_init_sysfs(void)
  623. {
  624. int err = device_register(&tick_bc_dev);
  625. if (!err)
  626. err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
  627. return err;
  628. }
  629. #else
  630. static struct tick_device *tick_get_tick_dev(struct device *dev)
  631. {
  632. return &per_cpu(tick_cpu_device, dev->id);
  633. }
  634. static inline int tick_broadcast_init_sysfs(void) { return 0; }
  635. #endif
  636. static int __init tick_init_sysfs(void)
  637. {
  638. int cpu;
  639. for_each_possible_cpu(cpu) {
  640. struct device *dev = &per_cpu(tick_percpu_dev, cpu);
  641. int err;
  642. dev->id = cpu;
  643. dev->bus = &clockevents_subsys;
  644. err = device_register(dev);
  645. if (!err)
  646. err = device_create_file(dev, &dev_attr_current_device);
  647. if (!err)
  648. err = device_create_file(dev, &dev_attr_unbind_device);
  649. if (err)
  650. return err;
  651. }
  652. return tick_broadcast_init_sysfs();
  653. }
  654. static int __init clockevents_init_sysfs(void)
  655. {
  656. int err = subsys_system_register(&clockevents_subsys, NULL);
  657. if (!err)
  658. err = tick_init_sysfs();
  659. return err;
  660. }
  661. device_initcall(clockevents_init_sysfs);
  662. #endif /* SYSFS */