clockevents.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. do_div(clc, evt->mult);
  39. if (clc < 1000)
  40. clc = 1000;
  41. if (clc > LONG_MAX)
  42. clc = LONG_MAX;
  43. return (unsigned long) clc;
  44. }
  45. /**
  46. * clockevents_set_mode - set the operating mode of a clock event device
  47. * @dev: device to modify
  48. * @mode: new mode
  49. *
  50. * Must be called with interrupts disabled !
  51. */
  52. void clockevents_set_mode(struct clock_event_device *dev,
  53. enum clock_event_mode mode)
  54. {
  55. if (dev->mode != mode) {
  56. dev->set_mode(mode, dev);
  57. dev->mode = mode;
  58. }
  59. }
  60. /**
  61. * clockevents_program_event - Reprogram the clock event device.
  62. * @expires: absolute expiry time (monotonic clock)
  63. *
  64. * Returns 0 on success, -ETIME when the event is in the past.
  65. */
  66. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  67. ktime_t now)
  68. {
  69. unsigned long long clc;
  70. int64_t delta;
  71. if (unlikely(expires.tv64 < 0)) {
  72. WARN_ON_ONCE(1);
  73. return -ETIME;
  74. }
  75. delta = ktime_to_ns(ktime_sub(expires, now));
  76. if (delta <= 0)
  77. return -ETIME;
  78. dev->next_event = expires;
  79. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  80. return 0;
  81. if (delta > dev->max_delta_ns)
  82. delta = dev->max_delta_ns;
  83. if (delta < dev->min_delta_ns)
  84. delta = dev->min_delta_ns;
  85. clc = delta * dev->mult;
  86. clc >>= dev->shift;
  87. return dev->set_next_event((unsigned long) clc, dev);
  88. }
  89. /**
  90. * clockevents_register_notifier - register a clock events change listener
  91. */
  92. int clockevents_register_notifier(struct notifier_block *nb)
  93. {
  94. int ret;
  95. spin_lock(&clockevents_lock);
  96. ret = raw_notifier_chain_register(&clockevents_chain, nb);
  97. spin_unlock(&clockevents_lock);
  98. return ret;
  99. }
  100. /*
  101. * Notify about a clock event change. Called with clockevents_lock
  102. * held.
  103. */
  104. static void clockevents_do_notify(unsigned long reason, void *dev)
  105. {
  106. raw_notifier_call_chain(&clockevents_chain, reason, dev);
  107. }
  108. /*
  109. * Called after a notify add to make devices availble which were
  110. * released from the notifier call.
  111. */
  112. static void clockevents_notify_released(void)
  113. {
  114. struct clock_event_device *dev;
  115. while (!list_empty(&clockevents_released)) {
  116. dev = list_entry(clockevents_released.next,
  117. struct clock_event_device, list);
  118. list_del(&dev->list);
  119. list_add(&dev->list, &clockevent_devices);
  120. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  121. }
  122. }
  123. /**
  124. * clockevents_register_device - register a clock event device
  125. * @dev: device to register
  126. */
  127. void clockevents_register_device(struct clock_event_device *dev)
  128. {
  129. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  130. spin_lock(&clockevents_lock);
  131. list_add(&dev->list, &clockevent_devices);
  132. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  133. clockevents_notify_released();
  134. spin_unlock(&clockevents_lock);
  135. }
  136. /*
  137. * Noop handler when we shut down an event device
  138. */
  139. static void clockevents_handle_noop(struct clock_event_device *dev)
  140. {
  141. }
  142. /**
  143. * clockevents_exchange_device - release and request clock devices
  144. * @old: device to release (can be NULL)
  145. * @new: device to request (can be NULL)
  146. *
  147. * Called from the notifier chain. clockevents_lock is held already
  148. */
  149. void clockevents_exchange_device(struct clock_event_device *old,
  150. struct clock_event_device *new)
  151. {
  152. unsigned long flags;
  153. local_irq_save(flags);
  154. /*
  155. * Caller releases a clock event device. We queue it into the
  156. * released list and do a notify add later.
  157. */
  158. if (old) {
  159. old->event_handler = clockevents_handle_noop;
  160. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  161. list_del(&old->list);
  162. list_add(&old->list, &clockevents_released);
  163. }
  164. if (new) {
  165. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  166. clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN);
  167. }
  168. local_irq_restore(flags);
  169. }
  170. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  171. /**
  172. * clockevents_notify - notification about relevant events
  173. */
  174. void clockevents_notify(unsigned long reason, void *arg)
  175. {
  176. spin_lock(&clockevents_lock);
  177. clockevents_do_notify(reason, arg);
  178. switch (reason) {
  179. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  180. /*
  181. * Unregister the clock event devices which were
  182. * released from the users in the notify chain.
  183. */
  184. while (!list_empty(&clockevents_released)) {
  185. struct clock_event_device *dev;
  186. dev = list_entry(clockevents_released.next,
  187. struct clock_event_device, list);
  188. list_del(&dev->list);
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. spin_unlock(&clockevents_lock);
  195. }
  196. EXPORT_SYMBOL_GPL(clockevents_notify);
  197. #endif