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 /* The registered clock event devices */
23 static LIST_HEAD(clockevent_devices
);
24 static LIST_HEAD(clockevents_released
);
26 /* Notification for clock events */
27 static RAW_NOTIFIER_HEAD(clockevents_chain
);
29 /* Protection for the above */
30 static DEFINE_SPINLOCK(clockevents_lock
);
33 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
34 * @latch: value to convert
35 * @evt: pointer to clock event device descriptor
37 * Math helper, returns latch value converted to nanoseconds (bound checked)
39 unsigned long clockevent_delta2ns(unsigned long latch
,
40 struct clock_event_device
*evt
)
42 u64 clc
= ((u64
) latch
<< evt
->shift
);
44 if (unlikely(!evt
->mult
)) {
49 do_div(clc
, evt
->mult
);
55 return (unsigned long) clc
;
59 * clockevents_set_mode - set the operating mode of a clock event device
60 * @dev: device to modify
63 * Must be called with interrupts disabled !
65 void clockevents_set_mode(struct clock_event_device
*dev
,
66 enum clock_event_mode mode
)
68 if (dev
->mode
!= mode
) {
69 dev
->set_mode(mode
, dev
);
75 * clockevents_shutdown - shutdown the device and clear next_event
76 * @dev: device to shutdown
78 void clockevents_shutdown(struct clock_event_device
*dev
)
80 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
81 dev
->next_event
.tv64
= KTIME_MAX
;
85 * clockevents_program_event - Reprogram the clock event device.
86 * @expires: absolute expiry time (monotonic clock)
88 * Returns 0 on success, -ETIME when the event is in the past.
90 int clockevents_program_event(struct clock_event_device
*dev
, ktime_t expires
,
93 unsigned long long clc
;
96 if (unlikely(expires
.tv64
< 0)) {
101 delta
= ktime_to_ns(ktime_sub(expires
, now
));
106 dev
->next_event
= expires
;
108 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
111 if (delta
> dev
->max_delta_ns
)
112 delta
= dev
->max_delta_ns
;
113 if (delta
< dev
->min_delta_ns
)
114 delta
= dev
->min_delta_ns
;
116 clc
= delta
* dev
->mult
;
119 return dev
->set_next_event((unsigned long) clc
, dev
);
123 * clockevents_register_notifier - register a clock events change listener
125 int clockevents_register_notifier(struct notifier_block
*nb
)
130 spin_lock_irqsave(&clockevents_lock
, flags
);
131 ret
= raw_notifier_chain_register(&clockevents_chain
, nb
);
132 spin_unlock_irqrestore(&clockevents_lock
, flags
);
138 * Notify about a clock event change. Called with clockevents_lock
141 static void clockevents_do_notify(unsigned long reason
, void *dev
)
143 raw_notifier_call_chain(&clockevents_chain
, reason
, dev
);
147 * Called after a notify add to make devices available which were
148 * released from the notifier call.
150 static void clockevents_notify_released(void)
152 struct clock_event_device
*dev
;
154 while (!list_empty(&clockevents_released
)) {
155 dev
= list_entry(clockevents_released
.next
,
156 struct clock_event_device
, list
);
157 list_del(&dev
->list
);
158 list_add(&dev
->list
, &clockevent_devices
);
159 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
164 * clockevents_register_device - register a clock event device
165 * @dev: device to register
167 void clockevents_register_device(struct clock_event_device
*dev
)
171 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
173 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
174 * on it, so fix it up and emit a warning:
176 if (unlikely(!dev
->mult
)) {
181 spin_lock_irqsave(&clockevents_lock
, flags
);
183 list_add(&dev
->list
, &clockevent_devices
);
184 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
185 clockevents_notify_released();
187 spin_unlock_irqrestore(&clockevents_lock
, flags
);
191 * Noop handler when we shut down an event device
193 void clockevents_handle_noop(struct clock_event_device
*dev
)
198 * clockevents_exchange_device - release and request clock devices
199 * @old: device to release (can be NULL)
200 * @new: device to request (can be NULL)
202 * Called from the notifier chain. clockevents_lock is held already
204 void clockevents_exchange_device(struct clock_event_device
*old
,
205 struct clock_event_device
*new)
209 local_irq_save(flags
);
211 * Caller releases a clock event device. We queue it into the
212 * released list and do a notify add later.
215 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
216 list_del(&old
->list
);
217 list_add(&old
->list
, &clockevents_released
);
221 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
222 clockevents_shutdown(new);
224 local_irq_restore(flags
);
227 #ifdef CONFIG_GENERIC_CLOCKEVENTS
229 * clockevents_notify - notification about relevant events
231 void clockevents_notify(unsigned long reason
, void *arg
)
233 struct list_head
*node
, *tmp
;
236 spin_lock_irqsave(&clockevents_lock
, flags
);
237 clockevents_do_notify(reason
, arg
);
240 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
242 * Unregister the clock event devices which were
243 * released from the users in the notify chain.
245 list_for_each_safe(node
, tmp
, &clockevents_released
)
251 spin_unlock_irqrestore(&clockevents_lock
, flags
);
253 EXPORT_SYMBOL_GPL(clockevents_notify
);