blob: d13be216a7905d1a538286322c242726a1af3aba [file] [log] [blame]
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001/*
2 * linux/kernel/time/clockevents.c
3 *
4 * This file contains functions which manage clock event devices.
5 *
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
9 *
10 * This code is licenced under the GPL version 2. For details see
11 * kernel-base/COPYING.
12 */
13
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
22/* The registered clock event devices */
23static LIST_HEAD(clockevent_devices);
24static LIST_HEAD(clockevents_released);
25
26/* Notification for clock events */
27static RAW_NOTIFIER_HEAD(clockevents_chain);
28
29/* Protection for the above */
30static DEFINE_SPINLOCK(clockevents_lock);
31
32/**
33 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
34 * @latch: value to convert
35 * @evt: pointer to clock event device descriptor
36 *
37 * Math helper, returns latch value converted to nanoseconds (bound checked)
38 */
39unsigned long clockevent_delta2ns(unsigned long latch,
40 struct clock_event_device *evt)
41{
42 u64 clc = ((u64) latch << evt->shift);
43
Ingo Molnar45fe4fe2008-01-30 13:30:03 +010044 if (unlikely(!evt->mult)) {
45 evt->mult = 1;
46 WARN_ON(1);
47 }
48
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049 do_div(clc, evt->mult);
50 if (clc < 1000)
51 clc = 1000;
52 if (clc > LONG_MAX)
53 clc = LONG_MAX;
54
55 return (unsigned long) clc;
56}
57
58/**
59 * clockevents_set_mode - set the operating mode of a clock event device
60 * @dev: device to modify
61 * @mode: new mode
62 *
63 * Must be called with interrupts disabled !
64 */
65void clockevents_set_mode(struct clock_event_device *dev,
66 enum clock_event_mode mode)
67{
68 if (dev->mode != mode) {
69 dev->set_mode(mode, dev);
70 dev->mode = mode;
Magnus Damm2d682592009-01-16 17:14:38 +090071
72 /*
73 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
74 * on it, so fix it up and emit a warning:
75 */
76 if (mode == CLOCK_EVT_MODE_ONESHOT) {
77 if (unlikely(!dev->mult)) {
78 dev->mult = 1;
79 WARN_ON(1);
80 }
81 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -080082 }
83}
84
85/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -070086 * clockevents_shutdown - shutdown the device and clear next_event
87 * @dev: device to shutdown
88 */
89void clockevents_shutdown(struct clock_event_device *dev)
90{
91 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
92 dev->next_event.tv64 = KTIME_MAX;
93}
94
95/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -080096 * clockevents_program_event - Reprogram the clock event device.
97 * @expires: absolute expiry time (monotonic clock)
98 *
99 * Returns 0 on success, -ETIME when the event is in the past.
100 */
101int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
102 ktime_t now)
103{
104 unsigned long long clc;
105 int64_t delta;
106
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100107 if (unlikely(expires.tv64 < 0)) {
108 WARN_ON_ONCE(1);
109 return -ETIME;
110 }
111
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800112 delta = ktime_to_ns(ktime_sub(expires, now));
113
114 if (delta <= 0)
115 return -ETIME;
116
117 dev->next_event = expires;
118
119 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
120 return 0;
121
122 if (delta > dev->max_delta_ns)
123 delta = dev->max_delta_ns;
124 if (delta < dev->min_delta_ns)
125 delta = dev->min_delta_ns;
126
127 clc = delta * dev->mult;
128 clc >>= dev->shift;
129
130 return dev->set_next_event((unsigned long) clc, dev);
131}
132
133/**
134 * clockevents_register_notifier - register a clock events change listener
135 */
136int clockevents_register_notifier(struct notifier_block *nb)
137{
138 int ret;
139
140 spin_lock(&clockevents_lock);
141 ret = raw_notifier_chain_register(&clockevents_chain, nb);
142 spin_unlock(&clockevents_lock);
143
144 return ret;
145}
146
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800147/*
148 * Notify about a clock event change. Called with clockevents_lock
149 * held.
150 */
151static void clockevents_do_notify(unsigned long reason, void *dev)
152{
153 raw_notifier_call_chain(&clockevents_chain, reason, dev);
154}
155
156/*
Li Zefan3eb05672008-02-08 04:19:25 -0800157 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800158 * released from the notifier call.
159 */
160static void clockevents_notify_released(void)
161{
162 struct clock_event_device *dev;
163
164 while (!list_empty(&clockevents_released)) {
165 dev = list_entry(clockevents_released.next,
166 struct clock_event_device, list);
167 list_del(&dev->list);
168 list_add(&dev->list, &clockevent_devices);
169 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
170 }
171}
172
173/**
174 * clockevents_register_device - register a clock event device
175 * @dev: device to register
176 */
177void clockevents_register_device(struct clock_event_device *dev)
178{
179 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030180 BUG_ON(!dev->cpumask);
181
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800182 spin_lock(&clockevents_lock);
183
184 list_add(&dev->list, &clockevent_devices);
185 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
186 clockevents_notify_released();
187
188 spin_unlock(&clockevents_lock);
189}
190
191/*
192 * Noop handler when we shut down an event device
193 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000194void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800195{
196}
197
198/**
199 * clockevents_exchange_device - release and request clock devices
200 * @old: device to release (can be NULL)
201 * @new: device to request (can be NULL)
202 *
203 * Called from the notifier chain. clockevents_lock is held already
204 */
205void clockevents_exchange_device(struct clock_event_device *old,
206 struct clock_event_device *new)
207{
208 unsigned long flags;
209
210 local_irq_save(flags);
211 /*
212 * Caller releases a clock event device. We queue it into the
213 * released list and do a notify add later.
214 */
215 if (old) {
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800216 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
217 list_del(&old->list);
218 list_add(&old->list, &clockevents_released);
219 }
220
221 if (new) {
222 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700223 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800224 }
225 local_irq_restore(flags);
226}
227
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200228#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800229/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800230 * clockevents_notify - notification about relevant events
231 */
232void clockevents_notify(unsigned long reason, void *arg)
233{
Li Zefan0b858e62008-02-08 04:19:24 -0800234 struct list_head *node, *tmp;
235
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800236 spin_lock(&clockevents_lock);
237 clockevents_do_notify(reason, arg);
238
239 switch (reason) {
240 case CLOCK_EVT_NOTIFY_CPU_DEAD:
241 /*
242 * Unregister the clock event devices which were
243 * released from the users in the notify chain.
244 */
Li Zefan0b858e62008-02-08 04:19:24 -0800245 list_for_each_safe(node, tmp, &clockevents_released)
246 list_del(node);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800247 break;
248 default:
249 break;
250 }
251 spin_unlock(&clockevents_lock);
252}
253EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200254#endif