clockevents.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include "tick-internal.h"
  21. /* The registered clock event devices */
  22. static LIST_HEAD(clockevent_devices);
  23. static LIST_HEAD(clockevents_released);
  24. /* Notification for clock events */
  25. static RAW_NOTIFIER_HEAD(clockevents_chain);
  26. /* Protection for the above */
  27. static DEFINE_RAW_SPINLOCK(clockevents_lock);
  28. /**
  29. * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
  30. * @latch: value to convert
  31. * @evt: pointer to clock event device descriptor
  32. *
  33. * Math helper, returns latch value converted to nanoseconds (bound checked)
  34. */
  35. u64 clockevent_delta2ns(unsigned long latch, 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 > KTIME_MAX)
  46. clc = KTIME_MAX;
  47. return clc;
  48. }
  49. EXPORT_SYMBOL_GPL(clockevent_delta2ns);
  50. /**
  51. * clockevents_set_mode - set the operating mode of a clock event device
  52. * @dev: device to modify
  53. * @mode: new mode
  54. *
  55. * Must be called with interrupts disabled !
  56. */
  57. void clockevents_set_mode(struct clock_event_device *dev,
  58. enum clock_event_mode mode)
  59. {
  60. if (dev->mode != mode) {
  61. dev->set_mode(mode, dev);
  62. dev->mode = mode;
  63. /*
  64. * A nsec2cyc multiplicator of 0 is invalid and we'd crash
  65. * on it, so fix it up and emit a warning:
  66. */
  67. if (mode == CLOCK_EVT_MODE_ONESHOT) {
  68. if (unlikely(!dev->mult)) {
  69. dev->mult = 1;
  70. WARN_ON(1);
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * clockevents_shutdown - shutdown the device and clear next_event
  77. * @dev: device to shutdown
  78. */
  79. void clockevents_shutdown(struct clock_event_device *dev)
  80. {
  81. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  82. dev->next_event.tv64 = KTIME_MAX;
  83. }
  84. /**
  85. * clockevents_program_event - Reprogram the clock event device.
  86. * @expires: absolute expiry time (monotonic clock)
  87. *
  88. * Returns 0 on success, -ETIME when the event is in the past.
  89. */
  90. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  91. ktime_t now)
  92. {
  93. unsigned long long clc;
  94. int64_t delta;
  95. if (unlikely(expires.tv64 < 0)) {
  96. WARN_ON_ONCE(1);
  97. return -ETIME;
  98. }
  99. delta = ktime_to_ns(ktime_sub(expires, now));
  100. if (delta <= 0)
  101. return -ETIME;
  102. dev->next_event = expires;
  103. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  104. return 0;
  105. if (delta > dev->max_delta_ns)
  106. delta = dev->max_delta_ns;
  107. if (delta < dev->min_delta_ns)
  108. delta = dev->min_delta_ns;
  109. clc = delta * dev->mult;
  110. clc >>= dev->shift;
  111. return dev->set_next_event((unsigned long) clc, dev);
  112. }
  113. /**
  114. * clockevents_register_notifier - register a clock events change listener
  115. */
  116. int clockevents_register_notifier(struct notifier_block *nb)
  117. {
  118. unsigned long flags;
  119. int ret;
  120. raw_spin_lock_irqsave(&clockevents_lock, flags);
  121. ret = raw_notifier_chain_register(&clockevents_chain, nb);
  122. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  123. return ret;
  124. }
  125. /*
  126. * Notify about a clock event change. Called with clockevents_lock
  127. * held.
  128. */
  129. static void clockevents_do_notify(unsigned long reason, void *dev)
  130. {
  131. raw_notifier_call_chain(&clockevents_chain, reason, dev);
  132. }
  133. /*
  134. * Called after a notify add to make devices available which were
  135. * released from the notifier call.
  136. */
  137. static void clockevents_notify_released(void)
  138. {
  139. struct clock_event_device *dev;
  140. while (!list_empty(&clockevents_released)) {
  141. dev = list_entry(clockevents_released.next,
  142. struct clock_event_device, list);
  143. list_del(&dev->list);
  144. list_add(&dev->list, &clockevent_devices);
  145. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  146. }
  147. }
  148. /**
  149. * clockevents_register_device - register a clock event device
  150. * @dev: device to register
  151. */
  152. void clockevents_register_device(struct clock_event_device *dev)
  153. {
  154. unsigned long flags;
  155. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  156. if (!dev->cpumask) {
  157. WARN_ON(num_possible_cpus() > 1);
  158. dev->cpumask = cpumask_of(smp_processor_id());
  159. }
  160. raw_spin_lock_irqsave(&clockevents_lock, flags);
  161. list_add(&dev->list, &clockevent_devices);
  162. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  163. clockevents_notify_released();
  164. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  165. }
  166. EXPORT_SYMBOL_GPL(clockevents_register_device);
  167. static void clockevents_config(struct clock_event_device *dev,
  168. u32 freq)
  169. {
  170. u64 sec;
  171. if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  172. return;
  173. /*
  174. * Calculate the maximum number of seconds we can sleep. Limit
  175. * to 10 minutes for hardware which can program more than
  176. * 32bit ticks so we still get reasonable conversion values.
  177. */
  178. sec = dev->max_delta_ticks;
  179. do_div(sec, freq);
  180. if (!sec)
  181. sec = 1;
  182. else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
  183. sec = 600;
  184. clockevents_calc_mult_shift(dev, freq, sec);
  185. dev->min_delta_ns = clockevent_delta2ns(dev->min_delta_ticks, dev);
  186. dev->max_delta_ns = clockevent_delta2ns(dev->max_delta_ticks, dev);
  187. }
  188. /**
  189. * clockevents_config_and_register - Configure and register a clock event device
  190. * @dev: device to register
  191. * @freq: The clock frequency
  192. * @min_delta: The minimum clock ticks to program in oneshot mode
  193. * @max_delta: The maximum clock ticks to program in oneshot mode
  194. *
  195. * min/max_delta can be 0 for devices which do not support oneshot mode.
  196. */
  197. void clockevents_config_and_register(struct clock_event_device *dev,
  198. u32 freq, unsigned long min_delta,
  199. unsigned long max_delta)
  200. {
  201. dev->min_delta_ticks = min_delta;
  202. dev->max_delta_ticks = max_delta;
  203. clockevents_config(dev, freq);
  204. clockevents_register_device(dev);
  205. }
  206. /**
  207. * clockevents_update_freq - Update frequency and reprogram a clock event device.
  208. * @dev: device to modify
  209. * @freq: new device frequency
  210. *
  211. * Reconfigure and reprogram a clock event device in oneshot
  212. * mode. Must be called on the cpu for which the device delivers per
  213. * cpu timer events with interrupts disabled! Returns 0 on success,
  214. * -ETIME when the event is in the past.
  215. */
  216. int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
  217. {
  218. clockevents_config(dev, freq);
  219. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  220. return 0;
  221. return clockevents_program_event(dev, dev->next_event, ktime_get());
  222. }
  223. /*
  224. * Noop handler when we shut down an event device
  225. */
  226. void clockevents_handle_noop(struct clock_event_device *dev)
  227. {
  228. }
  229. /**
  230. * clockevents_exchange_device - release and request clock devices
  231. * @old: device to release (can be NULL)
  232. * @new: device to request (can be NULL)
  233. *
  234. * Called from the notifier chain. clockevents_lock is held already
  235. */
  236. void clockevents_exchange_device(struct clock_event_device *old,
  237. struct clock_event_device *new)
  238. {
  239. unsigned long flags;
  240. local_irq_save(flags);
  241. /*
  242. * Caller releases a clock event device. We queue it into the
  243. * released list and do a notify add later.
  244. */
  245. if (old) {
  246. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  247. list_del(&old->list);
  248. list_add(&old->list, &clockevents_released);
  249. }
  250. if (new) {
  251. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  252. clockevents_shutdown(new);
  253. }
  254. local_irq_restore(flags);
  255. }
  256. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  257. /**
  258. * clockevents_notify - notification about relevant events
  259. */
  260. void clockevents_notify(unsigned long reason, void *arg)
  261. {
  262. struct clock_event_device *dev, *tmp;
  263. unsigned long flags;
  264. int cpu;
  265. raw_spin_lock_irqsave(&clockevents_lock, flags);
  266. clockevents_do_notify(reason, arg);
  267. switch (reason) {
  268. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  269. /*
  270. * Unregister the clock event devices which were
  271. * released from the users in the notify chain.
  272. */
  273. list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
  274. list_del(&dev->list);
  275. /*
  276. * Now check whether the CPU has left unused per cpu devices
  277. */
  278. cpu = *((int *)arg);
  279. list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
  280. if (cpumask_test_cpu(cpu, dev->cpumask) &&
  281. cpumask_weight(dev->cpumask) == 1 &&
  282. !tick_is_broadcast_device(dev)) {
  283. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  284. list_del(&dev->list);
  285. }
  286. }
  287. break;
  288. default:
  289. break;
  290. }
  291. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  292. }
  293. EXPORT_SYMBOL_GPL(clockevents_notify);
  294. #endif