clockevents.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. delta = ktime_to_ns(ktime_sub(expires, now));
  72. if (delta <= 0)
  73. return -ETIME;
  74. dev->next_event = expires;
  75. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  76. return 0;
  77. if (delta > dev->max_delta_ns)
  78. delta = dev->max_delta_ns;
  79. if (delta < dev->min_delta_ns)
  80. delta = dev->min_delta_ns;
  81. clc = delta * dev->mult;
  82. clc >>= dev->shift;
  83. return dev->set_next_event((unsigned long) clc, dev);
  84. }
  85. /**
  86. * clockevents_register_notifier - register a clock events change listener
  87. */
  88. int clockevents_register_notifier(struct notifier_block *nb)
  89. {
  90. int ret;
  91. spin_lock(&clockevents_lock);
  92. ret = raw_notifier_chain_register(&clockevents_chain, nb);
  93. spin_unlock(&clockevents_lock);
  94. return ret;
  95. }
  96. /**
  97. * clockevents_unregister_notifier - unregister a clock events change listener
  98. */
  99. void clockevents_unregister_notifier(struct notifier_block *nb)
  100. {
  101. spin_lock(&clockevents_lock);
  102. raw_notifier_chain_unregister(&clockevents_chain, nb);
  103. spin_unlock(&clockevents_lock);
  104. }
  105. /*
  106. * Notify about a clock event change. Called with clockevents_lock
  107. * held.
  108. */
  109. static void clockevents_do_notify(unsigned long reason, void *dev)
  110. {
  111. raw_notifier_call_chain(&clockevents_chain, reason, dev);
  112. }
  113. /*
  114. * Called after a notify add to make devices availble which were
  115. * released from the notifier call.
  116. */
  117. static void clockevents_notify_released(void)
  118. {
  119. struct clock_event_device *dev;
  120. while (!list_empty(&clockevents_released)) {
  121. dev = list_entry(clockevents_released.next,
  122. struct clock_event_device, list);
  123. list_del(&dev->list);
  124. list_add(&dev->list, &clockevent_devices);
  125. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  126. }
  127. }
  128. /**
  129. * clockevents_register_device - register a clock event device
  130. * @dev: device to register
  131. */
  132. void clockevents_register_device(struct clock_event_device *dev)
  133. {
  134. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  135. spin_lock(&clockevents_lock);
  136. list_add(&dev->list, &clockevent_devices);
  137. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  138. clockevents_notify_released();
  139. spin_unlock(&clockevents_lock);
  140. }
  141. /*
  142. * Noop handler when we shut down an event device
  143. */
  144. static void clockevents_handle_noop(struct clock_event_device *dev)
  145. {
  146. }
  147. /**
  148. * clockevents_exchange_device - release and request clock devices
  149. * @old: device to release (can be NULL)
  150. * @new: device to request (can be NULL)
  151. *
  152. * Called from the notifier chain. clockevents_lock is held already
  153. */
  154. void clockevents_exchange_device(struct clock_event_device *old,
  155. struct clock_event_device *new)
  156. {
  157. unsigned long flags;
  158. local_irq_save(flags);
  159. /*
  160. * Caller releases a clock event device. We queue it into the
  161. * released list and do a notify add later.
  162. */
  163. if (old) {
  164. old->event_handler = clockevents_handle_noop;
  165. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  166. list_del(&old->list);
  167. list_add(&old->list, &clockevents_released);
  168. }
  169. if (new) {
  170. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  171. clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN);
  172. }
  173. local_irq_restore(flags);
  174. }
  175. /**
  176. * clockevents_request_device
  177. */
  178. struct clock_event_device *clockevents_request_device(unsigned int features,
  179. cpumask_t cpumask)
  180. {
  181. struct clock_event_device *cur, *dev = NULL;
  182. struct list_head *tmp;
  183. spin_lock(&clockevents_lock);
  184. list_for_each(tmp, &clockevent_devices) {
  185. cur = list_entry(tmp, struct clock_event_device, list);
  186. if ((cur->features & features) == features &&
  187. cpus_equal(cpumask, cur->cpumask)) {
  188. if (!dev || dev->rating < cur->rating)
  189. dev = cur;
  190. }
  191. }
  192. clockevents_exchange_device(NULL, dev);
  193. spin_unlock(&clockevents_lock);
  194. return dev;
  195. }
  196. /**
  197. * clockevents_release_device
  198. */
  199. void clockevents_release_device(struct clock_event_device *dev)
  200. {
  201. spin_lock(&clockevents_lock);
  202. clockevents_exchange_device(dev, NULL);
  203. clockevents_notify_released();
  204. spin_unlock(&clockevents_lock);
  205. }
  206. /**
  207. * clockevents_notify - notification about relevant events
  208. */
  209. void clockevents_notify(unsigned long reason, void *arg)
  210. {
  211. spin_lock(&clockevents_lock);
  212. clockevents_do_notify(reason, arg);
  213. switch (reason) {
  214. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  215. /*
  216. * Unregister the clock event devices which were
  217. * released from the users in the notify chain.
  218. */
  219. while (!list_empty(&clockevents_released)) {
  220. struct clock_event_device *dev;
  221. dev = list_entry(clockevents_released.next,
  222. struct clock_event_device, list);
  223. list_del(&dev->list);
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. spin_unlock(&clockevents_lock);
  230. }
  231. EXPORT_SYMBOL_GPL(clockevents_notify);