clockevents.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/notifier.h>
  18. #include <linux/smp.h>
  19. #include <linux/sysdev.h>
  20. /* The registered clock event devices */
  21. static LIST_HEAD(clockevent_devices);
  22. static LIST_HEAD(clockevents_released);
  23. /* Notification for clock events */
  24. static RAW_NOTIFIER_HEAD(clockevents_chain);
  25. /* Protection for the above */
  26. static DEFINE_SPINLOCK(clockevents_lock);
  27. /**
  28. * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
  29. * @latch: value to convert
  30. * @evt: pointer to clock event device descriptor
  31. *
  32. * Math helper, returns latch value converted to nanoseconds (bound checked)
  33. */
  34. unsigned long clockevent_delta2ns(unsigned long latch,
  35. struct clock_event_device *evt)
  36. {
  37. u64 clc = ((u64) latch << evt->shift);
  38. if (unlikely(!evt->mult)) {
  39. evt->mult = 1;
  40. WARN_ON(1);
  41. }
  42. do_div(clc, evt->mult);
  43. if (clc < 1000)
  44. clc = 1000;
  45. if (clc > LONG_MAX)
  46. clc = LONG_MAX;
  47. return (unsigned long) clc;
  48. }
  49. /**
  50. * clockevents_set_mode - set the operating mode of a clock event device
  51. * @dev: device to modify
  52. * @mode: new mode
  53. *
  54. * Must be called with interrupts disabled !
  55. */
  56. void clockevents_set_mode(struct clock_event_device *dev,
  57. enum clock_event_mode mode)
  58. {
  59. if (dev->mode != mode) {
  60. dev->set_mode(mode, dev);
  61. dev->mode = mode;
  62. /*
  63. * A nsec2cyc multiplicator of 0 is invalid and we'd crash
  64. * on it, so fix it up and emit a warning:
  65. */
  66. if (mode == CLOCK_EVT_MODE_ONESHOT) {
  67. if (unlikely(!dev->mult)) {
  68. dev->mult = 1;
  69. WARN_ON(1);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * clockevents_shutdown - shutdown the device and clear next_event
  76. * @dev: device to shutdown
  77. */
  78. void clockevents_shutdown(struct clock_event_device *dev)
  79. {
  80. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  81. dev->next_event.tv64 = KTIME_MAX;
  82. }
  83. /**
  84. * clockevents_program_event - Reprogram the clock event device.
  85. * @expires: absolute expiry time (monotonic clock)
  86. *
  87. * Returns 0 on success, -ETIME when the event is in the past.
  88. */
  89. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  90. ktime_t now)
  91. {
  92. unsigned long long clc;
  93. int64_t delta;
  94. if (unlikely(expires.tv64 < 0)) {
  95. WARN_ON_ONCE(1);
  96. return -ETIME;
  97. }
  98. delta = ktime_to_ns(ktime_sub(expires, now));
  99. if (delta <= 0)
  100. return -ETIME;
  101. dev->next_event = expires;
  102. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  103. return 0;
  104. if (delta > dev->max_delta_ns)
  105. delta = dev->max_delta_ns;
  106. if (delta < dev->min_delta_ns)
  107. delta = dev->min_delta_ns;
  108. clc = delta * dev->mult;
  109. clc >>= dev->shift;
  110. return dev->set_next_event((unsigned long) clc, dev);
  111. }
  112. /**
  113. * clockevents_register_notifier - register a clock events change listener
  114. */
  115. int clockevents_register_notifier(struct notifier_block *nb)
  116. {
  117. int ret;
  118. spin_lock(&clockevents_lock);
  119. ret = raw_notifier_chain_register(&clockevents_chain, nb);
  120. spin_unlock(&clockevents_lock);
  121. return ret;
  122. }
  123. /*
  124. * Notify about a clock event change. Called with clockevents_lock
  125. * held.
  126. */
  127. static void clockevents_do_notify(unsigned long reason, void *dev)
  128. {
  129. raw_notifier_call_chain(&clockevents_chain, reason, dev);
  130. }
  131. /*
  132. * Called after a notify add to make devices available which were
  133. * released from the notifier call.
  134. */
  135. static void clockevents_notify_released(void)
  136. {
  137. struct clock_event_device *dev;
  138. while (!list_empty(&clockevents_released)) {
  139. dev = list_entry(clockevents_released.next,
  140. struct clock_event_device, list);
  141. list_del(&dev->list);
  142. list_add(&dev->list, &clockevent_devices);
  143. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  144. }
  145. }
  146. /**
  147. * clockevents_register_device - register a clock event device
  148. * @dev: device to register
  149. */
  150. void clockevents_register_device(struct clock_event_device *dev)
  151. {
  152. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  153. BUG_ON(!dev->cpumask);
  154. spin_lock(&clockevents_lock);
  155. list_add(&dev->list, &clockevent_devices);
  156. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  157. clockevents_notify_released();
  158. spin_unlock(&clockevents_lock);
  159. }
  160. /*
  161. * Noop handler when we shut down an event device
  162. */
  163. void clockevents_handle_noop(struct clock_event_device *dev)
  164. {
  165. }
  166. /**
  167. * clockevents_exchange_device - release and request clock devices
  168. * @old: device to release (can be NULL)
  169. * @new: device to request (can be NULL)
  170. *
  171. * Called from the notifier chain. clockevents_lock is held already
  172. */
  173. void clockevents_exchange_device(struct clock_event_device *old,
  174. struct clock_event_device *new)
  175. {
  176. unsigned long flags;
  177. local_irq_save(flags);
  178. /*
  179. * Caller releases a clock event device. We queue it into the
  180. * released list and do a notify add later.
  181. */
  182. if (old) {
  183. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  184. list_del(&old->list);
  185. list_add(&old->list, &clockevents_released);
  186. }
  187. if (new) {
  188. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  189. clockevents_shutdown(new);
  190. }
  191. local_irq_restore(flags);
  192. }
  193. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  194. /**
  195. * clockevents_notify - notification about relevant events
  196. */
  197. void clockevents_notify(unsigned long reason, void *arg)
  198. {
  199. struct list_head *node, *tmp;
  200. spin_lock(&clockevents_lock);
  201. clockevents_do_notify(reason, arg);
  202. switch (reason) {
  203. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  204. /*
  205. * Unregister the clock event devices which were
  206. * released from the users in the notify chain.
  207. */
  208. list_for_each_safe(node, tmp, &clockevents_released)
  209. list_del(node);
  210. break;
  211. default:
  212. break;
  213. }
  214. spin_unlock(&clockevents_lock);
  215. }
  216. EXPORT_SYMBOL_GPL(clockevents_notify);
  217. #endif