2 * linux/kernel/time/clockevents.c
4 * This file contains functions which manage clock event devices.
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
10 * This code is licenced under the GPL version 2. For details see
11 * kernel-base/COPYING.
14 #include <linux/clockchips.h>
15 #include <linux/hrtimer.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/smp.h>
20 #include "tick-internal.h"
22 /* The registered clock event devices */
23 static LIST_HEAD(clockevent_devices);
24 static LIST_HEAD(clockevents_released);
25 /* Protection for the above */
26 static DEFINE_RAW_SPINLOCK(clockevents_lock);
28 static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
31 u64 clc = (u64) latch << evt->shift;
34 if (unlikely(!evt->mult)) {
38 rnd = (u64) evt->mult - 1;
41 * Upper bound sanity check. If the backwards conversion is
42 * not equal latch, we know that the above shift overflowed.
44 if ((clc >> evt->shift) != (u64)latch)
48 * Scaled math oddities:
50 * For mult <= (1 << shift) we can safely add mult - 1 to
51 * prevent integer rounding loss. So the backwards conversion
52 * from nsec to device ticks will be correct.
54 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
55 * need to be careful. Adding mult - 1 will result in a value
56 * which when converted back to device ticks can be larger
57 * than latch by up to (mult - 1) >> shift. For the min_delta
58 * calculation we still want to apply this in order to stay
59 * above the minimum device ticks limit. For the upper limit
60 * we would end up with a latch value larger than the upper
61 * limit of the device, so we omit the add to stay below the
62 * device upper boundary.
64 * Also omit the add if it would overflow the u64 boundary.
66 if ((~0ULL - clc > rnd) &&
67 (!ismax || evt->mult <= (1U << evt->shift)))
70 do_div(clc, evt->mult);
72 /* Deltas less than 1usec are pointless noise */
73 return clc > 1000 ? clc : 1000;
77 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
78 * @latch: value to convert
79 * @evt: pointer to clock event device descriptor
81 * Math helper, returns latch value converted to nanoseconds (bound checked)
83 u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
85 return cev_delta2ns(latch, evt, false);
87 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
90 * clockevents_set_mode - set the operating mode of a clock event device
91 * @dev: device to modify
94 * Must be called with interrupts disabled !
96 void clockevents_set_mode(struct clock_event_device *dev,
97 enum clock_event_mode mode)
99 if (dev->mode != mode) {
100 dev->set_mode(mode, dev);
104 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
105 * on it, so fix it up and emit a warning:
107 if (mode == CLOCK_EVT_MODE_ONESHOT) {
108 if (unlikely(!dev->mult)) {
117 * clockevents_shutdown - shutdown the device and clear next_event
118 * @dev: device to shutdown
120 void clockevents_shutdown(struct clock_event_device *dev)
122 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
123 dev->next_event.tv64 = KTIME_MAX;
126 #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
128 /* Limit min_delta to a jiffie */
129 #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
132 * clockevents_increase_min_delta - raise minimum delta of a clock event device
133 * @dev: device to increase the minimum delta
135 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
137 static int clockevents_increase_min_delta(struct clock_event_device *dev)
139 /* Nothing to do if we already reached the limit */
140 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
141 printk_deferred(KERN_WARNING
142 "CE: Reprogramming failure. Giving up\n");
143 dev->next_event.tv64 = KTIME_MAX;
147 if (dev->min_delta_ns < 5000)
148 dev->min_delta_ns = 5000;
150 dev->min_delta_ns += dev->min_delta_ns >> 1;
152 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
153 dev->min_delta_ns = MIN_DELTA_LIMIT;
155 printk_deferred(KERN_WARNING
156 "CE: %s increased min_delta_ns to %llu nsec\n",
157 dev->name ? dev->name : "?",
158 (unsigned long long) dev->min_delta_ns);
163 * clockevents_program_min_delta - Set clock event device to the minimum delay.
164 * @dev: device to program
166 * Returns 0 on success, -ETIME when the retry loop failed.
168 static int clockevents_program_min_delta(struct clock_event_device *dev)
170 unsigned long long clc;
175 delta = dev->min_delta_ns;
176 dev->next_event = ktime_add_ns(ktime_get(), delta);
178 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
182 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
183 if (dev->set_next_event((unsigned long) clc, dev) == 0)
188 * We tried 3 times to program the device with the
189 * given min_delta_ns. Try to increase the minimum
190 * delta, if that fails as well get out of here.
192 if (clockevents_increase_min_delta(dev))
199 #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
202 * clockevents_program_min_delta - Set clock event device to the minimum delay.
203 * @dev: device to program
205 * Returns 0 on success, -ETIME when the retry loop failed.
207 static int clockevents_program_min_delta(struct clock_event_device *dev)
209 unsigned long long clc;
212 delta = dev->min_delta_ns;
213 dev->next_event = ktime_add_ns(ktime_get(), delta);
215 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
219 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
220 return dev->set_next_event((unsigned long) clc, dev);
223 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
226 * clockevents_program_event - Reprogram the clock event device.
227 * @dev: device to program
228 * @expires: absolute expiry time (monotonic clock)
229 * @force: program minimum delay if expires can not be set
231 * Returns 0 on success, -ETIME when the event is in the past.
233 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
236 unsigned long long clc;
240 if (unlikely(expires.tv64 < 0)) {
245 dev->next_event = expires;
247 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
250 /* Shortcut for clockevent devices that can deal with ktime. */
251 if (dev->features & CLOCK_EVT_FEAT_KTIME)
252 return dev->set_next_ktime(expires, dev);
254 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
256 return force ? clockevents_program_min_delta(dev) : -ETIME;
258 delta = min(delta, (int64_t) dev->max_delta_ns);
259 delta = max(delta, (int64_t) dev->min_delta_ns);
261 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
262 rc = dev->set_next_event((unsigned long) clc, dev);
264 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
268 * Called after a notify add to make devices available which were
269 * released from the notifier call.
271 static void clockevents_notify_released(void)
273 struct clock_event_device *dev;
275 while (!list_empty(&clockevents_released)) {
276 dev = list_entry(clockevents_released.next,
277 struct clock_event_device, list);
278 list_del(&dev->list);
279 list_add(&dev->list, &clockevent_devices);
280 tick_check_new_device(dev);
285 * clockevents_register_device - register a clock event device
286 * @dev: device to register
288 void clockevents_register_device(struct clock_event_device *dev)
292 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
294 WARN_ON(num_possible_cpus() > 1);
295 dev->cpumask = cpumask_of(smp_processor_id());
298 raw_spin_lock_irqsave(&clockevents_lock, flags);
300 list_add(&dev->list, &clockevent_devices);
301 tick_check_new_device(dev);
302 clockevents_notify_released();
304 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
306 EXPORT_SYMBOL_GPL(clockevents_register_device);
308 void clockevents_config(struct clock_event_device *dev, u32 freq)
312 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
316 * Calculate the maximum number of seconds we can sleep. Limit
317 * to 10 minutes for hardware which can program more than
318 * 32bit ticks so we still get reasonable conversion values.
320 sec = dev->max_delta_ticks;
324 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
327 clockevents_calc_mult_shift(dev, freq, sec);
328 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
329 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
333 * clockevents_config_and_register - Configure and register a clock event device
334 * @dev: device to register
335 * @freq: The clock frequency
336 * @min_delta: The minimum clock ticks to program in oneshot mode
337 * @max_delta: The maximum clock ticks to program in oneshot mode
339 * min/max_delta can be 0 for devices which do not support oneshot mode.
341 void clockevents_config_and_register(struct clock_event_device *dev,
342 u32 freq, unsigned long min_delta,
343 unsigned long max_delta)
345 dev->min_delta_ticks = min_delta;
346 dev->max_delta_ticks = max_delta;
347 clockevents_config(dev, freq);
348 clockevents_register_device(dev);
350 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
353 * clockevents_update_freq - Update frequency and reprogram a clock event device.
354 * @dev: device to modify
355 * @freq: new device frequency
357 * Reconfigure and reprogram a clock event device in oneshot
358 * mode. Must be called on the cpu for which the device delivers per
359 * cpu timer events with interrupts disabled! Returns 0 on success,
360 * -ETIME when the event is in the past.
362 int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
364 clockevents_config(dev, freq);
366 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
369 return clockevents_program_event(dev, dev->next_event, false);
373 * Noop handler when we shut down an event device
375 void clockevents_handle_noop(struct clock_event_device *dev)
380 * clockevents_exchange_device - release and request clock devices
381 * @old: device to release (can be NULL)
382 * @new: device to request (can be NULL)
384 * Called from the notifier chain. clockevents_lock is held already
386 void clockevents_exchange_device(struct clock_event_device *old,
387 struct clock_event_device *new)
391 local_irq_save(flags);
393 * Caller releases a clock event device. We queue it into the
394 * released list and do a notify add later.
397 module_put(old->owner);
398 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
399 list_del(&old->list);
400 list_add(&old->list, &clockevents_released);
404 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
405 clockevents_shutdown(new);
407 local_irq_restore(flags);
411 * clockevents_suspend - suspend clock devices
413 void clockevents_suspend(void)
415 struct clock_event_device *dev;
417 list_for_each_entry_reverse(dev, &clockevent_devices, list)
423 * clockevents_resume - resume clock devices
425 void clockevents_resume(void)
427 struct clock_event_device *dev;
429 list_for_each_entry(dev, &clockevent_devices, list)
434 #ifdef CONFIG_GENERIC_CLOCKEVENTS
436 * clockevents_notify - notification about relevant events
438 void clockevents_notify(unsigned long reason, void *arg)
440 struct clock_event_device *dev, *tmp;
444 raw_spin_lock_irqsave(&clockevents_lock, flags);
445 tick_notify(reason, arg);
448 case CLOCK_EVT_NOTIFY_CPU_DEAD:
450 * Unregister the clock event devices which were
451 * released from the users in the notify chain.
453 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
454 list_del(&dev->list);
456 * Now check whether the CPU has left unused per cpu devices
459 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
460 if (cpumask_test_cpu(cpu, dev->cpumask) &&
461 cpumask_weight(dev->cpumask) == 1 &&
462 !tick_is_broadcast_device(dev)) {
463 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
464 list_del(&dev->list);
471 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
473 EXPORT_SYMBOL_GPL(clockevents_notify);