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 do_div(clc
, evt
->mult
);
50 return (unsigned long) clc
;
54 * clockevents_set_mode - set the operating mode of a clock event device
55 * @dev: device to modify
58 * Must be called with interrupts disabled !
60 void clockevents_set_mode(struct clock_event_device
*dev
,
61 enum clock_event_mode mode
)
63 if (dev
->mode
!= mode
) {
64 dev
->set_mode(mode
, dev
);
70 * clockevents_program_event - Reprogram the clock event device.
71 * @expires: absolute expiry time (monotonic clock)
73 * Returns 0 on success, -ETIME when the event is in the past.
75 int clockevents_program_event(struct clock_event_device
*dev
, ktime_t expires
,
78 unsigned long long clc
;
81 if (unlikely(expires
.tv64
< 0)) {
86 delta
= ktime_to_ns(ktime_sub(expires
, now
));
91 dev
->next_event
= expires
;
93 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
96 if (delta
> dev
->max_delta_ns
)
97 delta
= dev
->max_delta_ns
;
98 if (delta
< dev
->min_delta_ns
)
99 delta
= dev
->min_delta_ns
;
101 clc
= delta
* dev
->mult
;
104 return dev
->set_next_event((unsigned long) clc
, dev
);
108 * clockevents_register_notifier - register a clock events change listener
110 int clockevents_register_notifier(struct notifier_block
*nb
)
114 spin_lock(&clockevents_lock
);
115 ret
= raw_notifier_chain_register(&clockevents_chain
, nb
);
116 spin_unlock(&clockevents_lock
);
122 * Notify about a clock event change. Called with clockevents_lock
125 static void clockevents_do_notify(unsigned long reason
, void *dev
)
127 raw_notifier_call_chain(&clockevents_chain
, reason
, dev
);
131 * Called after a notify add to make devices availble which were
132 * released from the notifier call.
134 static void clockevents_notify_released(void)
136 struct clock_event_device
*dev
;
138 while (!list_empty(&clockevents_released
)) {
139 dev
= list_entry(clockevents_released
.next
,
140 struct clock_event_device
, list
);
141 list_del(&dev
->list
);
142 list_add(&dev
->list
, &clockevent_devices
);
143 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
148 * clockevents_register_device - register a clock event device
149 * @dev: device to register
151 void clockevents_register_device(struct clock_event_device
*dev
)
153 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
155 spin_lock(&clockevents_lock
);
157 list_add(&dev
->list
, &clockevent_devices
);
158 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD
, dev
);
159 clockevents_notify_released();
161 spin_unlock(&clockevents_lock
);
165 * Noop handler when we shut down an event device
167 static void clockevents_handle_noop(struct clock_event_device
*dev
)
172 * clockevents_exchange_device - release and request clock devices
173 * @old: device to release (can be NULL)
174 * @new: device to request (can be NULL)
176 * Called from the notifier chain. clockevents_lock is held already
178 void clockevents_exchange_device(struct clock_event_device
*old
,
179 struct clock_event_device
*new)
183 local_irq_save(flags
);
185 * Caller releases a clock event device. We queue it into the
186 * released list and do a notify add later.
189 old
->event_handler
= clockevents_handle_noop
;
190 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
191 list_del(&old
->list
);
192 list_add(&old
->list
, &clockevents_released
);
196 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
197 clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN
);
199 local_irq_restore(flags
);
202 #ifdef CONFIG_GENERIC_CLOCKEVENTS
204 * clockevents_notify - notification about relevant events
206 void clockevents_notify(unsigned long reason
, void *arg
)
208 spin_lock(&clockevents_lock
);
209 clockevents_do_notify(reason
, arg
);
212 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
214 * Unregister the clock event devices which were
215 * released from the users in the notify chain.
217 while (!list_empty(&clockevents_released
)) {
218 struct clock_event_device
*dev
;
220 dev
= list_entry(clockevents_released
.next
,
221 struct clock_event_device
, list
);
222 list_del(&dev
->list
);
228 spin_unlock(&clockevents_lock
);
230 EXPORT_SYMBOL_GPL(clockevents_notify
);