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>
20 #include <linux/sysdev.h>
22 #include "tick-internal.h"
24 /* The registered clock event devices */
25 static LIST_HEAD(clockevent_devices
);
26 static LIST_HEAD(clockevents_released
);
28 /* Notification for clock events */
29 static RAW_NOTIFIER_HEAD(clockevents_chain
);
31 /* Protection for the above */
32 static DEFINE_RAW_SPINLOCK(clockevents_lock
);
35 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
36 * @latch: value to convert
37 * @evt: pointer to clock event device descriptor
39 * Math helper, returns latch value converted to nanoseconds (bound checked)
41 u64
clockevent_delta2ns(unsigned long latch
, struct clock_event_device
*evt
)
43 u64 clc
= (u64
) latch
<< evt
->shift
;
45 if (unlikely(!evt
->mult
)) {
50 do_div(clc
, evt
->mult
);
58 EXPORT_SYMBOL_GPL(clockevent_delta2ns
);
61 * clockevents_set_mode - set the operating mode of a clock event device
62 * @dev: device to modify
65 * Must be called with interrupts disabled !
67 void clockevents_set_mode(struct clock_event_device
*dev
,
68 enum clock_event_mode mode
)
70 if (dev
->mode
!= mode
) {
71 dev
->set_mode(mode
, dev
);
75 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
76 * on it, so fix it up and emit a warning:
78 if (mode
== CLOCK_EVT_MODE_ONESHOT
) {
79 if (unlikely(!dev
->mult
)) {
88 * clockevents_shutdown - shutdown the device and clear next_event
89 * @dev: device to shutdown
91 void clockevents_shutdown(struct clock_event_device
*dev
)
93 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
94 dev
->next_event
.tv64
= KTIME_MAX
;
98 * clockevents_program_event - Reprogram the clock event device.
99 * @expires: absolute expiry time (monotonic clock)
101 * Returns 0 on success, -ETIME when the event is in the past.
103 int clockevents_program_event(struct clock_event_device
*dev
, ktime_t expires
,
106 unsigned long long clc
;
109 if (unlikely(expires
.tv64
< 0)) {
114 delta
= ktime_to_ns(ktime_sub(expires
, now
));
119 dev
->next_event
= expires
;
121 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
124 if (delta
> dev
->max_delta_ns
)
125 delta
= dev
->max_delta_ns
;
126 if (delta
< dev
->min_delta_ns
)
127 delta
= dev
->min_delta_ns
;
129 clc
= delta
* dev
->mult
;
132 return dev
->set_next_event((unsigned long) clc
, dev
);
136 * clockevents_register_notifier - register a clock events change listener
138 int clockevents_register_notifier(struct notifier_block
*nb
)
143 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
144 ret
= raw_notifier_chain_register(&clockevents_chain
, nb
);
145 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
151 * Notify about a clock event change. Called with clockevents_lock
154 static void clockevents_do_notify(unsigned long reason
, void *dev
)
156 raw_notifier_call_chain(&clockevents_chain
, reason
, dev
);
160 * Called after a notify add to make devices available which were
161 * released from the notifier call.
163 static void clockevents_notify_released(void)
165 struct clock_event_device
*dev
;
167 while (!list_empty(&clockevents_released
)) {
168 dev
= list_entry(clockevents_released
.next
,
169 struct clock_event_device
, list
);
170 list_del(&dev
->list
);
171 list_add(&dev
->list
, &clockevent_devices
);
172 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
177 * clockevents_register_device - register a clock event device
178 * @dev: device to register
180 void clockevents_register_device(struct clock_event_device
*dev
)
184 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
185 BUG_ON(!dev
->cpumask
);
187 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
189 list_add(&dev
->list
, &clockevent_devices
);
190 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
191 clockevents_notify_released();
193 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
195 EXPORT_SYMBOL_GPL(clockevents_register_device
);
197 static void clockevents_config(struct clock_event_device
*dev
,
202 if (!(dev
->features
& CLOCK_EVT_FEAT_ONESHOT
))
206 * Calculate the maximum number of seconds we can sleep. Limit
207 * to 10 minutes for hardware which can program more than
208 * 32bit ticks so we still get reasonable conversion values.
210 sec
= dev
->max_delta_ticks
;
214 else if (sec
> 600 && dev
->max_delta_ticks
> UINT_MAX
)
217 clockevents_calc_mult_shift(dev
, freq
, sec
);
218 dev
->min_delta_ns
= clockevent_delta2ns(dev
->min_delta_ticks
, dev
);
219 dev
->max_delta_ns
= clockevent_delta2ns(dev
->max_delta_ticks
, dev
);
223 * clockevents_config_and_register - Configure and register a clock event device
224 * @dev: device to register
225 * @freq: The clock frequency
226 * @min_delta: The minimum clock ticks to program in oneshot mode
227 * @max_delta: The maximum clock ticks to program in oneshot mode
229 * min/max_delta can be 0 for devices which do not support oneshot mode.
231 void clockevents_config_and_register(struct clock_event_device
*dev
,
232 u32 freq
, unsigned long min_delta
,
233 unsigned long max_delta
)
235 dev
->min_delta_ticks
= min_delta
;
236 dev
->max_delta_ticks
= max_delta
;
237 clockevents_config(dev
, freq
);
238 clockevents_register_device(dev
);
242 * clockevents_update_freq - Update frequency and reprogram a clock event device.
243 * @dev: device to modify
244 * @freq: new device frequency
246 * Reconfigure and reprogram a clock event device in oneshot
247 * mode. Must be called on the cpu for which the device delivers per
248 * cpu timer events with interrupts disabled! Returns 0 on success,
249 * -ETIME when the event is in the past.
251 int clockevents_update_freq(struct clock_event_device
*dev
, u32 freq
)
253 clockevents_config(dev
, freq
);
255 if (dev
->mode
!= CLOCK_EVT_MODE_ONESHOT
)
258 return clockevents_program_event(dev
, dev
->next_event
, ktime_get());
262 * Noop handler when we shut down an event device
264 void clockevents_handle_noop(struct clock_event_device
*dev
)
269 * clockevents_exchange_device - release and request clock devices
270 * @old: device to release (can be NULL)
271 * @new: device to request (can be NULL)
273 * Called from the notifier chain. clockevents_lock is held already
275 void clockevents_exchange_device(struct clock_event_device
*old
,
276 struct clock_event_device
*new)
280 local_irq_save(flags
);
282 * Caller releases a clock event device. We queue it into the
283 * released list and do a notify add later.
286 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
287 list_del(&old
->list
);
288 list_add(&old
->list
, &clockevents_released
);
292 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
293 clockevents_shutdown(new);
295 local_irq_restore(flags
);
298 #ifdef CONFIG_GENERIC_CLOCKEVENTS
300 * clockevents_notify - notification about relevant events
302 void clockevents_notify(unsigned long reason
, void *arg
)
304 struct clock_event_device
*dev
, *tmp
;
308 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
309 clockevents_do_notify(reason
, arg
);
312 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
314 * Unregister the clock event devices which were
315 * released from the users in the notify chain.
317 list_for_each_entry_safe(dev
, tmp
, &clockevents_released
, list
)
318 list_del(&dev
->list
);
320 * Now check whether the CPU has left unused per cpu devices
323 list_for_each_entry_safe(dev
, tmp
, &clockevent_devices
, list
) {
324 if (cpumask_test_cpu(cpu
, dev
->cpumask
) &&
325 cpumask_weight(dev
->cpumask
) == 1 &&
326 !tick_is_broadcast_device(dev
)) {
327 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
328 list_del(&dev
->list
);
335 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
337 EXPORT_SYMBOL_GPL(clockevents_notify
);