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/notifier.h>
19 #include <linux/smp.h>
21 #include "tick-internal.h"
23 /* The registered clock event devices */
24 static LIST_HEAD(clockevent_devices
);
25 static LIST_HEAD(clockevents_released
);
27 /* Notification for clock events */
28 static RAW_NOTIFIER_HEAD(clockevents_chain
);
30 /* Protection for the above */
31 static DEFINE_RAW_SPINLOCK(clockevents_lock
);
33 static u64
cev_delta2ns(unsigned long latch
, struct clock_event_device
*evt
,
36 u64 clc
= (u64
) latch
<< evt
->shift
;
39 if (unlikely(!evt
->mult
)) {
43 rnd
= (u64
) evt
->mult
- 1;
46 * Upper bound sanity check. If the backwards conversion is
47 * not equal latch, we know that the above shift overflowed.
49 if ((clc
>> evt
->shift
) != (u64
)latch
)
53 * Scaled math oddities:
55 * For mult <= (1 << shift) we can safely add mult - 1 to
56 * prevent integer rounding loss. So the backwards conversion
57 * from nsec to device ticks will be correct.
59 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
60 * need to be careful. Adding mult - 1 will result in a value
61 * which when converted back to device ticks can be larger
62 * than latch by up to (mult - 1) >> shift. For the min_delta
63 * calculation we still want to apply this in order to stay
64 * above the minimum device ticks limit. For the upper limit
65 * we would end up with a latch value larger than the upper
66 * limit of the device, so we omit the add to stay below the
67 * device upper boundary.
69 * Also omit the add if it would overflow the u64 boundary.
71 if ((~0ULL - clc
> rnd
) &&
72 (!ismax
|| evt
->mult
<= (1U << evt
->shift
)))
75 do_div(clc
, evt
->mult
);
77 /* Deltas less than 1usec are pointless noise */
78 return clc
> 1000 ? clc
: 1000;
82 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
83 * @latch: value to convert
84 * @evt: pointer to clock event device descriptor
86 * Math helper, returns latch value converted to nanoseconds (bound checked)
88 u64
clockevent_delta2ns(unsigned long latch
, struct clock_event_device
*evt
)
90 return cev_delta2ns(latch
, evt
, false);
92 EXPORT_SYMBOL_GPL(clockevent_delta2ns
);
95 * clockevents_set_mode - set the operating mode of a clock event device
96 * @dev: device to modify
99 * Must be called with interrupts disabled !
101 void clockevents_set_mode(struct clock_event_device
*dev
,
102 enum clock_event_mode mode
)
104 if (dev
->mode
!= mode
) {
105 dev
->set_mode(mode
, dev
);
109 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
110 * on it, so fix it up and emit a warning:
112 if (mode
== CLOCK_EVT_MODE_ONESHOT
) {
113 if (unlikely(!dev
->mult
)) {
122 * clockevents_shutdown - shutdown the device and clear next_event
123 * @dev: device to shutdown
125 void clockevents_shutdown(struct clock_event_device
*dev
)
127 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
128 dev
->next_event
.tv64
= KTIME_MAX
;
131 #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
133 /* Limit min_delta to a jiffie */
134 #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
137 * clockevents_increase_min_delta - raise minimum delta of a clock event device
138 * @dev: device to increase the minimum delta
140 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
142 static int clockevents_increase_min_delta(struct clock_event_device
*dev
)
144 /* Nothing to do if we already reached the limit */
145 if (dev
->min_delta_ns
>= MIN_DELTA_LIMIT
) {
146 printk(KERN_WARNING
"CE: Reprogramming failure. Giving up\n");
147 dev
->next_event
.tv64
= KTIME_MAX
;
151 if (dev
->min_delta_ns
< 5000)
152 dev
->min_delta_ns
= 5000;
154 dev
->min_delta_ns
+= dev
->min_delta_ns
>> 1;
156 if (dev
->min_delta_ns
> MIN_DELTA_LIMIT
)
157 dev
->min_delta_ns
= MIN_DELTA_LIMIT
;
159 printk(KERN_WARNING
"CE: %s increased min_delta_ns to %llu nsec\n",
160 dev
->name
? dev
->name
: "?",
161 (unsigned long long) dev
->min_delta_ns
);
166 * clockevents_program_min_delta - Set clock event device to the minimum delay.
167 * @dev: device to program
169 * Returns 0 on success, -ETIME when the retry loop failed.
171 static int clockevents_program_min_delta(struct clock_event_device
*dev
)
173 unsigned long long clc
;
178 delta
= dev
->min_delta_ns
;
179 dev
->next_event
= ktime_add_ns(ktime_get(), delta
);
181 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
185 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
186 if (dev
->set_next_event((unsigned long) clc
, dev
) == 0)
191 * We tried 3 times to program the device with the
192 * given min_delta_ns. Try to increase the minimum
193 * delta, if that fails as well get out of here.
195 if (clockevents_increase_min_delta(dev
))
202 #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
205 * clockevents_program_min_delta - Set clock event device to the minimum delay.
206 * @dev: device to program
208 * Returns 0 on success, -ETIME when the retry loop failed.
210 static int clockevents_program_min_delta(struct clock_event_device
*dev
)
212 unsigned long long clc
;
215 delta
= dev
->min_delta_ns
;
216 dev
->next_event
= ktime_add_ns(ktime_get(), delta
);
218 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
222 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
223 return dev
->set_next_event((unsigned long) clc
, dev
);
226 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
229 * clockevents_program_event - Reprogram the clock event device.
230 * @dev: device to program
231 * @expires: absolute expiry time (monotonic clock)
232 * @force: program minimum delay if expires can not be set
234 * Returns 0 on success, -ETIME when the event is in the past.
236 int clockevents_program_event(struct clock_event_device
*dev
, ktime_t expires
,
239 unsigned long long clc
;
243 if (unlikely(expires
.tv64
< 0)) {
248 dev
->next_event
= expires
;
250 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
253 /* Shortcut for clockevent devices that can deal with ktime. */
254 if (dev
->features
& CLOCK_EVT_FEAT_KTIME
)
255 return dev
->set_next_ktime(expires
, dev
);
257 delta
= ktime_to_ns(ktime_sub(expires
, ktime_get()));
259 return force
? clockevents_program_min_delta(dev
) : -ETIME
;
261 delta
= min(delta
, (int64_t) dev
->max_delta_ns
);
262 delta
= max(delta
, (int64_t) dev
->min_delta_ns
);
264 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
265 rc
= dev
->set_next_event((unsigned long) clc
, dev
);
267 return (rc
&& force
) ? clockevents_program_min_delta(dev
) : rc
;
271 * clockevents_register_notifier - register a clock events change listener
273 int clockevents_register_notifier(struct notifier_block
*nb
)
278 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
279 ret
= raw_notifier_chain_register(&clockevents_chain
, nb
);
280 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
286 * Notify about a clock event change. Called with clockevents_lock
289 static void clockevents_do_notify(unsigned long reason
, void *dev
)
291 raw_notifier_call_chain(&clockevents_chain
, reason
, dev
);
295 * Called after a notify add to make devices available which were
296 * released from the notifier call.
298 static void clockevents_notify_released(void)
300 struct clock_event_device
*dev
;
302 while (!list_empty(&clockevents_released
)) {
303 dev
= list_entry(clockevents_released
.next
,
304 struct clock_event_device
, list
);
305 list_del(&dev
->list
);
306 list_add(&dev
->list
, &clockevent_devices
);
307 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
312 * clockevents_register_device - register a clock event device
313 * @dev: device to register
315 void clockevents_register_device(struct clock_event_device
*dev
)
319 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
321 WARN_ON(num_possible_cpus() > 1);
322 dev
->cpumask
= cpumask_of(smp_processor_id());
325 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
327 list_add(&dev
->list
, &clockevent_devices
);
328 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
329 clockevents_notify_released();
331 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
333 EXPORT_SYMBOL_GPL(clockevents_register_device
);
335 static void clockevents_config(struct clock_event_device
*dev
,
340 if (!(dev
->features
& CLOCK_EVT_FEAT_ONESHOT
))
344 * Calculate the maximum number of seconds we can sleep. Limit
345 * to 10 minutes for hardware which can program more than
346 * 32bit ticks so we still get reasonable conversion values.
348 sec
= dev
->max_delta_ticks
;
352 else if (sec
> 600 && dev
->max_delta_ticks
> UINT_MAX
)
355 clockevents_calc_mult_shift(dev
, freq
, sec
);
356 dev
->min_delta_ns
= cev_delta2ns(dev
->min_delta_ticks
, dev
, false);
357 dev
->max_delta_ns
= cev_delta2ns(dev
->max_delta_ticks
, dev
, true);
361 * clockevents_config_and_register - Configure and register a clock event device
362 * @dev: device to register
363 * @freq: The clock frequency
364 * @min_delta: The minimum clock ticks to program in oneshot mode
365 * @max_delta: The maximum clock ticks to program in oneshot mode
367 * min/max_delta can be 0 for devices which do not support oneshot mode.
369 void clockevents_config_and_register(struct clock_event_device
*dev
,
370 u32 freq
, unsigned long min_delta
,
371 unsigned long max_delta
)
373 dev
->min_delta_ticks
= min_delta
;
374 dev
->max_delta_ticks
= max_delta
;
375 clockevents_config(dev
, freq
);
376 clockevents_register_device(dev
);
380 * clockevents_update_freq - Update frequency and reprogram a clock event device.
381 * @dev: device to modify
382 * @freq: new device frequency
384 * Reconfigure and reprogram a clock event device in oneshot
385 * mode. Must be called on the cpu for which the device delivers per
386 * cpu timer events with interrupts disabled! Returns 0 on success,
387 * -ETIME when the event is in the past.
389 int clockevents_update_freq(struct clock_event_device
*dev
, u32 freq
)
391 clockevents_config(dev
, freq
);
393 if (dev
->mode
!= CLOCK_EVT_MODE_ONESHOT
)
396 return clockevents_program_event(dev
, dev
->next_event
, false);
400 * Noop handler when we shut down an event device
402 void clockevents_handle_noop(struct clock_event_device
*dev
)
407 * clockevents_exchange_device - release and request clock devices
408 * @old: device to release (can be NULL)
409 * @new: device to request (can be NULL)
411 * Called from the notifier chain. clockevents_lock is held already
413 void clockevents_exchange_device(struct clock_event_device
*old
,
414 struct clock_event_device
*new)
418 local_irq_save(flags
);
420 * Caller releases a clock event device. We queue it into the
421 * released list and do a notify add later.
424 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
425 list_del(&old
->list
);
426 list_add(&old
->list
, &clockevents_released
);
430 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
431 clockevents_shutdown(new);
433 local_irq_restore(flags
);
436 #ifdef CONFIG_GENERIC_CLOCKEVENTS
438 * clockevents_notify - notification about relevant events
440 void clockevents_notify(unsigned long reason
, void *arg
)
442 struct clock_event_device
*dev
, *tmp
;
446 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
447 clockevents_do_notify(reason
, arg
);
450 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
452 * Unregister the clock event devices which were
453 * released from the users in the notify chain.
455 list_for_each_entry_safe(dev
, tmp
, &clockevents_released
, list
)
456 list_del(&dev
->list
);
458 * Now check whether the CPU has left unused per cpu devices
461 list_for_each_entry_safe(dev
, tmp
, &clockevent_devices
, list
) {
462 if (cpumask_test_cpu(cpu
, dev
->cpumask
) &&
463 cpumask_weight(dev
->cpumask
) == 1 &&
464 !tick_is_broadcast_device(dev
)) {
465 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
466 list_del(&dev
->list
);
473 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
475 EXPORT_SYMBOL_GPL(clockevents_notify
);