rk312x DDR:idle port when power domain is normal
[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(KERN_WARNING "CE: Reprogramming failure. Giving up\n");
142                 dev->next_event.tv64 = KTIME_MAX;
143                 return -ETIME;
144         }
145
146         if (dev->min_delta_ns < 5000)
147                 dev->min_delta_ns = 5000;
148         else
149                 dev->min_delta_ns += dev->min_delta_ns >> 1;
150
151         if (dev->min_delta_ns > MIN_DELTA_LIMIT)
152                 dev->min_delta_ns = MIN_DELTA_LIMIT;
153
154         printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
155                dev->name ? dev->name : "?",
156                (unsigned long long) dev->min_delta_ns);
157         return 0;
158 }
159
160 /**
161  * clockevents_program_min_delta - Set clock event device to the minimum delay.
162  * @dev:        device to program
163  *
164  * Returns 0 on success, -ETIME when the retry loop failed.
165  */
166 static int clockevents_program_min_delta(struct clock_event_device *dev)
167 {
168         unsigned long long clc;
169         int64_t delta;
170         int i;
171
172         for (i = 0;;) {
173                 delta = dev->min_delta_ns;
174                 dev->next_event = ktime_add_ns(ktime_get(), delta);
175
176                 if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
177                         return 0;
178
179                 dev->retries++;
180                 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
181                 if (dev->set_next_event((unsigned long) clc, dev) == 0)
182                         return 0;
183
184                 if (++i > 2) {
185                         /*
186                          * We tried 3 times to program the device with the
187                          * given min_delta_ns. Try to increase the minimum
188                          * delta, if that fails as well get out of here.
189                          */
190                         if (clockevents_increase_min_delta(dev))
191                                 return -ETIME;
192                         i = 0;
193                 }
194         }
195 }
196
197 #else  /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
198
199 /**
200  * clockevents_program_min_delta - Set clock event device to the minimum delay.
201  * @dev:        device to program
202  *
203  * Returns 0 on success, -ETIME when the retry loop failed.
204  */
205 static int clockevents_program_min_delta(struct clock_event_device *dev)
206 {
207         unsigned long long clc;
208         int64_t delta;
209
210         delta = dev->min_delta_ns;
211         dev->next_event = ktime_add_ns(ktime_get(), delta);
212
213         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
214                 return 0;
215
216         dev->retries++;
217         clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
218         return dev->set_next_event((unsigned long) clc, dev);
219 }
220
221 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
222
223 /**
224  * clockevents_program_event - Reprogram the clock event device.
225  * @dev:        device to program
226  * @expires:    absolute expiry time (monotonic clock)
227  * @force:      program minimum delay if expires can not be set
228  *
229  * Returns 0 on success, -ETIME when the event is in the past.
230  */
231 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
232                               bool force)
233 {
234         unsigned long long clc;
235         int64_t delta;
236         int rc;
237
238         if (unlikely(expires.tv64 < 0)) {
239                 WARN_ON_ONCE(1);
240                 return -ETIME;
241         }
242
243         dev->next_event = expires;
244
245         if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
246                 return 0;
247
248         /* Shortcut for clockevent devices that can deal with ktime. */
249         if (dev->features & CLOCK_EVT_FEAT_KTIME)
250                 return dev->set_next_ktime(expires, dev);
251
252         delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
253         if (delta <= 0)
254                 return force ? clockevents_program_min_delta(dev) : -ETIME;
255
256         delta = min(delta, (int64_t) dev->max_delta_ns);
257         delta = max(delta, (int64_t) dev->min_delta_ns);
258
259         clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
260         rc = dev->set_next_event((unsigned long) clc, dev);
261
262         return (rc && force) ? clockevents_program_min_delta(dev) : rc;
263 }
264
265 /*
266  * Called after a notify add to make devices available which were
267  * released from the notifier call.
268  */
269 static void clockevents_notify_released(void)
270 {
271         struct clock_event_device *dev;
272
273         while (!list_empty(&clockevents_released)) {
274                 dev = list_entry(clockevents_released.next,
275                                  struct clock_event_device, list);
276                 list_del(&dev->list);
277                 list_add(&dev->list, &clockevent_devices);
278                 tick_check_new_device(dev);
279         }
280 }
281
282 /**
283  * clockevents_register_device - register a clock event device
284  * @dev:        device to register
285  */
286 void clockevents_register_device(struct clock_event_device *dev)
287 {
288         unsigned long flags;
289
290         BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
291         if (!dev->cpumask) {
292                 WARN_ON(num_possible_cpus() > 1);
293                 dev->cpumask = cpumask_of(smp_processor_id());
294         }
295
296         raw_spin_lock_irqsave(&clockevents_lock, flags);
297
298         list_add(&dev->list, &clockevent_devices);
299         tick_check_new_device(dev);
300         clockevents_notify_released();
301
302         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
303 }
304 EXPORT_SYMBOL_GPL(clockevents_register_device);
305
306 void clockevents_config(struct clock_event_device *dev, u32 freq)
307 {
308         u64 sec;
309
310         if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
311                 return;
312
313         /*
314          * Calculate the maximum number of seconds we can sleep. Limit
315          * to 10 minutes for hardware which can program more than
316          * 32bit ticks so we still get reasonable conversion values.
317          */
318         sec = dev->max_delta_ticks;
319         do_div(sec, freq);
320         if (!sec)
321                 sec = 1;
322         else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
323                 sec = 600;
324
325         clockevents_calc_mult_shift(dev, freq, sec);
326         dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
327         dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
328 }
329
330 /**
331  * clockevents_config_and_register - Configure and register a clock event device
332  * @dev:        device to register
333  * @freq:       The clock frequency
334  * @min_delta:  The minimum clock ticks to program in oneshot mode
335  * @max_delta:  The maximum clock ticks to program in oneshot mode
336  *
337  * min/max_delta can be 0 for devices which do not support oneshot mode.
338  */
339 void clockevents_config_and_register(struct clock_event_device *dev,
340                                      u32 freq, unsigned long min_delta,
341                                      unsigned long max_delta)
342 {
343         dev->min_delta_ticks = min_delta;
344         dev->max_delta_ticks = max_delta;
345         clockevents_config(dev, freq);
346         clockevents_register_device(dev);
347 }
348 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
349
350 /**
351  * clockevents_update_freq - Update frequency and reprogram a clock event device.
352  * @dev:        device to modify
353  * @freq:       new device frequency
354  *
355  * Reconfigure and reprogram a clock event device in oneshot
356  * mode. Must be called on the cpu for which the device delivers per
357  * cpu timer events with interrupts disabled!  Returns 0 on success,
358  * -ETIME when the event is in the past.
359  */
360 int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
361 {
362         clockevents_config(dev, freq);
363
364         if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
365                 return 0;
366
367         return clockevents_program_event(dev, dev->next_event, false);
368 }
369
370 /*
371  * Noop handler when we shut down an event device
372  */
373 void clockevents_handle_noop(struct clock_event_device *dev)
374 {
375 }
376
377 /**
378  * clockevents_exchange_device - release and request clock devices
379  * @old:        device to release (can be NULL)
380  * @new:        device to request (can be NULL)
381  *
382  * Called from the notifier chain. clockevents_lock is held already
383  */
384 void clockevents_exchange_device(struct clock_event_device *old,
385                                  struct clock_event_device *new)
386 {
387         unsigned long flags;
388
389         local_irq_save(flags);
390         /*
391          * Caller releases a clock event device. We queue it into the
392          * released list and do a notify add later.
393          */
394         if (old) {
395                 module_put(old->owner);
396                 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
397                 list_del(&old->list);
398                 list_add(&old->list, &clockevents_released);
399         }
400
401         if (new) {
402                 BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
403                 clockevents_shutdown(new);
404         }
405         local_irq_restore(flags);
406 }
407
408 /**
409  * clockevents_suspend - suspend clock devices
410  */
411 void clockevents_suspend(void)
412 {
413         struct clock_event_device *dev;
414
415         list_for_each_entry_reverse(dev, &clockevent_devices, list)
416                 if (dev->suspend)
417                         dev->suspend(dev);
418 }
419
420 /**
421  * clockevents_resume - resume clock devices
422  */
423 void clockevents_resume(void)
424 {
425         struct clock_event_device *dev;
426
427         list_for_each_entry(dev, &clockevent_devices, list)
428                 if (dev->resume)
429                         dev->resume(dev);
430 }
431
432 #ifdef CONFIG_GENERIC_CLOCKEVENTS
433 /**
434  * clockevents_notify - notification about relevant events
435  */
436 void clockevents_notify(unsigned long reason, void *arg)
437 {
438         struct clock_event_device *dev, *tmp;
439         unsigned long flags;
440         int cpu;
441
442         raw_spin_lock_irqsave(&clockevents_lock, flags);
443         tick_notify(reason, arg);
444
445         switch (reason) {
446         case CLOCK_EVT_NOTIFY_CPU_DEAD:
447                 /*
448                  * Unregister the clock event devices which were
449                  * released from the users in the notify chain.
450                  */
451                 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
452                         list_del(&dev->list);
453                 /*
454                  * Now check whether the CPU has left unused per cpu devices
455                  */
456                 cpu = *((int *)arg);
457                 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
458                         if (cpumask_test_cpu(cpu, dev->cpumask) &&
459                             cpumask_weight(dev->cpumask) == 1 &&
460                             !tick_is_broadcast_device(dev)) {
461                                 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
462                                 list_del(&dev->list);
463                         }
464                 }
465                 break;
466         default:
467                 break;
468         }
469         raw_spin_unlock_irqrestore(&clockevents_lock, flags);
470 }
471 EXPORT_SYMBOL_GPL(clockevents_notify);
472 #endif