clockevents.c 20 KB

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