Merge tag 'v3.10.22' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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
21 #include "tick-internal.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_RAW_SPINLOCK(clockevents_lock);
32
33 static 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
81 /**
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  */
88 u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
89 {
90         return cev_delta2ns(latch, evt, false);
91 }
92 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
93
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  */
101 void 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;
107
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                 }
118         }
119 }
120
121 /**
122  * clockevents_shutdown - shutdown the device and clear next_event
123  * @dev:        device to shutdown
124  */
125 void 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
131 #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  */
142 static 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  */
171 static 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  */
210 static 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
228 /**
229  * clockevents_program_event - Reprogram the clock event device.
230  * @dev:        device to program
231  * @expires:    absolute expiry time (monotonic clock)
232  * @force:      program minimum delay if expires can not be set
233  *
234  * Returns 0 on success, -ETIME when the event is in the past.
235  */
236 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
237                               bool force)
238 {
239         unsigned long long clc;
240         int64_t delta;
241         int rc;
242
243         if (unlikely(expires.tv64 < 0)) {
244                 WARN_ON_ONCE(1);
245                 return -ETIME;
246         }
247
248         dev->next_event = expires;
249
250         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
251                 return 0;
252
253         /* 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
257         delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
258         if (delta <= 0)
259                 return force ? clockevents_program_min_delta(dev) : -ETIME;
260
261         delta = min(delta, (int64_t) dev->max_delta_ns);
262         delta = max(delta, (int64_t) dev->min_delta_ns);
263
264         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;
268 }
269
270 /**
271  * clockevents_register_notifier - register a clock events change listener
272  */
273 int clockevents_register_notifier(struct notifier_block *nb)
274 {
275         unsigned long flags;
276         int ret;
277
278         raw_spin_lock_irqsave(&clockevents_lock, flags);
279         ret = raw_notifier_chain_register(&clockevents_chain, nb);
280         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
281
282         return ret;
283 }
284
285 /*
286  * Notify about a clock event change. Called with clockevents_lock
287  * held.
288  */
289 static void clockevents_do_notify(unsigned long reason, void *dev)
290 {
291         raw_notifier_call_chain(&clockevents_chain, reason, dev);
292 }
293
294 /*
295  * Called after a notify add to make devices available which were
296  * released from the notifier call.
297  */
298 static 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  */
315 void clockevents_register_device(struct clock_event_device *dev)
316 {
317         unsigned long flags;
318
319         BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
320         if (!dev->cpumask) {
321                 WARN_ON(num_possible_cpus() > 1);
322                 dev->cpumask = cpumask_of(smp_processor_id());
323         }
324
325         raw_spin_lock_irqsave(&clockevents_lock, flags);
326
327         list_add(&dev->list, &clockevent_devices);
328         clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev);
329         clockevents_notify_released();
330
331         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
332 }
333 EXPORT_SYMBOL_GPL(clockevents_register_device);
334
335 void clockevents_config(struct clock_event_device *dev, u32 freq)
336 {
337         u64 sec;
338
339         if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
340                 return;
341
342         /*
343          * Calculate the maximum number of seconds we can sleep. Limit
344          * to 10 minutes for hardware which can program more than
345          * 32bit ticks so we still get reasonable conversion values.
346          */
347         sec = dev->max_delta_ticks;
348         do_div(sec, freq);
349         if (!sec)
350                 sec = 1;
351         else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
352                 sec = 600;
353
354         clockevents_calc_mult_shift(dev, freq, sec);
355         dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
356         dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
357 }
358
359 /**
360  * clockevents_config_and_register - Configure and register a clock event device
361  * @dev:        device to register
362  * @freq:       The clock frequency
363  * @min_delta:  The minimum clock ticks to program in oneshot mode
364  * @max_delta:  The maximum clock ticks to program in oneshot mode
365  *
366  * min/max_delta can be 0 for devices which do not support oneshot mode.
367  */
368 void clockevents_config_and_register(struct clock_event_device *dev,
369                                      u32 freq, unsigned long min_delta,
370                                      unsigned long max_delta)
371 {
372         dev->min_delta_ticks = min_delta;
373         dev->max_delta_ticks = max_delta;
374         clockevents_config(dev, freq);
375         clockevents_register_device(dev);
376 }
377 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
378
379 /**
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  */
389 int 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
396         return clockevents_program_event(dev, dev->next_event, false);
397 }
398
399 /*
400  * Noop handler when we shut down an event device
401  */
402 void clockevents_handle_noop(struct clock_event_device *dev)
403 {
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  */
413 void 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) {
424                 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);
431                 clockevents_shutdown(new);
432         }
433         local_irq_restore(flags);
434 }
435
436 /**
437  * clockevents_suspend - suspend clock devices
438  */
439 void clockevents_suspend(void)
440 {
441         struct clock_event_device *dev;
442
443         list_for_each_entry_reverse(dev, &clockevent_devices, list)
444                 if (dev->suspend)
445                         dev->suspend(dev);
446 }
447
448 /**
449  * clockevents_resume - resume clock devices
450  */
451 void clockevents_resume(void)
452 {
453         struct clock_event_device *dev;
454
455         list_for_each_entry(dev, &clockevent_devices, list)
456                 if (dev->resume)
457                         dev->resume(dev);
458 }
459
460 #ifdef CONFIG_GENERIC_CLOCKEVENTS
461 /**
462  * clockevents_notify - notification about relevant events
463  */
464 void clockevents_notify(unsigned long reason, void *arg)
465 {
466         struct clock_event_device *dev, *tmp;
467         unsigned long flags;
468         int cpu;
469
470         raw_spin_lock_irqsave(&clockevents_lock, flags);
471         clockevents_do_notify(reason, arg);
472
473         switch (reason) {
474         case CLOCK_EVT_NOTIFY_CPU_DEAD:
475                 /*
476                  * Unregister the clock event devices which were
477                  * released from the users in the notify chain.
478                  */
479                 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
480                         list_del(&dev->list);
481                 /*
482                  * Now check whether the CPU has left unused per cpu devices
483                  */
484                 cpu = *((int *)arg);
485                 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
486                         if (cpumask_test_cpu(cpu, dev->cpumask) &&
487                             cpumask_weight(dev->cpumask) == 1 &&
488                             !tick_is_broadcast_device(dev)) {
489                                 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
490                                 list_del(&dev->list);
491                         }
492                 }
493                 break;
494         default:
495                 break;
496         }
497         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
498 }
499 EXPORT_SYMBOL_GPL(clockevents_notify);
500 #endif