blob: af58898d9ebf1ff982a89e91ad4e28dca713cc90 [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
Yangtao Li7d9df982018-11-03 22:31:04 -040042 if (WARN_ON(!evt->mult))
Thomas Gleixner97b94102013-09-24 21:50:23 +020043 evt->mult = 1;
Thomas Gleixner97b94102013-09-24 21:50:23 +020044 rnd = (u64) evt->mult - 1;
45
46 /*
47 * Upper bound sanity check. If the backwards conversion is
48 * not equal latch, we know that the above shift overflowed.
49 */
50 if ((clc >> evt->shift) != (u64)latch)
51 clc = ~0ULL;
52
53 /*
54 * Scaled math oddities:
55 *
56 * For mult <= (1 << shift) we can safely add mult - 1 to
57 * prevent integer rounding loss. So the backwards conversion
58 * from nsec to device ticks will be correct.
59 *
60 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
61 * need to be careful. Adding mult - 1 will result in a value
62 * which when converted back to device ticks can be larger
63 * than latch by up to (mult - 1) >> shift. For the min_delta
64 * calculation we still want to apply this in order to stay
65 * above the minimum device ticks limit. For the upper limit
66 * we would end up with a latch value larger than the upper
67 * limit of the device, so we omit the add to stay below the
68 * device upper boundary.
69 *
70 * Also omit the add if it would overflow the u64 boundary.
71 */
72 if ((~0ULL - clc > rnd) &&
Thomas Gleixner10632002014-10-20 15:07:50 +040073 (!ismax || evt->mult <= (1ULL << evt->shift)))
Thomas Gleixner97b94102013-09-24 21:50:23 +020074 clc += rnd;
75
76 do_div(clc, evt->mult);
77
78 /* Deltas less than 1usec are pointless noise */
79 return clc > 1000 ? clc : 1000;
80}
81
Thomas Gleixnerd316c572007-02-16 01:28:00 -080082/**
83 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
84 * @latch: value to convert
85 * @evt: pointer to clock event device descriptor
86 *
87 * Math helper, returns latch value converted to nanoseconds (bound checked)
88 */
Jon Hunter97813f22009-08-18 12:45:11 -050089u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
Thomas Gleixnerd316c572007-02-16 01:28:00 -080090{
Thomas Gleixner97b94102013-09-24 21:50:23 +020091 return cev_delta2ns(latch, evt, false);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080092}
Magnus Dammc81fc2c2009-05-01 14:52:47 +090093EXPORT_SYMBOL_GPL(clockevent_delta2ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -080094
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020095static int __clockevents_switch_state(struct clock_event_device *dev,
96 enum clock_event_state state)
Viresh Kumarbd624d72015-02-13 08:54:56 +080097{
Viresh Kumarbd624d72015-02-13 08:54:56 +080098 if (dev->features & CLOCK_EVT_FEAT_DUMMY)
99 return 0;
100
Viresh Kumar77e32c82015-02-27 17:21:33 +0530101 /* Transition with new state-specific callbacks */
102 switch (state) {
103 case CLOCK_EVT_STATE_DETACHED:
Viresh Kumar149aabc2015-04-10 12:56:41 +0530104 /* The clockevent device is getting replaced. Shut it down. */
Viresh Kumarbd624d72015-02-13 08:54:56 +0800105
Viresh Kumar77e32c82015-02-27 17:21:33 +0530106 case CLOCK_EVT_STATE_SHUTDOWN:
Viresh Kumar7c4a9762015-07-07 10:14:35 +0200107 if (dev->set_state_shutdown)
108 return dev->set_state_shutdown(dev);
109 return 0;
Viresh Kumarbd624d72015-02-13 08:54:56 +0800110
Viresh Kumar77e32c82015-02-27 17:21:33 +0530111 case CLOCK_EVT_STATE_PERIODIC:
Viresh Kumarbd624d72015-02-13 08:54:56 +0800112 /* Core internal bug */
113 if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
114 return -ENOSYS;
Viresh Kumar7c4a9762015-07-07 10:14:35 +0200115 if (dev->set_state_periodic)
116 return dev->set_state_periodic(dev);
117 return 0;
Viresh Kumarbd624d72015-02-13 08:54:56 +0800118
Viresh Kumar77e32c82015-02-27 17:21:33 +0530119 case CLOCK_EVT_STATE_ONESHOT:
Viresh Kumarbd624d72015-02-13 08:54:56 +0800120 /* Core internal bug */
121 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
122 return -ENOSYS;
Viresh Kumar7c4a9762015-07-07 10:14:35 +0200123 if (dev->set_state_oneshot)
124 return dev->set_state_oneshot(dev);
125 return 0;
Viresh Kumarbd624d72015-02-13 08:54:56 +0800126
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530127 case CLOCK_EVT_STATE_ONESHOT_STOPPED:
128 /* Core internal bug */
Viresh Kumar472c4a92015-05-21 13:33:46 +0530129 if (WARN_ONCE(!clockevent_state_oneshot(dev),
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200130 "Current state: %d\n",
131 clockevent_get_state(dev)))
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530132 return -EINVAL;
133
134 if (dev->set_state_oneshot_stopped)
135 return dev->set_state_oneshot_stopped(dev);
136 else
137 return -ENOSYS;
138
Viresh Kumarbd624d72015-02-13 08:54:56 +0800139 default:
140 return -ENOSYS;
141 }
142}
143
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800144/**
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200145 * clockevents_switch_state - set the operating state of a clock event device
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800146 * @dev: device to modify
Viresh Kumar77e32c82015-02-27 17:21:33 +0530147 * @state: new state
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800148 *
149 * Must be called with interrupts disabled !
150 */
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200151void clockevents_switch_state(struct clock_event_device *dev,
152 enum clock_event_state state)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800153{
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200154 if (clockevent_get_state(dev) != state) {
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200155 if (__clockevents_switch_state(dev, state))
Viresh Kumarbd624d72015-02-13 08:54:56 +0800156 return;
157
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200158 clockevent_set_state(dev, state);
Magnus Damm2d682592009-01-16 17:14:38 +0900159
160 /*
161 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
162 * on it, so fix it up and emit a warning:
163 */
Viresh Kumar472c4a92015-05-21 13:33:46 +0530164 if (clockevent_state_oneshot(dev)) {
Yangtao Li7d9df982018-11-03 22:31:04 -0400165 if (WARN_ON(!dev->mult))
Magnus Damm2d682592009-01-16 17:14:38 +0900166 dev->mult = 1;
Magnus Damm2d682592009-01-16 17:14:38 +0900167 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800168 }
169}
170
171/**
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700172 * clockevents_shutdown - shutdown the device and clear next_event
173 * @dev: device to shutdown
174 */
175void clockevents_shutdown(struct clock_event_device *dev)
176{
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200177 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100178 dev->next_event = KTIME_MAX;
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700179}
180
Viresh Kumar554ef382015-02-27 17:21:32 +0530181/**
182 * clockevents_tick_resume - Resume the tick device before using it again
183 * @dev: device to resume
184 */
185int clockevents_tick_resume(struct clock_event_device *dev)
186{
187 int ret = 0;
188
Viresh Kumareef76352015-09-11 09:34:26 +0530189 if (dev->tick_resume)
Viresh Kumar77e32c82015-02-27 17:21:33 +0530190 ret = dev->tick_resume(dev);
Viresh Kumar554ef382015-02-27 17:21:32 +0530191
192 return ret;
193}
194
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200195#ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
196
197/* Limit min_delta to a jiffie */
198#define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
199
200/**
201 * clockevents_increase_min_delta - raise minimum delta of a clock event device
202 * @dev: device to increase the minimum delta
203 *
204 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
205 */
206static int clockevents_increase_min_delta(struct clock_event_device *dev)
207{
208 /* Nothing to do if we already reached the limit */
209 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
Jan Kara504d5872014-08-01 12:20:02 +0200210 printk_deferred(KERN_WARNING
211 "CE: Reprogramming failure. Giving up\n");
Thomas Gleixner2456e852016-12-25 11:38:40 +0100212 dev->next_event = KTIME_MAX;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200213 return -ETIME;
214 }
215
216 if (dev->min_delta_ns < 5000)
217 dev->min_delta_ns = 5000;
218 else
219 dev->min_delta_ns += dev->min_delta_ns >> 1;
220
221 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
222 dev->min_delta_ns = MIN_DELTA_LIMIT;
223
Jan Kara504d5872014-08-01 12:20:02 +0200224 printk_deferred(KERN_WARNING
225 "CE: %s increased min_delta_ns to %llu nsec\n",
226 dev->name ? dev->name : "?",
227 (unsigned long long) dev->min_delta_ns);
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200228 return 0;
229}
230
231/**
232 * clockevents_program_min_delta - Set clock event device to the minimum delay.
233 * @dev: device to program
234 *
235 * Returns 0 on success, -ETIME when the retry loop failed.
236 */
237static int clockevents_program_min_delta(struct clock_event_device *dev)
238{
239 unsigned long long clc;
240 int64_t delta;
241 int i;
242
243 for (i = 0;;) {
244 delta = dev->min_delta_ns;
245 dev->next_event = ktime_add_ns(ktime_get(), delta);
246
Viresh Kumar472c4a92015-05-21 13:33:46 +0530247 if (clockevent_state_shutdown(dev))
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200248 return 0;
249
250 dev->retries++;
251 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
252 if (dev->set_next_event((unsigned long) clc, dev) == 0)
253 return 0;
254
255 if (++i > 2) {
256 /*
257 * We tried 3 times to program the device with the
258 * given min_delta_ns. Try to increase the minimum
259 * delta, if that fails as well get out of here.
260 */
261 if (clockevents_increase_min_delta(dev))
262 return -ETIME;
263 i = 0;
264 }
265 }
266}
267
268#else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
269
270/**
271 * clockevents_program_min_delta - Set clock event device to the minimum delay.
272 * @dev: device to program
273 *
274 * Returns 0 on success, -ETIME when the retry loop failed.
275 */
276static int clockevents_program_min_delta(struct clock_event_device *dev)
277{
278 unsigned long long clc;
James Hogan3a29ddb2017-10-19 15:17:23 +0100279 int64_t delta = 0;
280 int i;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200281
James Hogan3a29ddb2017-10-19 15:17:23 +0100282 for (i = 0; i < 10; i++) {
283 delta += dev->min_delta_ns;
284 dev->next_event = ktime_add_ns(ktime_get(), delta);
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200285
James Hogan3a29ddb2017-10-19 15:17:23 +0100286 if (clockevent_state_shutdown(dev))
287 return 0;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200288
James Hogan3a29ddb2017-10-19 15:17:23 +0100289 dev->retries++;
290 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
291 if (dev->set_next_event((unsigned long) clc, dev) == 0)
292 return 0;
293 }
294 return -ETIME;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200295}
296
297#endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
298
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700299/**
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800300 * clockevents_program_event - Reprogram the clock event device.
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200301 * @dev: device to program
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800302 * @expires: absolute expiry time (monotonic clock)
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200303 * @force: program minimum delay if expires can not be set
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800304 *
305 * Returns 0 on success, -ETIME when the event is in the past.
306 */
307int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200308 bool force)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800309{
310 unsigned long long clc;
311 int64_t delta;
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200312 int rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800313
Yangtao Li7d9df982018-11-03 22:31:04 -0400314 if (WARN_ON_ONCE(expires < 0))
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100315 return -ETIME;
Thomas Gleixner167b1de2007-12-07 19:16:17 +0100316
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800317 dev->next_event = expires;
318
Viresh Kumar472c4a92015-05-21 13:33:46 +0530319 if (clockevent_state_shutdown(dev))
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800320 return 0;
321
Viresh Kumard25408752015-04-03 09:04:05 +0530322 /* We must be in ONESHOT state here */
Viresh Kumar472c4a92015-05-21 13:33:46 +0530323 WARN_ONCE(!clockevent_state_oneshot(dev), "Current state: %d\n",
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200324 clockevent_get_state(dev));
Viresh Kumard25408752015-04-03 09:04:05 +0530325
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200326 /* Shortcut for clockevent devices that can deal with ktime. */
327 if (dev->features & CLOCK_EVT_FEAT_KTIME)
328 return dev->set_next_ktime(expires, dev);
329
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200330 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
331 if (delta <= 0)
332 return force ? clockevents_program_min_delta(dev) : -ETIME;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800333
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200334 delta = min(delta, (int64_t) dev->max_delta_ns);
335 delta = max(delta, (int64_t) dev->min_delta_ns);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800336
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200337 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
338 rc = dev->set_next_event((unsigned long) clc, dev);
339
340 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800341}
342
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800343/*
Li Zefan3eb05672008-02-08 04:19:25 -0800344 * Called after a notify add to make devices available which were
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800345 * released from the notifier call.
346 */
347static void clockevents_notify_released(void)
348{
349 struct clock_event_device *dev;
350
351 while (!list_empty(&clockevents_released)) {
352 dev = list_entry(clockevents_released.next,
353 struct clock_event_device, list);
354 list_del(&dev->list);
355 list_add(&dev->list, &clockevent_devices);
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000356 tick_check_new_device(dev);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800357 }
358}
359
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000360/*
361 * Try to install a replacement clock event device
362 */
363static int clockevents_replace(struct clock_event_device *ced)
364{
365 struct clock_event_device *dev, *newdev = NULL;
366
367 list_for_each_entry(dev, &clockevent_devices, list) {
Viresh Kumar472c4a92015-05-21 13:33:46 +0530368 if (dev == ced || !clockevent_state_detached(dev))
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000369 continue;
370
371 if (!tick_check_replacement(newdev, dev))
372 continue;
373
374 if (!try_module_get(dev->owner))
375 continue;
376
377 if (newdev)
378 module_put(newdev->owner);
379 newdev = dev;
380 }
381 if (newdev) {
382 tick_install_replacement(newdev);
383 list_del_init(&ced->list);
384 }
385 return newdev ? 0 : -EBUSY;
386}
387
388/*
389 * Called with clockevents_mutex and clockevents_lock held
390 */
391static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
392{
393 /* Fast track. Device is unused */
Viresh Kumar472c4a92015-05-21 13:33:46 +0530394 if (clockevent_state_detached(ced)) {
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000395 list_del_init(&ced->list);
396 return 0;
397 }
398
399 return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
400}
401
402/*
403 * SMP function call to unbind a device
404 */
405static void __clockevents_unbind(void *arg)
406{
407 struct ce_unbind *cu = arg;
408 int res;
409
410 raw_spin_lock(&clockevents_lock);
411 res = __clockevents_try_unbind(cu->ce, smp_processor_id());
412 if (res == -EAGAIN)
413 res = clockevents_replace(cu->ce);
414 cu->res = res;
415 raw_spin_unlock(&clockevents_lock);
416}
417
418/*
419 * Issues smp function call to unbind a per cpu device. Called with
420 * clockevents_mutex held.
421 */
422static int clockevents_unbind(struct clock_event_device *ced, int cpu)
423{
424 struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
425
426 smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
427 return cu.res;
428}
429
430/*
431 * Unbind a clockevents device.
432 */
433int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
434{
435 int ret;
436
437 mutex_lock(&clockevents_mutex);
438 ret = clockevents_unbind(ced, cpu);
439 mutex_unlock(&clockevents_mutex);
440 return ret;
441}
Vitaly Kuznetsov32a15832015-02-27 11:25:56 -0800442EXPORT_SYMBOL_GPL(clockevents_unbind_device);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000443
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800444/**
445 * clockevents_register_device - register a clock event device
446 * @dev: device to register
447 */
448void clockevents_register_device(struct clock_event_device *dev)
449{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700450 unsigned long flags;
451
Viresh Kumar77e32c82015-02-27 17:21:33 +0530452 /* Initialize state to DETACHED */
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200453 clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530454
Thomas Gleixner1b054b62011-06-03 11:13:33 +0200455 if (!dev->cpumask) {
456 WARN_ON(num_possible_cpus() > 1);
457 dev->cpumask = cpumask_of(smp_processor_id());
458 }
Rusty Russell320ab2b2008-12-13 21:20:26 +1030459
Sudeep Hollafbfa9262018-07-11 12:24:24 +0100460 if (dev->cpumask == cpu_all_mask) {
461 WARN(1, "%s cpumask == cpu_all_mask, using cpu_possible_mask instead\n",
462 dev->name);
463 dev->cpumask = cpu_possible_mask;
464 }
465
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100466 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800467
468 list_add(&dev->list, &clockevent_devices);
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000469 tick_check_new_device(dev);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800470 clockevents_notify_released();
471
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100472 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800473}
Magnus Dammc81fc2c2009-05-01 14:52:47 +0900474EXPORT_SYMBOL_GPL(clockevents_register_device);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800475
Nicolai Stange0695bd92017-02-06 22:12:04 +0100476static void clockevents_config(struct clock_event_device *dev, u32 freq)
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000477{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200478 u64 sec;
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000479
480 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
481 return;
482
483 /*
484 * Calculate the maximum number of seconds we can sleep. Limit
485 * to 10 minutes for hardware which can program more than
486 * 32bit ticks so we still get reasonable conversion values.
487 */
488 sec = dev->max_delta_ticks;
489 do_div(sec, freq);
490 if (!sec)
491 sec = 1;
492 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
493 sec = 600;
494
495 clockevents_calc_mult_shift(dev, freq, sec);
Thomas Gleixner97b94102013-09-24 21:50:23 +0200496 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
497 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000498}
499
500/**
501 * clockevents_config_and_register - Configure and register a clock event device
502 * @dev: device to register
503 * @freq: The clock frequency
504 * @min_delta: The minimum clock ticks to program in oneshot mode
505 * @max_delta: The maximum clock ticks to program in oneshot mode
506 *
507 * min/max_delta can be 0 for devices which do not support oneshot mode.
508 */
509void clockevents_config_and_register(struct clock_event_device *dev,
510 u32 freq, unsigned long min_delta,
511 unsigned long max_delta)
512{
513 dev->min_delta_ticks = min_delta;
514 dev->max_delta_ticks = max_delta;
515 clockevents_config(dev, freq);
516 clockevents_register_device(dev);
517}
Shawn Guoc35ef952013-01-12 11:50:04 +0000518EXPORT_SYMBOL_GPL(clockevents_config_and_register);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000519
Thomas Gleixner627ee792014-02-03 14:34:31 -0800520int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000521{
522 clockevents_config(dev, freq);
523
Viresh Kumar472c4a92015-05-21 13:33:46 +0530524 if (clockevent_state_oneshot(dev))
Soren Brinkmannfe79a9b2014-02-03 14:34:32 -0800525 return clockevents_program_event(dev, dev->next_event, false);
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000526
Viresh Kumar472c4a92015-05-21 13:33:46 +0530527 if (clockevent_state_periodic(dev))
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200528 return __clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
Soren Brinkmannfe79a9b2014-02-03 14:34:32 -0800529
530 return 0;
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000531}
532
Thomas Gleixner627ee792014-02-03 14:34:31 -0800533/**
534 * clockevents_update_freq - Update frequency and reprogram a clock event device.
535 * @dev: device to modify
536 * @freq: new device frequency
537 *
538 * Reconfigure and reprogram a clock event device in oneshot
539 * mode. Must be called on the cpu for which the device delivers per
540 * cpu timer events. If called for the broadcast device the core takes
541 * care of serialization.
542 *
543 * Returns 0 on success, -ETIME when the event is in the past.
544 */
545int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
546{
547 unsigned long flags;
548 int ret;
549
550 local_irq_save(flags);
551 ret = tick_broadcast_update_freq(dev, freq);
552 if (ret == -ENODEV)
553 ret = __clockevents_update_freq(dev, freq);
554 local_irq_restore(flags);
555 return ret;
556}
557
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800558/*
559 * Noop handler when we shut down an event device
560 */
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000561void clockevents_handle_noop(struct clock_event_device *dev)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800562{
563}
564
565/**
566 * clockevents_exchange_device - release and request clock devices
567 * @old: device to release (can be NULL)
568 * @new: device to request (can be NULL)
569 *
Thomas Gleixnerdb6f6722015-03-25 13:08:27 +0100570 * Called from various tick functions with clockevents_lock held and
571 * interrupts disabled.
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800572 */
573void clockevents_exchange_device(struct clock_event_device *old,
574 struct clock_event_device *new)
575{
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800576 /*
577 * Caller releases a clock event device. We queue it into the
578 * released list and do a notify add later.
579 */
580 if (old) {
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000581 module_put(old->owner);
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200582 clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800583 list_del(&old->list);
584 list_add(&old->list, &clockevents_released);
585 }
586
587 if (new) {
Viresh Kumar472c4a92015-05-21 13:33:46 +0530588 BUG_ON(!clockevent_state_detached(new));
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700589 clockevents_shutdown(new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800590 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800591}
592
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200593/**
594 * clockevents_suspend - suspend clock devices
595 */
596void clockevents_suspend(void)
597{
598 struct clock_event_device *dev;
599
600 list_for_each_entry_reverse(dev, &clockevent_devices, list)
Viresh Kumara9d20982015-06-17 16:04:46 +0530601 if (dev->suspend && !clockevent_state_detached(dev))
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200602 dev->suspend(dev);
603}
604
605/**
606 * clockevents_resume - resume clock devices
607 */
608void clockevents_resume(void)
609{
610 struct clock_event_device *dev;
611
612 list_for_each_entry(dev, &clockevent_devices, list)
Viresh Kumara9d20982015-06-17 16:04:46 +0530613 if (dev->resume && !clockevent_state_detached(dev))
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200614 dev->resume(dev);
615}
616
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200617#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800618/**
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200619 * tick_cleanup_dead_cpu - Cleanup the tick and clockevents of a dead cpu
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800620 */
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200621void tick_cleanup_dead_cpu(int cpu)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800622{
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100623 struct clock_event_device *dev, *tmp;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700624 unsigned long flags;
Li Zefan0b858e62008-02-08 04:19:24 -0800625
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100626 raw_spin_lock_irqsave(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800627
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200628 tick_shutdown_broadcast_oneshot(cpu);
629 tick_shutdown_broadcast(cpu);
630 tick_shutdown(cpu);
631 /*
632 * Unregister the clock event devices which were
633 * released from the users in the notify chain.
634 */
635 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
636 list_del(&dev->list);
637 /*
638 * Now check whether the CPU has left unused per cpu devices
639 */
640 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
641 if (cpumask_test_cpu(cpu, dev->cpumask) &&
642 cpumask_weight(dev->cpumask) == 1 &&
643 !tick_is_broadcast_device(dev)) {
Viresh Kumar472c4a92015-05-21 13:33:46 +0530644 BUG_ON(!clockevent_state_detached(dev));
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100645 list_del(&dev->list);
Thomas Gleixnerbb6eddf2009-12-10 15:35:10 +0100646 }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800647 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100648 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800649}
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200650#endif
Thomas Gleixner501f8672013-04-25 20:31:49 +0000651
652#ifdef CONFIG_SYSFS
Ben Dooks775be502016-06-17 16:56:14 +0100653static struct bus_type clockevents_subsys = {
Thomas Gleixner501f8672013-04-25 20:31:49 +0000654 .name = "clockevents",
655 .dev_name = "clockevent",
656};
657
658static DEFINE_PER_CPU(struct device, tick_percpu_dev);
659static struct tick_device *tick_get_tick_dev(struct device *dev);
660
661static ssize_t sysfs_show_current_tick_dev(struct device *dev,
662 struct device_attribute *attr,
663 char *buf)
664{
665 struct tick_device *td;
666 ssize_t count = 0;
667
668 raw_spin_lock_irq(&clockevents_lock);
669 td = tick_get_tick_dev(dev);
670 if (td && td->evtdev)
671 count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
672 raw_spin_unlock_irq(&clockevents_lock);
673 return count;
674}
675static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
676
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000677/* We don't support the abomination of removable broadcast devices */
678static ssize_t sysfs_unbind_tick_dev(struct device *dev,
679 struct device_attribute *attr,
680 const char *buf, size_t count)
681{
682 char name[CS_NAME_LEN];
Patrick Palka891292a2013-10-11 13:11:55 -0400683 ssize_t ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000684 struct clock_event_device *ce;
685
686 if (ret < 0)
687 return ret;
688
689 ret = -ENODEV;
690 mutex_lock(&clockevents_mutex);
691 raw_spin_lock_irq(&clockevents_lock);
692 list_for_each_entry(ce, &clockevent_devices, list) {
693 if (!strcmp(ce->name, name)) {
694 ret = __clockevents_try_unbind(ce, dev->id);
695 break;
696 }
697 }
698 raw_spin_unlock_irq(&clockevents_lock);
699 /*
700 * We hold clockevents_mutex, so ce can't go away
701 */
702 if (ret == -EAGAIN)
703 ret = clockevents_unbind(ce, dev->id);
704 mutex_unlock(&clockevents_mutex);
705 return ret ? ret : count;
706}
707static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
708
Thomas Gleixner501f8672013-04-25 20:31:49 +0000709#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
710static struct device tick_bc_dev = {
711 .init_name = "broadcast",
712 .id = 0,
713 .bus = &clockevents_subsys,
714};
715
716static struct tick_device *tick_get_tick_dev(struct device *dev)
717{
718 return dev == &tick_bc_dev ? tick_get_broadcast_device() :
719 &per_cpu(tick_cpu_device, dev->id);
720}
721
722static __init int tick_broadcast_init_sysfs(void)
723{
724 int err = device_register(&tick_bc_dev);
725
726 if (!err)
727 err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
728 return err;
729}
730#else
731static struct tick_device *tick_get_tick_dev(struct device *dev)
732{
733 return &per_cpu(tick_cpu_device, dev->id);
734}
735static inline int tick_broadcast_init_sysfs(void) { return 0; }
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200736#endif
Thomas Gleixner501f8672013-04-25 20:31:49 +0000737
738static int __init tick_init_sysfs(void)
739{
740 int cpu;
741
742 for_each_possible_cpu(cpu) {
743 struct device *dev = &per_cpu(tick_percpu_dev, cpu);
744 int err;
745
746 dev->id = cpu;
747 dev->bus = &clockevents_subsys;
748 err = device_register(dev);
749 if (!err)
750 err = device_create_file(dev, &dev_attr_current_device);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000751 if (!err)
752 err = device_create_file(dev, &dev_attr_unbind_device);
Thomas Gleixner501f8672013-04-25 20:31:49 +0000753 if (err)
754 return err;
755 }
756 return tick_broadcast_init_sysfs();
757}
758
759static int __init clockevents_init_sysfs(void)
760{
761 int err = subsys_system_register(&clockevents_subsys, NULL);
762
763 if (!err)
764 err = tick_init_sysfs();
765 return err;
766}
767device_initcall(clockevents_init_sysfs);
768#endif /* SYSFS */