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>
21 #include <linux/tick.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_SPINLOCK(clockevents_lock
);
34 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
35 * @latch: value to convert
36 * @evt: pointer to clock event device descriptor
38 * Math helper, returns latch value converted to nanoseconds (bound checked)
40 unsigned long clockevent_delta2ns(unsigned long latch
,
41 struct clock_event_device
*evt
)
43 u64 clc
= ((u64
) latch
<< evt
->shift
);
45 if (unlikely(!evt
->mult
)) {
50 do_div(clc
, evt
->mult
);
56 return (unsigned long) clc
;
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 spin_lock_irqsave(&clockevents_lock
, flags
);
144 ret
= raw_notifier_chain_register(&clockevents_chain
, nb
);
145 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 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 spin_unlock_irqrestore(&clockevents_lock
, flags
);
195 EXPORT_SYMBOL_GPL(clockevents_register_device
);
198 * Noop handler when we shut down an event device
200 void clockevents_handle_noop(struct clock_event_device
*dev
)
205 * clockevents_exchange_device - release and request clock devices
206 * @old: device to release (can be NULL)
207 * @new: device to request (can be NULL)
209 * Called from the notifier chain. clockevents_lock is held already
211 void clockevents_exchange_device(struct clock_event_device
*old
,
212 struct clock_event_device
*new)
216 local_irq_save(flags
);
218 * Caller releases a clock event device. We queue it into the
219 * released list and do a notify add later.
222 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
223 list_del(&old
->list
);
224 list_add(&old
->list
, &clockevents_released
);
228 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
229 clockevents_shutdown(new);
231 local_irq_restore(flags
);
234 #ifdef CONFIG_GENERIC_CLOCKEVENTS
236 * clockevents_notify - notification about relevant events
238 void clockevents_notify(unsigned long reason
, void *arg
)
240 struct list_head
*node
, *tmp
;
243 spin_lock_irqsave(&clockevents_lock
, flags
);
244 clockevents_do_notify(reason
, arg
);
247 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
249 * Unregister the clock event devices which were
250 * released from the users in the notify chain.
252 list_for_each_safe(node
, tmp
, &clockevents_released
)
258 spin_unlock_irqrestore(&clockevents_lock
, flags
);
260 EXPORT_SYMBOL_GPL(clockevents_notify
);