clockevents.c 20 KB

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