]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - kernel/time/clockevents.c
timers: Logic to move non pinned timers
[mirror_ubuntu-artful-kernel.git] / kernel / time / clockevents.c
1 /*
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 #include <linux/tick.h>
22
23 /* The registered clock event devices */
24 static LIST_HEAD(clockevent_devices);
25 static LIST_HEAD(clockevents_released);
26
27 /* Notification for clock events */
28 static RAW_NOTIFIER_HEAD(clockevents_chain);
29
30 /* Protection for the above */
31 static DEFINE_SPINLOCK(clockevents_lock);
32
33 /**
34 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
35 * @latch: value to convert
36 * @evt: pointer to clock event device descriptor
37 *
38 * Math helper, returns latch value converted to nanoseconds (bound checked)
39 */
40 unsigned long clockevent_delta2ns(unsigned long latch,
41 struct clock_event_device *evt)
42 {
43 u64 clc = ((u64) latch << evt->shift);
44
45 if (unlikely(!evt->mult)) {
46 evt->mult = 1;
47 WARN_ON(1);
48 }
49
50 do_div(clc, evt->mult);
51 if (clc < 1000)
52 clc = 1000;
53 if (clc > LONG_MAX)
54 clc = LONG_MAX;
55
56 return (unsigned long) clc;
57 }
58
59 /**
60 * clockevents_set_mode - set the operating mode of a clock event device
61 * @dev: device to modify
62 * @mode: new mode
63 *
64 * Must be called with interrupts disabled !
65 */
66 void clockevents_set_mode(struct clock_event_device *dev,
67 enum clock_event_mode mode)
68 {
69 if (dev->mode != mode) {
70 dev->set_mode(mode, dev);
71 dev->mode = mode;
72
73 /*
74 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
75 * on it, so fix it up and emit a warning:
76 */
77 if (mode == CLOCK_EVT_MODE_ONESHOT) {
78 if (unlikely(!dev->mult)) {
79 dev->mult = 1;
80 WARN_ON(1);
81 }
82 }
83 }
84 }
85
86 /**
87 * clockevents_shutdown - shutdown the device and clear next_event
88 * @dev: device to shutdown
89 */
90 void clockevents_shutdown(struct clock_event_device *dev)
91 {
92 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
93 dev->next_event.tv64 = KTIME_MAX;
94 }
95
96 /**
97 * clockevents_program_event - Reprogram the clock event device.
98 * @expires: absolute expiry time (monotonic clock)
99 *
100 * Returns 0 on success, -ETIME when the event is in the past.
101 */
102 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
103 ktime_t now)
104 {
105 unsigned long long clc;
106 int64_t delta;
107
108 if (unlikely(expires.tv64 < 0)) {
109 WARN_ON_ONCE(1);
110 return -ETIME;
111 }
112
113 delta = ktime_to_ns(ktime_sub(expires, now));
114
115 if (delta <= 0)
116 return -ETIME;
117
118 dev->next_event = expires;
119
120 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
121 return 0;
122
123 if (delta > dev->max_delta_ns)
124 delta = dev->max_delta_ns;
125 if (delta < dev->min_delta_ns)
126 delta = dev->min_delta_ns;
127
128 clc = delta * dev->mult;
129 clc >>= dev->shift;
130
131 return dev->set_next_event((unsigned long) clc, dev);
132 }
133
134 /**
135 * clockevents_register_notifier - register a clock events change listener
136 */
137 int clockevents_register_notifier(struct notifier_block *nb)
138 {
139 int ret;
140
141 spin_lock(&clockevents_lock);
142 ret = raw_notifier_chain_register(&clockevents_chain, nb);
143 spin_unlock(&clockevents_lock);
144
145 return ret;
146 }
147
148 /*
149 * Notify about a clock event change. Called with clockevents_lock
150 * held.
151 */
152 static void clockevents_do_notify(unsigned long reason, void *dev)
153 {
154 raw_notifier_call_chain(&clockevents_chain, reason, dev);
155 }
156
157 /*
158 * Called after a notify add to make devices available which were
159 * released from the notifier call.
160 */
161 static void clockevents_notify_released(void)
162 {
163 struct clock_event_device *dev;
164
165 while (!list_empty(&clockevents_released)) {
166 dev = list_entry(clockevents_released.next,
167 struct clock_event_device, list);
168 list_del(&dev->list);
169 list_add(&dev->list, &clockevent_devices);
170 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
171 }
172 }
173
174 /**
175 * clockevents_register_device - register a clock event device
176 * @dev: device to register
177 */
178 void clockevents_register_device(struct clock_event_device *dev)
179 {
180 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
181 BUG_ON(!dev->cpumask);
182
183 spin_lock(&clockevents_lock);
184
185 list_add(&dev->list, &clockevent_devices);
186 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
187 clockevents_notify_released();
188
189 spin_unlock(&clockevents_lock);
190 }
191
192 /*
193 * Noop handler when we shut down an event device
194 */
195 void clockevents_handle_noop(struct clock_event_device *dev)
196 {
197 }
198
199 /**
200 * clockevents_exchange_device - release and request clock devices
201 * @old: device to release (can be NULL)
202 * @new: device to request (can be NULL)
203 *
204 * Called from the notifier chain. clockevents_lock is held already
205 */
206 void clockevents_exchange_device(struct clock_event_device *old,
207 struct clock_event_device *new)
208 {
209 unsigned long flags;
210
211 local_irq_save(flags);
212 /*
213 * Caller releases a clock event device. We queue it into the
214 * released list and do a notify add later.
215 */
216 if (old) {
217 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
218 list_del(&old->list);
219 list_add(&old->list, &clockevents_released);
220 }
221
222 if (new) {
223 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
224 clockevents_shutdown(new);
225 }
226 local_irq_restore(flags);
227 }
228
229 #ifdef CONFIG_GENERIC_CLOCKEVENTS
230 /**
231 * clockevents_notify - notification about relevant events
232 */
233 void clockevents_notify(unsigned long reason, void *arg)
234 {
235 struct list_head *node, *tmp;
236
237 spin_lock(&clockevents_lock);
238 clockevents_do_notify(reason, arg);
239
240 switch (reason) {
241 case CLOCK_EVT_NOTIFY_CPU_DEAD:
242 /*
243 * Unregister the clock event devices which were
244 * released from the users in the notify chain.
245 */
246 list_for_each_safe(node, tmp, &clockevents_released)
247 list_del(node);
248 break;
249 default:
250 break;
251 }
252 spin_unlock(&clockevents_lock);
253 }
254 EXPORT_SYMBOL_GPL(clockevents_notify);
255
256 ktime_t clockevents_get_next_event(int cpu)
257 {
258 struct tick_device *td;
259 struct clock_event_device *dev;
260
261 td = &per_cpu(tick_cpu_device, cpu);
262 dev = td->evtdev;
263
264 return dev->next_event;
265 }
266 #endif