blob: 1b0ea63de69cde9b983afca36c5c7ee1931b474d [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>
Thomas Gleixnerd316c572007-02-16 01:28:00 -080018#include <linux/smp.h>
Thomas Gleixner501f8672013-04-25 20:31:49 +000019#include <linux/device.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);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080026/* Protection for the above */
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010027static DEFINE_RAW_SPINLOCK(clockevents_lock);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000028/* Protection for unbind operations */
29static DEFINE_MUTEX(clockevents_mutex);
30
31struct ce_unbind {
32 struct clock_event_device *ce;
33 int res;
34};
Thomas Gleixnerd316c572007-02-16 01:28:00 -080035
Thomas Gleixner97b94102013-09-24 21:50:23 +020036static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
37 bool ismax)
38{
39 u64 clc = (u64) latch << evt->shift;
40 u64 rnd;
41
42 if (unlikely(!evt->mult)) {
43 evt->mult = 1;
44 WARN_ON(1);
45 }
46 rnd = (u64) evt->mult - 1;
47
48 /*
49 * Upper bound sanity check. If the backwards conversion is
50 * not equal latch, we know that the above shift overflowed.
51 */
52 if ((clc >> evt->shift) != (u64)latch)
53 clc = ~0ULL;
54
55 /*
56 * Scaled math oddities:
57 *
58 * For mult <= (1 << shift) we can safely add mult - 1 to
59 * prevent integer rounding loss. So the backwards conversion
60 * from nsec to device ticks will be correct.
61 *
62 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
63 * need to be careful. Adding mult - 1 will result in a value
64 * which when converted back to device ticks can be larger
65 * than latch by up to (mult - 1) >> shift. For the min_delta
66 * calculation we still want to apply this in order to stay
67 * above the minimum device ticks limit. For the upper limit
68 * we would end up with a latch value larger than the upper
69 * limit of the device, so we omit the add to stay below the
70 * device upper boundary.
71 *
72 * Also omit the add if it would overflow the u64 boundary.
73 */
74 if ((~0ULL - clc > rnd) &&
Thomas Gleixner10632002014-10-20 15:07:50 +040075 (!ismax || evt->mult <= (1ULL << evt->shift)))
Thomas Gleixner97b94102013-09-24 21:50:23 +020076 clc += rnd;
77
78 do_div(clc, evt->mult);
79
80 /* Deltas less than 1usec are pointless noise */
81 return clc > 1000 ? clc : 1000;
82}
83
Thomas Gleixnerd316c572007-02-16 01:28:00 -080084/**
85 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
86 * @latch: value to convert
87 * @evt: pointer to clock event device descriptor
88 *
89 * Math helper, returns latch value converted to nanoseconds (bound checked)
90 */
Jon Hunter97813f22009-08-18 12:45:11 -050091u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080092{
Thomas Gleixner97b94102013-09-24 21:50:23 +020093 return cev_delta2ns(latch, evt, false);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080094}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090095EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080096
Viresh Kumarbd624d72015-02-13 08:54:56 +080097static int __clockevents_set_mode(struct clock_event_device *dev,
98 enum clock_event_mode mode)
99{
100 /* Transition with legacy set_mode() callback */
101 if (dev->set_mode) {
102 /* Legacy callback doesn't support new modes */
Viresh Kumar554ef382015-02-27 17:21:32 +0530103 if (mode > CLOCK_EVT_MODE_ONESHOT)
Viresh Kumarbd624d72015-02-13 08:54:56 +0800104 return -ENOSYS;
105 dev->set_mode(mode, dev);
106 return 0;
107 }
108
109 if (dev->features & CLOCK_EVT_FEAT_DUMMY)
110 return 0;
111
112 /* Transition with new mode-specific callbacks */
113 switch (mode) {
114 case CLOCK_EVT_MODE_UNUSED:
115 /*
116 * This is an internal state, which is guaranteed to go from
117 * SHUTDOWN to UNUSED. No driver interaction required.
118 */
119 return 0;
120
121 case CLOCK_EVT_MODE_SHUTDOWN:
122 return dev->set_mode_shutdown(dev);
123
124 case CLOCK_EVT_MODE_PERIODIC:
125 /* Core internal bug */
126 if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
127 return -ENOSYS;
128 return dev->set_mode_periodic(dev);
129
130 case CLOCK_EVT_MODE_ONESHOT:
131 /* Core internal bug */
132 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
133 return -ENOSYS;
134 return dev->set_mode_oneshot(dev);
135
Viresh Kumarbd624d72015-02-13 08:54:56 +0800136 default:
137 return -ENOSYS;
138 }
139}
140
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800141/**
142 * clockevents_set_mode - set the operating mode of a clock event device
143 * @dev: device to modify
144 * @mode: new mode
145 *
146 * Must be called with interrupts disabled !
147 */
148void clockevents_set_mode(struct clock_event_device *dev,
149 enum clock_event_mode mode)
150{
151 if (dev->mode != mode) {
Viresh Kumarbd624d72015-02-13 08:54:56 +0800152 if (__clockevents_set_mode(dev, mode))
153 return;
154
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800155 dev->mode = mode;
Magnus Damm2d682592009-01-16 17:14:38 +0900156
157 /*
158 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
159 * on it, so fix it up and emit a warning:
160 */
161 if (mode == CLOCK_EVT_MODE_ONESHOT) {
162 if (unlikely(!dev->mult)) {
163 dev->mult = 1;
164 WARN_ON(1);
165 }
166 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800167 }
168}
169
170/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700171 * clockevents_shutdown - shutdown the device and clear next_event
172 * @dev: device to shutdown
173 */
174void clockevents_shutdown(struct clock_event_device *dev)
175{
176 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
177 dev->next_event.tv64 = KTIME_MAX;
178}
179
Viresh Kumar554ef382015-02-27 17:21:32 +0530180/**
181 * clockevents_tick_resume - Resume the tick device before using it again
182 * @dev: device to resume
183 */
184int clockevents_tick_resume(struct clock_event_device *dev)
185{
186 int ret = 0;
187
188 if (dev->set_mode)
189 dev->set_mode(CLOCK_EVT_MODE_RESUME, dev);
190 else if (dev->tick_resume)
191 ret = dev->tick_resume(dev);
192
193 if (likely(!ret))
194 dev->mode = CLOCK_EVT_MODE_RESUME;
195
196 return ret;
197}
198
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200199#ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
200
201/* Limit min_delta to a jiffie */
202#define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
203
204/**
205 * clockevents_increase_min_delta - raise minimum delta of a clock event device
206 * @dev: device to increase the minimum delta
207 *
208 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
209 */
210static int clockevents_increase_min_delta(struct clock_event_device *dev)
211{
212 /* Nothing to do if we already reached the limit */
213 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
Jan Kara504d5872014-08-01 12:20:02 +0200214 printk_deferred(KERN_WARNING
215 "CE: Reprogramming failure. Giving up\n");
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200216 dev->next_event.tv64 = KTIME_MAX;
217 return -ETIME;
218 }
219
220 if (dev->min_delta_ns < 5000)
221 dev->min_delta_ns = 5000;
222 else
223 dev->min_delta_ns += dev->min_delta_ns >> 1;
224
225 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
226 dev->min_delta_ns = MIN_DELTA_LIMIT;
227
Jan Kara504d5872014-08-01 12:20:02 +0200228 printk_deferred(KERN_WARNING
229 "CE: %s increased min_delta_ns to %llu nsec\n",
230 dev->name ? dev->name : "?",
231 (unsigned long long) dev->min_delta_ns);
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200232 return 0;
233}
234
235/**
236 * clockevents_program_min_delta - Set clock event device to the minimum delay.
237 * @dev: device to program
238 *
239 * Returns 0 on success, -ETIME when the retry loop failed.
240 */
241static int clockevents_program_min_delta(struct clock_event_device *dev)
242{
243 unsigned long long clc;
244 int64_t delta;
245 int i;
246
247 for (i = 0;;) {
248 delta = dev->min_delta_ns;
249 dev->next_event = ktime_add_ns(ktime_get(), delta);
250
251 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
252 return 0;
253
254 dev->retries++;
255 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
256 if (dev->set_next_event((unsigned long) clc, dev) == 0)
257 return 0;
258
259 if (++i > 2) {
260 /*
261 * We tried 3 times to program the device with the
262 * given min_delta_ns. Try to increase the minimum
263 * delta, if that fails as well get out of here.
264 */
265 if (clockevents_increase_min_delta(dev))
266 return -ETIME;
267 i = 0;
268 }
269 }
270}
271
272#else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
273
274/**
275 * clockevents_program_min_delta - Set clock event device to the minimum delay.
276 * @dev: device to program
277 *
278 * Returns 0 on success, -ETIME when the retry loop failed.
279 */
280static int clockevents_program_min_delta(struct clock_event_device *dev)
281{
282 unsigned long long clc;
283 int64_t delta;
284
285 delta = dev->min_delta_ns;
286 dev->next_event = ktime_add_ns(ktime_get(), delta);
287
288 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
289 return 0;
290
291 dev->retries++;
292 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
293 return dev->set_next_event((unsigned long) clc, dev);
294}
295
296#endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
297
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700298/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800299 * clockevents_program_event - Reprogram the clock event device.
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200300 * @dev: device to program
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800301 * @expires: absolute expiry time (monotonic clock)
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200302 * @force: program minimum delay if expires can not be set
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800303 *
304 * Returns 0 on success, -ETIME when the event is in the past.
305 */
306int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200307 bool force)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800308{
309 unsigned long long clc;
310 int64_t delta;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200311 int rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800312
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100313 if (unlikely(expires.tv64 < 0)) {
314 WARN_ON_ONCE(1);
315 return -ETIME;
316 }
317
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800318 dev->next_event = expires;
319
320 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
321 return 0;
322
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200323 /* Shortcut for clockevent devices that can deal with ktime. */
324 if (dev->features & CLOCK_EVT_FEAT_KTIME)
325 return dev->set_next_ktime(expires, dev);
326
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200327 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
328 if (delta <= 0)
329 return force ? clockevents_program_min_delta(dev) : -ETIME;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800330
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200331 delta = min(delta, (int64_t) dev->max_delta_ns);
332 delta = max(delta, (int64_t) dev->min_delta_ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800333
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200334 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
335 rc = dev->set_next_event((unsigned long) clc, dev);
336
337 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800338}
339
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800340/*
Li Zefan3eb05672008-02-08 04:19:25 -0800341 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800342 * released from the notifier call.
343 */
344static void clockevents_notify_released(void)
345{
346 struct clock_event_device *dev;
347
348 while (!list_empty(&clockevents_released)) {
349 dev = list_entry(clockevents_released.next,
350 struct clock_event_device, list);
351 list_del(&dev->list);
352 list_add(&dev->list, &clockevent_devices);
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000353 tick_check_new_device(dev);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800354 }
355}
356
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000357/*
358 * Try to install a replacement clock event device
359 */
360static int clockevents_replace(struct clock_event_device *ced)
361{
362 struct clock_event_device *dev, *newdev = NULL;
363
364 list_for_each_entry(dev, &clockevent_devices, list) {
365 if (dev == ced || dev->mode != CLOCK_EVT_MODE_UNUSED)
366 continue;
367
368 if (!tick_check_replacement(newdev, dev))
369 continue;
370
371 if (!try_module_get(dev->owner))
372 continue;
373
374 if (newdev)
375 module_put(newdev->owner);
376 newdev = dev;
377 }
378 if (newdev) {
379 tick_install_replacement(newdev);
380 list_del_init(&ced->list);
381 }
382 return newdev ? 0 : -EBUSY;
383}
384
385/*
386 * Called with clockevents_mutex and clockevents_lock held
387 */
388static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
389{
390 /* Fast track. Device is unused */
391 if (ced->mode == CLOCK_EVT_MODE_UNUSED) {
392 list_del_init(&ced->list);
393 return 0;
394 }
395
396 return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
397}
398
399/*
400 * SMP function call to unbind a device
401 */
402static void __clockevents_unbind(void *arg)
403{
404 struct ce_unbind *cu = arg;
405 int res;
406
407 raw_spin_lock(&clockevents_lock);
408 res = __clockevents_try_unbind(cu->ce, smp_processor_id());
409 if (res == -EAGAIN)
410 res = clockevents_replace(cu->ce);
411 cu->res = res;
412 raw_spin_unlock(&clockevents_lock);
413}
414
415/*
416 * Issues smp function call to unbind a per cpu device. Called with
417 * clockevents_mutex held.
418 */
419static int clockevents_unbind(struct clock_event_device *ced, int cpu)
420{
421 struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
422
423 smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
424 return cu.res;
425}
426
427/*
428 * Unbind a clockevents device.
429 */
430int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
431{
432 int ret;
433
434 mutex_lock(&clockevents_mutex);
435 ret = clockevents_unbind(ced, cpu);
436 mutex_unlock(&clockevents_mutex);
437 return ret;
438}
439EXPORT_SYMBOL_GPL(clockevents_unbind);
440
Viresh Kumarbd624d72015-02-13 08:54:56 +0800441/* Sanity check of mode transition callbacks */
442static int clockevents_sanity_check(struct clock_event_device *dev)
443{
444 /* Legacy set_mode() callback */
445 if (dev->set_mode) {
446 /* We shouldn't be supporting new modes now */
447 WARN_ON(dev->set_mode_periodic || dev->set_mode_oneshot ||
Viresh Kumar554ef382015-02-27 17:21:32 +0530448 dev->set_mode_shutdown || dev->tick_resume);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800449 return 0;
450 }
451
452 if (dev->features & CLOCK_EVT_FEAT_DUMMY)
453 return 0;
454
455 /* New mode-specific callbacks */
456 if (!dev->set_mode_shutdown)
457 return -EINVAL;
458
459 if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
460 !dev->set_mode_periodic)
461 return -EINVAL;
462
463 if ((dev->features & CLOCK_EVT_FEAT_ONESHOT) &&
464 !dev->set_mode_oneshot)
465 return -EINVAL;
466
467 return 0;
468}
469
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800470/**
471 * clockevents_register_device - register a clock event device
472 * @dev: device to register
473 */
474void clockevents_register_device(struct clock_event_device *dev)
475{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700476 unsigned long flags;
477
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800478 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800479 BUG_ON(clockevents_sanity_check(dev));
480
Thomas Gleixner1b054b62011-06-03 11:13:33 +0200481 if (!dev->cpumask) {
482 WARN_ON(num_possible_cpus() > 1);
483 dev->cpumask = cpumask_of(smp_processor_id());
484 }
Rusty Russell320ab2b2008-12-13 21:20:26 +1030485
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100486 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800487
488 list_add(&dev->list, &clockevent_devices);
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000489 tick_check_new_device(dev);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800490 clockevents_notify_released();
491
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100492 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800493}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900494EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800495
Magnus Damme5400322012-05-09 23:39:34 +0900496void clockevents_config(struct clock_event_device *dev, u32 freq)
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000497{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200498 u64 sec;
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000499
500 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
501 return;
502
503 /*
504 * Calculate the maximum number of seconds we can sleep. Limit
505 * to 10 minutes for hardware which can program more than
506 * 32bit ticks so we still get reasonable conversion values.
507 */
508 sec = dev->max_delta_ticks;
509 do_div(sec, freq);
510 if (!sec)
511 sec = 1;
512 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
513 sec = 600;
514
515 clockevents_calc_mult_shift(dev, freq, sec);
Thomas Gleixner97b94102013-09-24 21:50:23 +0200516 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
517 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000518}
519
520/**
521 * clockevents_config_and_register - Configure and register a clock event device
522 * @dev: device to register
523 * @freq: The clock frequency
524 * @min_delta: The minimum clock ticks to program in oneshot mode
525 * @max_delta: The maximum clock ticks to program in oneshot mode
526 *
527 * min/max_delta can be 0 for devices which do not support oneshot mode.
528 */
529void clockevents_config_and_register(struct clock_event_device *dev,
530 u32 freq, unsigned long min_delta,
531 unsigned long max_delta)
532{
533 dev->min_delta_ticks = min_delta;
534 dev->max_delta_ticks = max_delta;
535 clockevents_config(dev, freq);
536 clockevents_register_device(dev);
537}
Shawn Guoc35ef952013-01-12 11:50:04 +0000538EXPORT_SYMBOL_GPL(clockevents_config_and_register);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000539
Thomas Gleixner627ee792014-02-03 14:34:31 -0800540int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000541{
542 clockevents_config(dev, freq);
543
Soren Brinkmannfe79a9b2014-02-03 14:34:32 -0800544 if (dev->mode == CLOCK_EVT_MODE_ONESHOT)
545 return clockevents_program_event(dev, dev->next_event, false);
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000546
Soren Brinkmannfe79a9b2014-02-03 14:34:32 -0800547 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
Viresh Kumarbd624d72015-02-13 08:54:56 +0800548 return __clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
Soren Brinkmannfe79a9b2014-02-03 14:34:32 -0800549
550 return 0;
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000551}
552
Thomas Gleixner627ee792014-02-03 14:34:31 -0800553/**
554 * clockevents_update_freq - Update frequency and reprogram a clock event device.
555 * @dev: device to modify
556 * @freq: new device frequency
557 *
558 * Reconfigure and reprogram a clock event device in oneshot
559 * mode. Must be called on the cpu for which the device delivers per
560 * cpu timer events. If called for the broadcast device the core takes
561 * care of serialization.
562 *
563 * Returns 0 on success, -ETIME when the event is in the past.
564 */
565int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
566{
567 unsigned long flags;
568 int ret;
569
570 local_irq_save(flags);
571 ret = tick_broadcast_update_freq(dev, freq);
572 if (ret == -ENODEV)
573 ret = __clockevents_update_freq(dev, freq);
574 local_irq_restore(flags);
575 return ret;
576}
577
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800578/*
579 * Noop handler when we shut down an event device
580 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000581void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800582{
583}
584
585/**
586 * clockevents_exchange_device - release and request clock devices
587 * @old: device to release (can be NULL)
588 * @new: device to request (can be NULL)
589 *
590 * Called from the notifier chain. clockevents_lock is held already
591 */
592void clockevents_exchange_device(struct clock_event_device *old,
593 struct clock_event_device *new)
594{
595 unsigned long flags;
596
597 local_irq_save(flags);
598 /*
599 * Caller releases a clock event device. We queue it into the
600 * released list and do a notify add later.
601 */
602 if (old) {
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000603 module_put(old->owner);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800604 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
605 list_del(&old->list);
606 list_add(&old->list, &clockevents_released);
607 }
608
609 if (new) {
610 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700611 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800612 }
613 local_irq_restore(flags);
614}
615
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200616/**
617 * clockevents_suspend - suspend clock devices
618 */
619void clockevents_suspend(void)
620{
621 struct clock_event_device *dev;
622
623 list_for_each_entry_reverse(dev, &clockevent_devices, list)
624 if (dev->suspend)
625 dev->suspend(dev);
626}
627
628/**
629 * clockevents_resume - resume clock devices
630 */
631void clockevents_resume(void)
632{
633 struct clock_event_device *dev;
634
635 list_for_each_entry(dev, &clockevent_devices, list)
636 if (dev->resume)
637 dev->resume(dev);
638}
639
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200640#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800641/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800642 * clockevents_notify - notification about relevant events
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530643 * Returns 0 on success, any other value on error
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800644 */
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530645int clockevents_notify(unsigned long reason, void *arg)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800646{
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100647 struct clock_event_device *dev, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700648 unsigned long flags;
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530649 int cpu, ret = 0;
Li Zefan0b858e62008-02-08 04:19:24 -0800650
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100651 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800652
653 switch (reason) {
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000654 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
655 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
656 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
657 tick_broadcast_on_off(reason, arg);
658 break;
659
660 case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
661 case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530662 ret = tick_broadcast_oneshot_control(reason);
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000663 break;
664
665 case CLOCK_EVT_NOTIFY_CPU_DYING:
666 tick_handover_do_timer(arg);
667 break;
668
669 case CLOCK_EVT_NOTIFY_SUSPEND:
670 tick_suspend();
671 tick_suspend_broadcast();
672 break;
673
674 case CLOCK_EVT_NOTIFY_RESUME:
675 tick_resume();
676 break;
677
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800678 case CLOCK_EVT_NOTIFY_CPU_DEAD:
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000679 tick_shutdown_broadcast_oneshot(arg);
680 tick_shutdown_broadcast(arg);
681 tick_shutdown(arg);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800682 /*
683 * Unregister the clock event devices which were
684 * released from the users in the notify chain.
685 */
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100686 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
687 list_del(&dev->list);
688 /*
689 * Now check whether the CPU has left unused per cpu devices
690 */
691 cpu = *((int *)arg);
692 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
693 if (cpumask_test_cpu(cpu, dev->cpumask) &&
Xiaotian Fengea9d8e32010-01-07 11:22:44 +0800694 cpumask_weight(dev->cpumask) == 1 &&
695 !tick_is_broadcast_device(dev)) {
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100696 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
697 list_del(&dev->list);
698 }
699 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800700 break;
701 default:
702 break;
703 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100704 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530705 return ret;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800706}
707EXPORT_SYMBOL_GPL(clockevents_notify);
Thomas Gleixner501f8672013-04-25 20:31:49 +0000708
709#ifdef CONFIG_SYSFS
710struct bus_type clockevents_subsys = {
711 .name = "clockevents",
712 .dev_name = "clockevent",
713};
714
715static DEFINE_PER_CPU(struct device, tick_percpu_dev);
716static struct tick_device *tick_get_tick_dev(struct device *dev);
717
718static ssize_t sysfs_show_current_tick_dev(struct device *dev,
719 struct device_attribute *attr,
720 char *buf)
721{
722 struct tick_device *td;
723 ssize_t count = 0;
724
725 raw_spin_lock_irq(&clockevents_lock);
726 td = tick_get_tick_dev(dev);
727 if (td && td->evtdev)
728 count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
729 raw_spin_unlock_irq(&clockevents_lock);
730 return count;
731}
732static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
733
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000734/* We don't support the abomination of removable broadcast devices */
735static ssize_t sysfs_unbind_tick_dev(struct device *dev,
736 struct device_attribute *attr,
737 const char *buf, size_t count)
738{
739 char name[CS_NAME_LEN];
Patrick Palka891292a2013-10-11 13:11:55 -0400740 ssize_t ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000741 struct clock_event_device *ce;
742
743 if (ret < 0)
744 return ret;
745
746 ret = -ENODEV;
747 mutex_lock(&clockevents_mutex);
748 raw_spin_lock_irq(&clockevents_lock);
749 list_for_each_entry(ce, &clockevent_devices, list) {
750 if (!strcmp(ce->name, name)) {
751 ret = __clockevents_try_unbind(ce, dev->id);
752 break;
753 }
754 }
755 raw_spin_unlock_irq(&clockevents_lock);
756 /*
757 * We hold clockevents_mutex, so ce can't go away
758 */
759 if (ret == -EAGAIN)
760 ret = clockevents_unbind(ce, dev->id);
761 mutex_unlock(&clockevents_mutex);
762 return ret ? ret : count;
763}
764static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
765
Thomas Gleixner501f8672013-04-25 20:31:49 +0000766#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
767static struct device tick_bc_dev = {
768 .init_name = "broadcast",
769 .id = 0,
770 .bus = &clockevents_subsys,
771};
772
773static struct tick_device *tick_get_tick_dev(struct device *dev)
774{
775 return dev == &tick_bc_dev ? tick_get_broadcast_device() :
776 &per_cpu(tick_cpu_device, dev->id);
777}
778
779static __init int tick_broadcast_init_sysfs(void)
780{
781 int err = device_register(&tick_bc_dev);
782
783 if (!err)
784 err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
785 return err;
786}
787#else
788static struct tick_device *tick_get_tick_dev(struct device *dev)
789{
790 return &per_cpu(tick_cpu_device, dev->id);
791}
792static inline int tick_broadcast_init_sysfs(void) { return 0; }
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200793#endif
Thomas Gleixner501f8672013-04-25 20:31:49 +0000794
795static int __init tick_init_sysfs(void)
796{
797 int cpu;
798
799 for_each_possible_cpu(cpu) {
800 struct device *dev = &per_cpu(tick_percpu_dev, cpu);
801 int err;
802
803 dev->id = cpu;
804 dev->bus = &clockevents_subsys;
805 err = device_register(dev);
806 if (!err)
807 err = device_create_file(dev, &dev_attr_current_device);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000808 if (!err)
809 err = device_create_file(dev, &dev_attr_unbind_device);
Thomas Gleixner501f8672013-04-25 20:31:49 +0000810 if (err)
811 return err;
812 }
813 return tick_broadcast_init_sysfs();
814}
815
816static int __init clockevents_init_sysfs(void)
817{
818 int err = subsys_system_register(&clockevents_subsys, NULL);
819
820 if (!err)
821 err = tick_init_sysfs();
822 return err;
823}
824device_initcall(clockevents_init_sysfs);
825#endif /* SYSFS */
826
827#endif /* GENERIC_CLOCK_EVENTS */