Merge branch develop-3.10 into develop-3.10-next
[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/smp.h>
19
20 #include "tick-internal.h"
21
22 /* The registered clock event devices */
23 static LIST_HEAD(clockevent_devices);
24 static LIST_HEAD(clockevents_released);
25 /* Protection for the above */
26 static DEFINE_RAW_SPINLOCK(clockevents_lock);
27
28 static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
29                         bool ismax)
30 {
31         u64 clc = (u64) latch << evt->shift;
32         u64 rnd;
33
34         if (unlikely(!evt->mult)) {
35                 evt->mult = 1;
36                 WARN_ON(1);
37         }
38         rnd = (u64) evt->mult - 1;
39
40         /*
41          * Upper bound sanity check. If the backwards conversion is
42          * not equal latch, we know that the above shift overflowed.
43          */
44         if ((clc >> evt->shift) != (u64)latch)
45                 clc = ~0ULL;
46
47         /*
48          * Scaled math oddities:
49          *
50          * For mult <= (1 << shift) we can safely add mult - 1 to
51          * prevent integer rounding loss. So the backwards conversion
52          * from nsec to device ticks will be correct.
53          *
54          * For mult > (1 << shift), i.e. device frequency is > 1GHz we
55          * need to be careful. Adding mult - 1 will result in a value
56          * which when converted back to device ticks can be larger
57          * than latch by up to (mult - 1) >> shift. For the min_delta
58          * calculation we still want to apply this in order to stay
59          * above the minimum device ticks limit. For the upper limit
60          * we would end up with a latch value larger than the upper
61          * limit of the device, so we omit the add to stay below the
62          * device upper boundary.
63          *
64          * Also omit the add if it would overflow the u64 boundary.
65          */
66         if ((~0ULL - clc > rnd) &&
67             (!ismax || evt->mult <= (1U << evt->shift)))
68                 clc += rnd;
69
70         do_div(clc, evt->mult);
71
72         /* Deltas less than 1usec are pointless noise */
73         return clc > 1000 ? clc : 1000;
74 }
75
76 /**
77  * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
78  * @latch:      value to convert
79  * @evt:        pointer to clock event device descriptor
80  *
81  * Math helper, returns latch value converted to nanoseconds (bound checked)
82  */
83 u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
84 {
85         return cev_delta2ns(latch, evt, false);
86 }
87 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
88
89 /**
90  * clockevents_set_mode - set the operating mode of a clock event device
91  * @dev:        device to modify
92  * @mode:       new mode
93  *
94  * Must be called with interrupts disabled !
95  */
96 void clockevents_set_mode(struct clock_event_device *dev,
97                                  enum clock_event_mode mode)
98 {
99         if (dev->mode != mode) {
100                 dev->set_mode(mode, dev);
101                 dev->mode = mode;
102
103                 /*
104                  * A nsec2cyc multiplicator of 0 is invalid and we'd crash
105                  * on it, so fix it up and emit a warning:
106                  */
107                 if (mode == CLOCK_EVT_MODE_ONESHOT) {
108                         if (unlikely(!dev->mult)) {
109                                 dev->mult = 1;
110                                 WARN_ON(1);
111                         }
112                 }
113         }
114 }
115
116 /**
117  * clockevents_shutdown - shutdown the device and clear next_event
118  * @dev:        device to shutdown
119  */
120 void clockevents_shutdown(struct clock_event_device *dev)
121 {
122         clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
123         dev->next_event.tv64 = KTIME_MAX;
124 }
125
126 #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
127
128 /* Limit min_delta to a jiffie */
129 #define MIN_DELTA_LIMIT         (NSEC_PER_SEC / HZ)
130
131 /**
132  * clockevents_increase_min_delta - raise minimum delta of a clock event device
133  * @dev:       device to increase the minimum delta
134  *
135  * Returns 0 on success, -ETIME when the minimum delta reached the limit.
136  */
137 static int clockevents_increase_min_delta(struct clock_event_device *dev)
138 {
139         /* Nothing to do if we already reached the limit */
140         if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
141                 printk_deferred(KERN_WARNING
142                                 "CE: Reprogramming failure. Giving up\n");
143                 dev->next_event.tv64 = KTIME_MAX;
144                 return -ETIME;
145         }
146
147         if (dev->min_delta_ns < 5000)
148                 dev->min_delta_ns = 5000;
149         else
150                 dev->min_delta_ns += dev->min_delta_ns >> 1;
151
152         if (dev->min_delta_ns > MIN_DELTA_LIMIT)
153                 dev->min_delta_ns = MIN_DELTA_LIMIT;
154
155         printk_deferred(KERN_WARNING
156                         "CE: %s increased min_delta_ns to %llu nsec\n",
157                         dev->name ? dev->name : "?",
158                         (unsigned long long) dev->min_delta_ns);
159         return 0;
160 }
161
162 /**
163  * clockevents_program_min_delta - Set clock event device to the minimum delay.
164  * @dev:        device to program
165  *
166  * Returns 0 on success, -ETIME when the retry loop failed.
167  */
168 static int clockevents_program_min_delta(struct clock_event_device *dev)
169 {
170         unsigned long long clc;
171         int64_t delta;
172         int i;
173
174         for (i = 0;;) {
175                 delta = dev->min_delta_ns;
176                 dev->next_event = ktime_add_ns(ktime_get(), delta);
177
178                 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
179                         return 0;
180
181                 dev->retries++;
182                 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
183                 if (dev->set_next_event((unsigned long) clc, dev) == 0)
184                         return 0;
185
186                 if (++i > 2) {
187                         /*
188                          * We tried 3 times to program the device with the
189                          * given min_delta_ns. Try to increase the minimum
190                          * delta, if that fails as well get out of here.
191                          */
192                         if (clockevents_increase_min_delta(dev))
193                                 return -ETIME;
194                         i = 0;
195                 }
196         }
197 }
198
199 #else  /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
200
201 /**
202  * clockevents_program_min_delta - Set clock event device to the minimum delay.
203  * @dev:        device to program
204  *
205  * Returns 0 on success, -ETIME when the retry loop failed.
206  */
207 static int clockevents_program_min_delta(struct clock_event_device *dev)
208 {
209         unsigned long long clc;
210         int64_t delta;
211
212         delta = dev->min_delta_ns;
213         dev->next_event = ktime_add_ns(ktime_get(), delta);
214
215         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
216                 return 0;
217
218         dev->retries++;
219         clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
220         return dev->set_next_event((unsigned long) clc, dev);
221 }
222
223 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
224
225 /**
226  * clockevents_program_event - Reprogram the clock event device.
227  * @dev:        device to program
228  * @expires:    absolute expiry time (monotonic clock)
229  * @force:      program minimum delay if expires can not be set
230  *
231  * Returns 0 on success, -ETIME when the event is in the past.
232  */
233 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
234                               bool force)
235 {
236         unsigned long long clc;
237         int64_t delta;
238         int rc;
239
240         if (unlikely(expires.tv64 < 0)) {
241                 WARN_ON_ONCE(1);
242                 return -ETIME;
243         }
244
245         dev->next_event = expires;
246
247         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
248                 return 0;
249
250         /* Shortcut for clockevent devices that can deal with ktime. */
251         if (dev->features & CLOCK_EVT_FEAT_KTIME)
252                 return dev->set_next_ktime(expires, dev);
253
254         delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
255         if (delta <= 0)
256                 return force ? clockevents_program_min_delta(dev) : -ETIME;
257
258         delta = min(delta, (int64_t) dev->max_delta_ns);
259         delta = max(delta, (int64_t) dev->min_delta_ns);
260
261         clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
262         rc = dev->set_next_event((unsigned long) clc, dev);
263
264         return (rc && force) ? clockevents_program_min_delta(dev) : rc;
265 }
266
267 /*
268  * Called after a notify add to make devices available which were
269  * released from the notifier call.
270  */
271 static void clockevents_notify_released(void)
272 {
273         struct clock_event_device *dev;
274
275         while (!list_empty(&clockevents_released)) {
276                 dev = list_entry(clockevents_released.next,
277                                  struct clock_event_device, list);
278                 list_del(&dev->list);
279                 list_add(&dev->list, &clockevent_devices);
280                 tick_check_new_device(dev);
281         }
282 }
283
284 /**
285  * clockevents_register_device - register a clock event device
286  * @dev:        device to register
287  */
288 void clockevents_register_device(struct clock_event_device *dev)
289 {
290         unsigned long flags;
291
292         BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
293         if (!dev->cpumask) {
294                 WARN_ON(num_possible_cpus() > 1);
295                 dev->cpumask = cpumask_of(smp_processor_id());
296         }
297
298         raw_spin_lock_irqsave(&clockevents_lock, flags);
299
300         list_add(&dev->list, &clockevent_devices);
301         tick_check_new_device(dev);
302         clockevents_notify_released();
303
304         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
305 }
306 EXPORT_SYMBOL_GPL(clockevents_register_device);
307
308 void clockevents_config(struct clock_event_device *dev, u32 freq)
309 {
310         u64 sec;
311
312         if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
313                 return;
314
315         /*
316          * Calculate the maximum number of seconds we can sleep. Limit
317          * to 10 minutes for hardware which can program more than
318          * 32bit ticks so we still get reasonable conversion values.
319          */
320         sec = dev->max_delta_ticks;
321         do_div(sec, freq);
322         if (!sec)
323                 sec = 1;
324         else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
325                 sec = 600;
326
327         clockevents_calc_mult_shift(dev, freq, sec);
328         dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
329         dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
330 }
331
332 /**
333  * clockevents_config_and_register - Configure and register a clock event device
334  * @dev:        device to register
335  * @freq:       The clock frequency
336  * @min_delta:  The minimum clock ticks to program in oneshot mode
337  * @max_delta:  The maximum clock ticks to program in oneshot mode
338  *
339  * min/max_delta can be 0 for devices which do not support oneshot mode.
340  */
341 void clockevents_config_and_register(struct clock_event_device *dev,
342                                      u32 freq, unsigned long min_delta,
343                                      unsigned long max_delta)
344 {
345         dev->min_delta_ticks = min_delta;
346         dev->max_delta_ticks = max_delta;
347         clockevents_config(dev, freq);
348         clockevents_register_device(dev);
349 }
350 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
351
352 /**
353  * clockevents_update_freq - Update frequency and reprogram a clock event device.
354  * @dev:        device to modify
355  * @freq:       new device frequency
356  *
357  * Reconfigure and reprogram a clock event device in oneshot
358  * mode. Must be called on the cpu for which the device delivers per
359  * cpu timer events with interrupts disabled!  Returns 0 on success,
360  * -ETIME when the event is in the past.
361  */
362 int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
363 {
364         clockevents_config(dev, freq);
365
366         if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
367                 return 0;
368
369         return clockevents_program_event(dev, dev->next_event, false);
370 }
371
372 /*
373  * Noop handler when we shut down an event device
374  */
375 void clockevents_handle_noop(struct clock_event_device *dev)
376 {
377 }
378
379 /**
380  * clockevents_exchange_device - release and request clock devices
381  * @old:        device to release (can be NULL)
382  * @new:        device to request (can be NULL)
383  *
384  * Called from the notifier chain. clockevents_lock is held already
385  */
386 void clockevents_exchange_device(struct clock_event_device *old,
387                                  struct clock_event_device *new)
388 {
389         unsigned long flags;
390
391         local_irq_save(flags);
392         /*
393          * Caller releases a clock event device. We queue it into the
394          * released list and do a notify add later.
395          */
396         if (old) {
397                 module_put(old->owner);
398                 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
399                 list_del(&old->list);
400                 list_add(&old->list, &clockevents_released);
401         }
402
403         if (new) {
404                 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
405                 clockevents_shutdown(new);
406         }
407         local_irq_restore(flags);
408 }
409
410 /**
411  * clockevents_suspend - suspend clock devices
412  */
413 void clockevents_suspend(void)
414 {
415         struct clock_event_device *dev;
416
417         list_for_each_entry_reverse(dev, &clockevent_devices, list)
418                 if (dev->suspend)
419                         dev->suspend(dev);
420 }
421
422 /**
423  * clockevents_resume - resume clock devices
424  */
425 void clockevents_resume(void)
426 {
427         struct clock_event_device *dev;
428
429         list_for_each_entry(dev, &clockevent_devices, list)
430                 if (dev->resume)
431                         dev->resume(dev);
432 }
433
434 #ifdef CONFIG_GENERIC_CLOCKEVENTS
435 /**
436  * clockevents_notify - notification about relevant events
437  */
438 void clockevents_notify(unsigned long reason, void *arg)
439 {
440         struct clock_event_device *dev, *tmp;
441         unsigned long flags;
442         int cpu;
443
444         raw_spin_lock_irqsave(&clockevents_lock, flags);
445         tick_notify(reason, arg);
446
447         switch (reason) {
448         case CLOCK_EVT_NOTIFY_CPU_DEAD:
449                 /*
450                  * Unregister the clock event devices which were
451                  * released from the users in the notify chain.
452                  */
453                 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
454                         list_del(&dev->list);
455                 /*
456                  * Now check whether the CPU has left unused per cpu devices
457                  */
458                 cpu = *((int *)arg);
459                 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
460                         if (cpumask_test_cpu(cpu, dev->cpumask) &&
461                             cpumask_weight(dev->cpumask) == 1 &&
462                             !tick_is_broadcast_device(dev)) {
463                                 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
464                                 list_del(&dev->list);
465                         }
466                 }
467                 break;
468         default:
469                 break;
470         }
471         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
472 }
473 EXPORT_SYMBOL_GPL(clockevents_notify);
474 #endif