blob: 0d37a6fd18af1048882ff5be1f0cf22389b3d82b [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>
Thomas Gleixnerd316c572007-02-16 01:28:00 -080020
H Hartley Sweeten8e1a9282009-10-16 18:19:01 -040021#include "tick-internal.h"
22
Thomas Gleixnerd316c572007-02-16 01:28:00 -080023/* The registered clock event devices */
24static LIST_HEAD(clockevent_devices);
25static LIST_HEAD(clockevents_released);
26
27/* Notification for clock events */
28static RAW_NOTIFIER_HEAD(clockevents_chain);
29
30/* Protection for the above */
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010031static DEFINE_RAW_SPINLOCK(clockevents_lock);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080032
Thomas Gleixnerf81d0a92013-09-24 21:50:23 +020033static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
34 bool ismax)
35{
36 u64 clc = (u64) latch << evt->shift;
37 u64 rnd;
38
39 if (unlikely(!evt->mult)) {
40 evt->mult = 1;
41 WARN_ON(1);
42 }
43 rnd = (u64) evt->mult - 1;
44
45 /*
46 * Upper bound sanity check. If the backwards conversion is
47 * not equal latch, we know that the above shift overflowed.
48 */
49 if ((clc >> evt->shift) != (u64)latch)
50 clc = ~0ULL;
51
52 /*
53 * Scaled math oddities:
54 *
55 * For mult <= (1 << shift) we can safely add mult - 1 to
56 * prevent integer rounding loss. So the backwards conversion
57 * from nsec to device ticks will be correct.
58 *
59 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
60 * need to be careful. Adding mult - 1 will result in a value
61 * which when converted back to device ticks can be larger
62 * than latch by up to (mult - 1) >> shift. For the min_delta
63 * calculation we still want to apply this in order to stay
64 * above the minimum device ticks limit. For the upper limit
65 * we would end up with a latch value larger than the upper
66 * limit of the device, so we omit the add to stay below the
67 * device upper boundary.
68 *
69 * Also omit the add if it would overflow the u64 boundary.
70 */
71 if ((~0ULL - clc > rnd) &&
72 (!ismax || evt->mult <= (1U << evt->shift)))
73 clc += rnd;
74
75 do_div(clc, evt->mult);
76
77 /* Deltas less than 1usec are pointless noise */
78 return clc > 1000 ? clc : 1000;
79}
80
Thomas Gleixnerd316c572007-02-16 01:28:00 -080081/**
82 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
83 * @latch: value to convert
84 * @evt: pointer to clock event device descriptor
85 *
86 * Math helper, returns latch value converted to nanoseconds (bound checked)
87 */
Jon Hunter97813f22009-08-18 12:45:11 -050088u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080089{
Thomas Gleixnerf81d0a92013-09-24 21:50:23 +020090 return cev_delta2ns(latch, evt, false);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080091}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090092EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080093
94/**
95 * clockevents_set_mode - set the operating mode of a clock event device
96 * @dev: device to modify
97 * @mode: new mode
98 *
99 * Must be called with interrupts disabled !
100 */
101void clockevents_set_mode(struct clock_event_device *dev,
102 enum clock_event_mode mode)
103{
104 if (dev->mode != mode) {
105 dev->set_mode(mode, dev);
106 dev->mode = mode;
Magnus Damm2d682592009-01-16 17:14:38 +0900107
108 /*
109 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
110 * on it, so fix it up and emit a warning:
111 */
112 if (mode == CLOCK_EVT_MODE_ONESHOT) {
113 if (unlikely(!dev->mult)) {
114 dev->mult = 1;
115 WARN_ON(1);
116 }
117 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800118 }
119}
120
121/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700122 * clockevents_shutdown - shutdown the device and clear next_event
123 * @dev: device to shutdown
124 */
125void clockevents_shutdown(struct clock_event_device *dev)
126{
127 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
128 dev->next_event.tv64 = KTIME_MAX;
129}
130
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200131#ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
132
133/* Limit min_delta to a jiffie */
134#define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
135
136/**
137 * clockevents_increase_min_delta - raise minimum delta of a clock event device
138 * @dev: device to increase the minimum delta
139 *
140 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
141 */
142static int clockevents_increase_min_delta(struct clock_event_device *dev)
143{
144 /* Nothing to do if we already reached the limit */
145 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
146 printk(KERN_WARNING "CE: Reprogramming failure. Giving up\n");
147 dev->next_event.tv64 = KTIME_MAX;
148 return -ETIME;
149 }
150
151 if (dev->min_delta_ns < 5000)
152 dev->min_delta_ns = 5000;
153 else
154 dev->min_delta_ns += dev->min_delta_ns >> 1;
155
156 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
157 dev->min_delta_ns = MIN_DELTA_LIMIT;
158
159 printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
160 dev->name ? dev->name : "?",
161 (unsigned long long) dev->min_delta_ns);
162 return 0;
163}
164
165/**
166 * clockevents_program_min_delta - Set clock event device to the minimum delay.
167 * @dev: device to program
168 *
169 * Returns 0 on success, -ETIME when the retry loop failed.
170 */
171static int clockevents_program_min_delta(struct clock_event_device *dev)
172{
173 unsigned long long clc;
174 int64_t delta;
175 int i;
176
177 for (i = 0;;) {
178 delta = dev->min_delta_ns;
179 dev->next_event = ktime_add_ns(ktime_get(), delta);
180
181 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
182 return 0;
183
184 dev->retries++;
185 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
186 if (dev->set_next_event((unsigned long) clc, dev) == 0)
187 return 0;
188
189 if (++i > 2) {
190 /*
191 * We tried 3 times to program the device with the
192 * given min_delta_ns. Try to increase the minimum
193 * delta, if that fails as well get out of here.
194 */
195 if (clockevents_increase_min_delta(dev))
196 return -ETIME;
197 i = 0;
198 }
199 }
200}
201
202#else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
203
204/**
205 * clockevents_program_min_delta - Set clock event device to the minimum delay.
206 * @dev: device to program
207 *
208 * Returns 0 on success, -ETIME when the retry loop failed.
209 */
210static int clockevents_program_min_delta(struct clock_event_device *dev)
211{
212 unsigned long long clc;
213 int64_t delta;
214
215 delta = dev->min_delta_ns;
216 dev->next_event = ktime_add_ns(ktime_get(), delta);
217
218 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
219 return 0;
220
221 dev->retries++;
222 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
223 return dev->set_next_event((unsigned long) clc, dev);
224}
225
226#endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
227
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700228/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800229 * clockevents_program_event - Reprogram the clock event device.
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200230 * @dev: device to program
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800231 * @expires: absolute expiry time (monotonic clock)
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200232 * @force: program minimum delay if expires can not be set
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800233 *
234 * Returns 0 on success, -ETIME when the event is in the past.
235 */
236int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200237 bool force)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800238{
239 unsigned long long clc;
240 int64_t delta;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200241 int rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800242
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100243 if (unlikely(expires.tv64 < 0)) {
244 WARN_ON_ONCE(1);
245 return -ETIME;
246 }
247
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800248 dev->next_event = expires;
249
250 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
251 return 0;
252
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200253 /* Shortcut for clockevent devices that can deal with ktime. */
254 if (dev->features & CLOCK_EVT_FEAT_KTIME)
255 return dev->set_next_ktime(expires, dev);
256
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200257 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
258 if (delta <= 0)
259 return force ? clockevents_program_min_delta(dev) : -ETIME;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800260
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200261 delta = min(delta, (int64_t) dev->max_delta_ns);
262 delta = max(delta, (int64_t) dev->min_delta_ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800263
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200264 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
265 rc = dev->set_next_event((unsigned long) clc, dev);
266
267 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800268}
269
270/**
271 * clockevents_register_notifier - register a clock events change listener
272 */
273int clockevents_register_notifier(struct notifier_block *nb)
274{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700275 unsigned long flags;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800276 int ret;
277
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100278 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800279 ret = raw_notifier_chain_register(&clockevents_chain, nb);
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100280 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800281
282 return ret;
283}
284
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800285/*
286 * Notify about a clock event change. Called with clockevents_lock
287 * held.
288 */
289static void clockevents_do_notify(unsigned long reason, void *dev)
290{
291 raw_notifier_call_chain(&clockevents_chain, reason, dev);
292}
293
294/*
Li Zefan3eb05672008-02-08 04:19:25 -0800295 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800296 * released from the notifier call.
297 */
298static void clockevents_notify_released(void)
299{
300 struct clock_event_device *dev;
301
302 while (!list_empty(&clockevents_released)) {
303 dev = list_entry(clockevents_released.next,
304 struct clock_event_device, list);
305 list_del(&dev->list);
306 list_add(&dev->list, &clockevent_devices);
307 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
308 }
309}
310
311/**
312 * clockevents_register_device - register a clock event device
313 * @dev: device to register
314 */
315void clockevents_register_device(struct clock_event_device *dev)
316{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700317 unsigned long flags;
318
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800319 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner1b054b62011-06-03 11:13:33 +0200320 if (!dev->cpumask) {
321 WARN_ON(num_possible_cpus() > 1);
322 dev->cpumask = cpumask_of(smp_processor_id());
323 }
Rusty Russell320ab2b2008-12-13 21:20:26 +1030324
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100325 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800326
327 list_add(&dev->list, &clockevent_devices);
328 clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
329 clockevents_notify_released();
330
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100331 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800332}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900333EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800334
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000335static void clockevents_config(struct clock_event_device *dev,
336 u32 freq)
337{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200338 u64 sec;
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000339
340 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
341 return;
342
343 /*
344 * Calculate the maximum number of seconds we can sleep. Limit
345 * to 10 minutes for hardware which can program more than
346 * 32bit ticks so we still get reasonable conversion values.
347 */
348 sec = dev->max_delta_ticks;
349 do_div(sec, freq);
350 if (!sec)
351 sec = 1;
352 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
353 sec = 600;
354
355 clockevents_calc_mult_shift(dev, freq, sec);
Thomas Gleixnerf81d0a92013-09-24 21:50:23 +0200356 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
357 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000358}
359
360/**
361 * clockevents_config_and_register - Configure and register a clock event device
362 * @dev: device to register
363 * @freq: The clock frequency
364 * @min_delta: The minimum clock ticks to program in oneshot mode
365 * @max_delta: The maximum clock ticks to program in oneshot mode
366 *
367 * min/max_delta can be 0 for devices which do not support oneshot mode.
368 */
369void clockevents_config_and_register(struct clock_event_device *dev,
370 u32 freq, unsigned long min_delta,
371 unsigned long max_delta)
372{
373 dev->min_delta_ticks = min_delta;
374 dev->max_delta_ticks = max_delta;
375 clockevents_config(dev, freq);
376 clockevents_register_device(dev);
377}
378
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000379/**
380 * clockevents_update_freq - Update frequency and reprogram a clock event device.
381 * @dev: device to modify
382 * @freq: new device frequency
383 *
384 * Reconfigure and reprogram a clock event device in oneshot
385 * mode. Must be called on the cpu for which the device delivers per
386 * cpu timer events with interrupts disabled! Returns 0 on success,
387 * -ETIME when the event is in the past.
388 */
389int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
390{
391 clockevents_config(dev, freq);
392
393 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
394 return 0;
395
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200396 return clockevents_program_event(dev, dev->next_event, false);
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000397}
398
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800399/*
400 * Noop handler when we shut down an event device
401 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000402void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800403{
404}
405
406/**
407 * clockevents_exchange_device - release and request clock devices
408 * @old: device to release (can be NULL)
409 * @new: device to request (can be NULL)
410 *
411 * Called from the notifier chain. clockevents_lock is held already
412 */
413void clockevents_exchange_device(struct clock_event_device *old,
414 struct clock_event_device *new)
415{
416 unsigned long flags;
417
418 local_irq_save(flags);
419 /*
420 * Caller releases a clock event device. We queue it into the
421 * released list and do a notify add later.
422 */
423 if (old) {
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800424 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
425 list_del(&old->list);
426 list_add(&old->list, &clockevents_released);
427 }
428
429 if (new) {
430 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700431 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800432 }
433 local_irq_restore(flags);
434}
435
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200436#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800437/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800438 * clockevents_notify - notification about relevant events
439 */
440void clockevents_notify(unsigned long reason, void *arg)
441{
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100442 struct clock_event_device *dev, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700443 unsigned long flags;
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100444 int cpu;
Li Zefan0b858e62008-02-08 04:19:24 -0800445
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100446 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800447 clockevents_do_notify(reason, arg);
448
449 switch (reason) {
450 case CLOCK_EVT_NOTIFY_CPU_DEAD:
451 /*
452 * Unregister the clock event devices which were
453 * released from the users in the notify chain.
454 */
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100455 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
456 list_del(&dev->list);
457 /*
458 * Now check whether the CPU has left unused per cpu devices
459 */
460 cpu = *((int *)arg);
461 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
462 if (cpumask_test_cpu(cpu, dev->cpumask) &&
Xiaotian Fengea9d8e32010-01-07 11:22:44 +0800463 cpumask_weight(dev->cpumask) == 1 &&
464 !tick_is_broadcast_device(dev)) {
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100465 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
466 list_del(&dev->list);
467 }
468 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800469 break;
470 default:
471 break;
472 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100473 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800474}
475EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200476#endif