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(KERN_WARNING "CE: Reprogramming failure. Giving up\n");
142 dev->next_event.tv64 = KTIME_MAX;
146 if (dev->min_delta_ns < 5000)
147 dev->min_delta_ns = 5000;
149 dev->min_delta_ns += dev->min_delta_ns >> 1;
151 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
152 dev->min_delta_ns = MIN_DELTA_LIMIT;
154 printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
155 dev->name ? dev->name : "?",
156 (unsigned long long) dev->min_delta_ns);
161 * clockevents_program_min_delta - Set clock event device to the minimum delay.
162 * @dev: device to program
164 * Returns 0 on success, -ETIME when the retry loop failed.
166 static int clockevents_program_min_delta(struct clock_event_device *dev)
168 unsigned long long clc;
173 delta = dev->min_delta_ns;
174 dev->next_event = ktime_add_ns(ktime_get(), delta);
176 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
180 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
181 if (dev->set_next_event((unsigned long) clc, dev) == 0)
186 * We tried 3 times to program the device with the
187 * given min_delta_ns. Try to increase the minimum
188 * delta, if that fails as well get out of here.
190 if (clockevents_increase_min_delta(dev))
197 #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
200 * clockevents_program_min_delta - Set clock event device to the minimum delay.
201 * @dev: device to program
203 * Returns 0 on success, -ETIME when the retry loop failed.
205 static int clockevents_program_min_delta(struct clock_event_device *dev)
207 unsigned long long clc;
210 delta = dev->min_delta_ns;
211 dev->next_event = ktime_add_ns(ktime_get(), delta);
213 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
217 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
218 return dev->set_next_event((unsigned long) clc, dev);
221 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
224 * clockevents_program_event - Reprogram the clock event device.
225 * @dev: device to program
226 * @expires: absolute expiry time (monotonic clock)
227 * @force: program minimum delay if expires can not be set
229 * Returns 0 on success, -ETIME when the event is in the past.
231 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
234 unsigned long long clc;
238 if (unlikely(expires.tv64 < 0)) {
243 dev->next_event = expires;
245 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
248 /* Shortcut for clockevent devices that can deal with ktime. */
249 if (dev->features & CLOCK_EVT_FEAT_KTIME)
250 return dev->set_next_ktime(expires, dev);
252 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
254 return force ? clockevents_program_min_delta(dev) : -ETIME;
256 delta = min(delta, (int64_t) dev->max_delta_ns);
257 delta = max(delta, (int64_t) dev->min_delta_ns);
259 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
260 rc = dev->set_next_event((unsigned long) clc, dev);
262 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
266 * Called after a notify add to make devices available which were
267 * released from the notifier call.
269 static void clockevents_notify_released(void)
271 struct clock_event_device *dev;
273 while (!list_empty(&clockevents_released)) {
274 dev = list_entry(clockevents_released.next,
275 struct clock_event_device, list);
276 list_del(&dev->list);
277 list_add(&dev->list, &clockevent_devices);
278 tick_check_new_device(dev);
283 * clockevents_register_device - register a clock event device
284 * @dev: device to register
286 void clockevents_register_device(struct clock_event_device *dev)
290 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
292 WARN_ON(num_possible_cpus() > 1);
293 dev->cpumask = cpumask_of(smp_processor_id());
296 raw_spin_lock_irqsave(&clockevents_lock, flags);
298 list_add(&dev->list, &clockevent_devices);
299 tick_check_new_device(dev);
300 clockevents_notify_released();
302 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
304 EXPORT_SYMBOL_GPL(clockevents_register_device);
306 void clockevents_config(struct clock_event_device *dev, u32 freq)
310 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
314 * Calculate the maximum number of seconds we can sleep. Limit
315 * to 10 minutes for hardware which can program more than
316 * 32bit ticks so we still get reasonable conversion values.
318 sec = dev->max_delta_ticks;
322 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
325 clockevents_calc_mult_shift(dev, freq, sec);
326 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
327 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
331 * clockevents_config_and_register - Configure and register a clock event device
332 * @dev: device to register
333 * @freq: The clock frequency
334 * @min_delta: The minimum clock ticks to program in oneshot mode
335 * @max_delta: The maximum clock ticks to program in oneshot mode
337 * min/max_delta can be 0 for devices which do not support oneshot mode.
339 void clockevents_config_and_register(struct clock_event_device *dev,
340 u32 freq, unsigned long min_delta,
341 unsigned long max_delta)
343 dev->min_delta_ticks = min_delta;
344 dev->max_delta_ticks = max_delta;
345 clockevents_config(dev, freq);
346 clockevents_register_device(dev);
348 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
351 * clockevents_update_freq - Update frequency and reprogram a clock event device.
352 * @dev: device to modify
353 * @freq: new device frequency
355 * Reconfigure and reprogram a clock event device in oneshot
356 * mode. Must be called on the cpu for which the device delivers per
357 * cpu timer events with interrupts disabled! Returns 0 on success,
358 * -ETIME when the event is in the past.
360 int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
362 clockevents_config(dev, freq);
364 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
367 return clockevents_program_event(dev, dev->next_event, false);
371 * Noop handler when we shut down an event device
373 void clockevents_handle_noop(struct clock_event_device *dev)
378 * clockevents_exchange_device - release and request clock devices
379 * @old: device to release (can be NULL)
380 * @new: device to request (can be NULL)
382 * Called from the notifier chain. clockevents_lock is held already
384 void clockevents_exchange_device(struct clock_event_device *old,
385 struct clock_event_device *new)
389 local_irq_save(flags);
391 * Caller releases a clock event device. We queue it into the
392 * released list and do a notify add later.
395 module_put(old->owner);
396 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
397 list_del(&old->list);
398 list_add(&old->list, &clockevents_released);
402 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
403 clockevents_shutdown(new);
405 local_irq_restore(flags);
409 * clockevents_suspend - suspend clock devices
411 void clockevents_suspend(void)
413 struct clock_event_device *dev;
415 list_for_each_entry_reverse(dev, &clockevent_devices, list)
421 * clockevents_resume - resume clock devices
423 void clockevents_resume(void)
425 struct clock_event_device *dev;
427 list_for_each_entry(dev, &clockevent_devices, list)
432 #ifdef CONFIG_GENERIC_CLOCKEVENTS
434 * clockevents_notify - notification about relevant events
436 void clockevents_notify(unsigned long reason, void *arg)
438 struct clock_event_device *dev, *tmp;
442 raw_spin_lock_irqsave(&clockevents_lock, flags);
443 tick_notify(reason, arg);
446 case CLOCK_EVT_NOTIFY_CPU_DEAD:
448 * Unregister the clock event devices which were
449 * released from the users in the notify chain.
451 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
452 list_del(&dev->list);
454 * Now check whether the CPU has left unused per cpu devices
457 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
458 if (cpumask_test_cpu(cpu, dev->cpumask) &&
459 cpumask_weight(dev->cpumask) == 1 &&
460 !tick_is_broadcast_device(dev)) {
461 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
462 list_del(&dev->list);
469 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
471 EXPORT_SYMBOL_GPL(clockevents_notify);