clockevents.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "tick-internal.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_RAW_SPINLOCK(clockevents_lock);
  27. static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
  28. bool ismax)
  29. {
  30. u64 clc = (u64) latch << evt->shift;
  31. u64 rnd;
  32. if (unlikely(!evt->mult)) {
  33. evt->mult = 1;
  34. WARN_ON(1);
  35. }
  36. rnd = (u64) evt->mult - 1;
  37. /*
  38. * Upper bound sanity check. If the backwards conversion is
  39. * not equal latch, we know that the above shift overflowed.
  40. */
  41. if ((clc >> evt->shift) != (u64)latch)
  42. clc = ~0ULL;
  43. /*
  44. * Scaled math oddities:
  45. *
  46. * For mult <= (1 << shift) we can safely add mult - 1 to
  47. * prevent integer rounding loss. So the backwards conversion
  48. * from nsec to device ticks will be correct.
  49. *
  50. * For mult > (1 << shift), i.e. device frequency is > 1GHz we
  51. * need to be careful. Adding mult - 1 will result in a value
  52. * which when converted back to device ticks can be larger
  53. * than latch by up to (mult - 1) >> shift. For the min_delta
  54. * calculation we still want to apply this in order to stay
  55. * above the minimum device ticks limit. For the upper limit
  56. * we would end up with a latch value larger than the upper
  57. * limit of the device, so we omit the add to stay below the
  58. * device upper boundary.
  59. *
  60. * Also omit the add if it would overflow the u64 boundary.
  61. */
  62. if ((~0ULL - clc > rnd) &&
  63. (!ismax || evt->mult <= (1U << evt->shift)))
  64. clc += rnd;
  65. do_div(clc, evt->mult);
  66. /* Deltas less than 1usec are pointless noise */
  67. return clc > 1000 ? clc : 1000;
  68. }
  69. /**
  70. * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
  71. * @latch: value to convert
  72. * @evt: pointer to clock event device descriptor
  73. *
  74. * Math helper, returns latch value converted to nanoseconds (bound checked)
  75. */
  76. u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
  77. {
  78. return cev_delta2ns(latch, evt, false);
  79. }
  80. EXPORT_SYMBOL_GPL(clockevent_delta2ns);
  81. /**
  82. * clockevents_set_mode - set the operating mode of a clock event device
  83. * @dev: device to modify
  84. * @mode: new mode
  85. *
  86. * Must be called with interrupts disabled !
  87. */
  88. void clockevents_set_mode(struct clock_event_device *dev,
  89. enum clock_event_mode mode)
  90. {
  91. if (dev->mode != mode) {
  92. dev->set_mode(mode, dev);
  93. dev->mode = mode;
  94. /*
  95. * A nsec2cyc multiplicator of 0 is invalid and we'd crash
  96. * on it, so fix it up and emit a warning:
  97. */
  98. if (mode == CLOCK_EVT_MODE_ONESHOT) {
  99. if (unlikely(!dev->mult)) {
  100. dev->mult = 1;
  101. WARN_ON(1);
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * clockevents_shutdown - shutdown the device and clear next_event
  108. * @dev: device to shutdown
  109. */
  110. void clockevents_shutdown(struct clock_event_device *dev)
  111. {
  112. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  113. dev->next_event.tv64 = KTIME_MAX;
  114. }
  115. #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
  116. /* Limit min_delta to a jiffie */
  117. #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
  118. /**
  119. * clockevents_increase_min_delta - raise minimum delta of a clock event device
  120. * @dev: device to increase the minimum delta
  121. *
  122. * Returns 0 on success, -ETIME when the minimum delta reached the limit.
  123. */
  124. static int clockevents_increase_min_delta(struct clock_event_device *dev)
  125. {
  126. /* Nothing to do if we already reached the limit */
  127. if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
  128. printk_deferred(KERN_WARNING
  129. "CE: Reprogramming failure. Giving up\n");
  130. dev->next_event.tv64 = KTIME_MAX;
  131. return -ETIME;
  132. }
  133. if (dev->min_delta_ns < 5000)
  134. dev->min_delta_ns = 5000;
  135. else
  136. dev->min_delta_ns += dev->min_delta_ns >> 1;
  137. if (dev->min_delta_ns > MIN_DELTA_LIMIT)
  138. dev->min_delta_ns = MIN_DELTA_LIMIT;
  139. printk_deferred(KERN_WARNING
  140. "CE: %s increased min_delta_ns to %llu nsec\n",
  141. dev->name ? dev->name : "?",
  142. (unsigned long long) dev->min_delta_ns);
  143. return 0;
  144. }
  145. /**
  146. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  147. * @dev: device to program
  148. *
  149. * Returns 0 on success, -ETIME when the retry loop failed.
  150. */
  151. static int clockevents_program_min_delta(struct clock_event_device *dev)
  152. {
  153. unsigned long long clc;
  154. int64_t delta;
  155. int i;
  156. for (i = 0;;) {
  157. delta = dev->min_delta_ns;
  158. dev->next_event = ktime_add_ns(ktime_get(), delta);
  159. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  160. return 0;
  161. dev->retries++;
  162. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  163. if (dev->set_next_event((unsigned long) clc, dev) == 0)
  164. return 0;
  165. if (++i > 2) {
  166. /*
  167. * We tried 3 times to program the device with the
  168. * given min_delta_ns. Try to increase the minimum
  169. * delta, if that fails as well get out of here.
  170. */
  171. if (clockevents_increase_min_delta(dev))
  172. return -ETIME;
  173. i = 0;
  174. }
  175. }
  176. }
  177. #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  178. /**
  179. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  180. * @dev: device to program
  181. *
  182. * Returns 0 on success, -ETIME when the retry loop failed.
  183. */
  184. static int clockevents_program_min_delta(struct clock_event_device *dev)
  185. {
  186. unsigned long long clc;
  187. int64_t delta;
  188. delta = dev->min_delta_ns;
  189. dev->next_event = ktime_add_ns(ktime_get(), delta);
  190. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  191. return 0;
  192. dev->retries++;
  193. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  194. return dev->set_next_event((unsigned long) clc, dev);
  195. }
  196. #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  197. /**
  198. * clockevents_program_event - Reprogram the clock event device.
  199. * @dev: device to program
  200. * @expires: absolute expiry time (monotonic clock)
  201. * @force: program minimum delay if expires can not be set
  202. *
  203. * Returns 0 on success, -ETIME when the event is in the past.
  204. */
  205. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  206. bool force)
  207. {
  208. unsigned long long clc;
  209. int64_t delta;
  210. int rc;
  211. if (unlikely(expires.tv64 < 0)) {
  212. WARN_ON_ONCE(1);
  213. return -ETIME;
  214. }
  215. dev->next_event = expires;
  216. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  217. return 0;
  218. /* Shortcut for clockevent devices that can deal with ktime. */
  219. if (dev->features & CLOCK_EVT_FEAT_KTIME)
  220. return dev->set_next_ktime(expires, dev);
  221. delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
  222. if (delta <= 0)
  223. return force ? clockevents_program_min_delta(dev) : -ETIME;
  224. delta = min(delta, (int64_t) dev->max_delta_ns);
  225. delta = max(delta, (int64_t) dev->min_delta_ns);
  226. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  227. rc = dev->set_next_event((unsigned long) clc, dev);
  228. return (rc && force) ? clockevents_program_min_delta(dev) : rc;
  229. }
  230. /**
  231. * clockevents_register_notifier - register a clock events change listener
  232. */
  233. int clockevents_register_notifier(struct notifier_block *nb)
  234. {
  235. unsigned long flags;
  236. int ret;
  237. raw_spin_lock_irqsave(&clockevents_lock, flags);
  238. ret = raw_notifier_chain_register(&clockevents_chain, nb);
  239. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  240. return ret;
  241. }
  242. /*
  243. * Notify about a clock event change. Called with clockevents_lock
  244. * held.
  245. */
  246. static void clockevents_do_notify(unsigned long reason, void *dev)
  247. {
  248. raw_notifier_call_chain(&clockevents_chain, reason, dev);
  249. }
  250. /*
  251. * Called after a notify add to make devices available which were
  252. * released from the notifier call.
  253. */
  254. static void clockevents_notify_released(void)
  255. {
  256. struct clock_event_device *dev;
  257. while (!list_empty(&clockevents_released)) {
  258. dev = list_entry(clockevents_released.next,
  259. struct clock_event_device, list);
  260. list_del(&dev->list);
  261. list_add(&dev->list, &clockevent_devices);
  262. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  263. }
  264. }
  265. /**
  266. * clockevents_register_device - register a clock event device
  267. * @dev: device to register
  268. */
  269. void clockevents_register_device(struct clock_event_device *dev)
  270. {
  271. unsigned long flags;
  272. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  273. if (!dev->cpumask) {
  274. WARN_ON(num_possible_cpus() > 1);
  275. dev->cpumask = cpumask_of(smp_processor_id());
  276. }
  277. raw_spin_lock_irqsave(&clockevents_lock, flags);
  278. list_add(&dev->list, &clockevent_devices);
  279. clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
  280. clockevents_notify_released();
  281. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  282. }
  283. EXPORT_SYMBOL_GPL(clockevents_register_device);
  284. static void clockevents_config(struct clock_event_device *dev,
  285. u32 freq)
  286. {
  287. u64 sec;
  288. if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  289. return;
  290. /*
  291. * Calculate the maximum number of seconds we can sleep. Limit
  292. * to 10 minutes for hardware which can program more than
  293. * 32bit ticks so we still get reasonable conversion values.
  294. */
  295. sec = dev->max_delta_ticks;
  296. do_div(sec, freq);
  297. if (!sec)
  298. sec = 1;
  299. else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
  300. sec = 600;
  301. clockevents_calc_mult_shift(dev, freq, sec);
  302. dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
  303. dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
  304. }
  305. /**
  306. * clockevents_config_and_register - Configure and register a clock event device
  307. * @dev: device to register
  308. * @freq: The clock frequency
  309. * @min_delta: The minimum clock ticks to program in oneshot mode
  310. * @max_delta: The maximum clock ticks to program in oneshot mode
  311. *
  312. * min/max_delta can be 0 for devices which do not support oneshot mode.
  313. */
  314. void clockevents_config_and_register(struct clock_event_device *dev,
  315. u32 freq, unsigned long min_delta,
  316. unsigned long max_delta)
  317. {
  318. dev->min_delta_ticks = min_delta;
  319. dev->max_delta_ticks = max_delta;
  320. clockevents_config(dev, freq);
  321. clockevents_register_device(dev);
  322. }
  323. /**
  324. * clockevents_update_freq - Update frequency and reprogram a clock event device.
  325. * @dev: device to modify
  326. * @freq: new device frequency
  327. *
  328. * Reconfigure and reprogram a clock event device in oneshot
  329. * mode. Must be called on the cpu for which the device delivers per
  330. * cpu timer events with interrupts disabled! Returns 0 on success,
  331. * -ETIME when the event is in the past.
  332. */
  333. int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
  334. {
  335. clockevents_config(dev, freq);
  336. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  337. return 0;
  338. return clockevents_program_event(dev, dev->next_event, false);
  339. }
  340. /*
  341. * Noop handler when we shut down an event device
  342. */
  343. void clockevents_handle_noop(struct clock_event_device *dev)
  344. {
  345. }
  346. /**
  347. * clockevents_exchange_device - release and request clock devices
  348. * @old: device to release (can be NULL)
  349. * @new: device to request (can be NULL)
  350. *
  351. * Called from the notifier chain. clockevents_lock is held already
  352. */
  353. void clockevents_exchange_device(struct clock_event_device *old,
  354. struct clock_event_device *new)
  355. {
  356. unsigned long flags;
  357. local_irq_save(flags);
  358. /*
  359. * Caller releases a clock event device. We queue it into the
  360. * released list and do a notify add later.
  361. */
  362. if (old) {
  363. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  364. list_del(&old->list);
  365. list_add(&old->list, &clockevents_released);
  366. }
  367. if (new) {
  368. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  369. clockevents_shutdown(new);
  370. }
  371. local_irq_restore(flags);
  372. }
  373. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  374. /**
  375. * clockevents_notify - notification about relevant events
  376. */
  377. void clockevents_notify(unsigned long reason, void *arg)
  378. {
  379. struct clock_event_device *dev, *tmp;
  380. unsigned long flags;
  381. int cpu;
  382. raw_spin_lock_irqsave(&clockevents_lock, flags);
  383. clockevents_do_notify(reason, arg);
  384. switch (reason) {
  385. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  386. /*
  387. * Unregister the clock event devices which were
  388. * released from the users in the notify chain.
  389. */
  390. list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
  391. list_del(&dev->list);
  392. /*
  393. * Now check whether the CPU has left unused per cpu devices
  394. */
  395. cpu = *((int *)arg);
  396. list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
  397. if (cpumask_test_cpu(cpu, dev->cpumask) &&
  398. cpumask_weight(dev->cpumask) == 1 &&
  399. !tick_is_broadcast_device(dev)) {
  400. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  401. list_del(&dev->list);
  402. }
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  409. }
  410. EXPORT_SYMBOL_GPL(clockevents_notify);
  411. #endif