ARM: rockchip: fix broken build
[linux/fpc-iii.git] / kernel / time / clockevents.c
blob50eb107f119877ab0762bb671a69cf9c0cd418bc
1 /*
2 * linux/kernel/time/clockevents.c
4 * This file contains functions which manage clock event devices.
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
10 * This code is licenced under the GPL version 2. For details see
11 * kernel-base/COPYING.
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 #include <linux/device.h>
21 #include "tick-internal.h"
23 /* The registered clock event devices */
24 static LIST_HEAD(clockevent_devices);
25 static LIST_HEAD(clockevents_released);
26 /* Protection for the above */
27 static DEFINE_RAW_SPINLOCK(clockevents_lock);
28 /* Protection for unbind operations */
29 static DEFINE_MUTEX(clockevents_mutex);
31 struct ce_unbind {
32 struct clock_event_device *ce;
33 int res;
36 static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
37 bool ismax)
39 u64 clc = (u64) latch << evt->shift;
40 u64 rnd;
42 if (unlikely(!evt->mult)) {
43 evt->mult = 1;
44 WARN_ON(1);
46 rnd = (u64) evt->mult - 1;
49 * Upper bound sanity check. If the backwards conversion is
50 * not equal latch, we know that the above shift overflowed.
52 if ((clc >> evt->shift) != (u64)latch)
53 clc = ~0ULL;
56 * Scaled math oddities:
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.
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.
72 * Also omit the add if it would overflow the u64 boundary.
74 if ((~0ULL - clc > rnd) &&
75 (!ismax || evt->mult <= (1ULL << evt->shift)))
76 clc += rnd;
78 do_div(clc, evt->mult);
80 /* Deltas less than 1usec are pointless noise */
81 return clc > 1000 ? clc : 1000;
84 /**
85 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
86 * @latch: value to convert
87 * @evt: pointer to clock event device descriptor
89 * Math helper, returns latch value converted to nanoseconds (bound checked)
91 u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
93 return cev_delta2ns(latch, evt, false);
95 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
97 static int __clockevents_switch_state(struct clock_event_device *dev,
98 enum clock_event_state state)
100 /* Transition with legacy set_mode() callback */
101 if (dev->set_mode) {
102 /* Legacy callback doesn't support new modes */
103 if (state > CLOCK_EVT_STATE_ONESHOT)
104 return -ENOSYS;
106 * 'clock_event_state' and 'clock_event_mode' have 1-to-1
107 * mapping until *_ONESHOT, and so a simple cast will work.
109 dev->set_mode((enum clock_event_mode)state, dev);
110 dev->mode = (enum clock_event_mode)state;
111 return 0;
114 if (dev->features & CLOCK_EVT_FEAT_DUMMY)
115 return 0;
117 /* Transition with new state-specific callbacks */
118 switch (state) {
119 case CLOCK_EVT_STATE_DETACHED:
120 /* The clockevent device is getting replaced. Shut it down. */
122 case CLOCK_EVT_STATE_SHUTDOWN:
123 if (dev->set_state_shutdown)
124 return dev->set_state_shutdown(dev);
125 return 0;
127 case CLOCK_EVT_STATE_PERIODIC:
128 /* Core internal bug */
129 if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
130 return -ENOSYS;
131 if (dev->set_state_periodic)
132 return dev->set_state_periodic(dev);
133 return 0;
135 case CLOCK_EVT_STATE_ONESHOT:
136 /* Core internal bug */
137 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
138 return -ENOSYS;
139 if (dev->set_state_oneshot)
140 return dev->set_state_oneshot(dev);
141 return 0;
143 case CLOCK_EVT_STATE_ONESHOT_STOPPED:
144 /* Core internal bug */
145 if (WARN_ONCE(!clockevent_state_oneshot(dev),
146 "Current state: %d\n",
147 clockevent_get_state(dev)))
148 return -EINVAL;
150 if (dev->set_state_oneshot_stopped)
151 return dev->set_state_oneshot_stopped(dev);
152 else
153 return -ENOSYS;
155 default:
156 return -ENOSYS;
161 * clockevents_switch_state - set the operating state of a clock event device
162 * @dev: device to modify
163 * @state: new state
165 * Must be called with interrupts disabled !
167 void clockevents_switch_state(struct clock_event_device *dev,
168 enum clock_event_state state)
170 if (clockevent_get_state(dev) != state) {
171 if (__clockevents_switch_state(dev, state))
172 return;
174 clockevent_set_state(dev, state);
177 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
178 * on it, so fix it up and emit a warning:
180 if (clockevent_state_oneshot(dev)) {
181 if (unlikely(!dev->mult)) {
182 dev->mult = 1;
183 WARN_ON(1);
190 * clockevents_shutdown - shutdown the device and clear next_event
191 * @dev: device to shutdown
193 void clockevents_shutdown(struct clock_event_device *dev)
195 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
196 dev->next_event.tv64 = KTIME_MAX;
200 * clockevents_tick_resume - Resume the tick device before using it again
201 * @dev: device to resume
203 int clockevents_tick_resume(struct clock_event_device *dev)
205 int ret = 0;
207 if (dev->set_mode) {
208 dev->set_mode(CLOCK_EVT_MODE_RESUME, dev);
209 dev->mode = CLOCK_EVT_MODE_RESUME;
210 } else if (dev->tick_resume) {
211 ret = dev->tick_resume(dev);
214 return ret;
217 #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
219 /* Limit min_delta to a jiffie */
220 #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
223 * clockevents_increase_min_delta - raise minimum delta of a clock event device
224 * @dev: device to increase the minimum delta
226 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
228 static int clockevents_increase_min_delta(struct clock_event_device *dev)
230 /* Nothing to do if we already reached the limit */
231 if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
232 printk_deferred(KERN_WARNING
233 "CE: Reprogramming failure. Giving up\n");
234 dev->next_event.tv64 = KTIME_MAX;
235 return -ETIME;
238 if (dev->min_delta_ns < 5000)
239 dev->min_delta_ns = 5000;
240 else
241 dev->min_delta_ns += dev->min_delta_ns >> 1;
243 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
244 dev->min_delta_ns = MIN_DELTA_LIMIT;
246 printk_deferred(KERN_WARNING
247 "CE: %s increased min_delta_ns to %llu nsec\n",
248 dev->name ? dev->name : "?",
249 (unsigned long long) dev->min_delta_ns);
250 return 0;
254 * clockevents_program_min_delta - Set clock event device to the minimum delay.
255 * @dev: device to program
257 * Returns 0 on success, -ETIME when the retry loop failed.
259 static int clockevents_program_min_delta(struct clock_event_device *dev)
261 unsigned long long clc;
262 int64_t delta;
263 int i;
265 for (i = 0;;) {
266 delta = dev->min_delta_ns;
267 dev->next_event = ktime_add_ns(ktime_get(), delta);
269 if (clockevent_state_shutdown(dev))
270 return 0;
272 dev->retries++;
273 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
274 if (dev->set_next_event((unsigned long) clc, dev) == 0)
275 return 0;
277 if (++i > 2) {
279 * We tried 3 times to program the device with the
280 * given min_delta_ns. Try to increase the minimum
281 * delta, if that fails as well get out of here.
283 if (clockevents_increase_min_delta(dev))
284 return -ETIME;
285 i = 0;
290 #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
293 * clockevents_program_min_delta - Set clock event device to the minimum delay.
294 * @dev: device to program
296 * Returns 0 on success, -ETIME when the retry loop failed.
298 static int clockevents_program_min_delta(struct clock_event_device *dev)
300 unsigned long long clc;
301 int64_t delta;
303 delta = dev->min_delta_ns;
304 dev->next_event = ktime_add_ns(ktime_get(), delta);
306 if (clockevent_state_shutdown(dev))
307 return 0;
309 dev->retries++;
310 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
311 return dev->set_next_event((unsigned long) clc, dev);
314 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
317 * clockevents_program_event - Reprogram the clock event device.
318 * @dev: device to program
319 * @expires: absolute expiry time (monotonic clock)
320 * @force: program minimum delay if expires can not be set
322 * Returns 0 on success, -ETIME when the event is in the past.
324 int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
325 bool force)
327 unsigned long long clc;
328 int64_t delta;
329 int rc;
331 if (unlikely(expires.tv64 < 0)) {
332 WARN_ON_ONCE(1);
333 return -ETIME;
336 dev->next_event = expires;
338 if (clockevent_state_shutdown(dev))
339 return 0;
341 /* We must be in ONESHOT state here */
342 WARN_ONCE(!clockevent_state_oneshot(dev), "Current state: %d\n",
343 clockevent_get_state(dev));
345 /* Shortcut for clockevent devices that can deal with ktime. */
346 if (dev->features & CLOCK_EVT_FEAT_KTIME)
347 return dev->set_next_ktime(expires, dev);
349 delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
350 if (delta <= 0)
351 return force ? clockevents_program_min_delta(dev) : -ETIME;
353 delta = min(delta, (int64_t) dev->max_delta_ns);
354 delta = max(delta, (int64_t) dev->min_delta_ns);
356 clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
357 rc = dev->set_next_event((unsigned long) clc, dev);
359 return (rc && force) ? clockevents_program_min_delta(dev) : rc;
363 * Called after a notify add to make devices available which were
364 * released from the notifier call.
366 static void clockevents_notify_released(void)
368 struct clock_event_device *dev;
370 while (!list_empty(&clockevents_released)) {
371 dev = list_entry(clockevents_released.next,
372 struct clock_event_device, list);
373 list_del(&dev->list);
374 list_add(&dev->list, &clockevent_devices);
375 tick_check_new_device(dev);
380 * Try to install a replacement clock event device
382 static int clockevents_replace(struct clock_event_device *ced)
384 struct clock_event_device *dev, *newdev = NULL;
386 list_for_each_entry(dev, &clockevent_devices, list) {
387 if (dev == ced || !clockevent_state_detached(dev))
388 continue;
390 if (!tick_check_replacement(newdev, dev))
391 continue;
393 if (!try_module_get(dev->owner))
394 continue;
396 if (newdev)
397 module_put(newdev->owner);
398 newdev = dev;
400 if (newdev) {
401 tick_install_replacement(newdev);
402 list_del_init(&ced->list);
404 return newdev ? 0 : -EBUSY;
408 * Called with clockevents_mutex and clockevents_lock held
410 static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
412 /* Fast track. Device is unused */
413 if (clockevent_state_detached(ced)) {
414 list_del_init(&ced->list);
415 return 0;
418 return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
422 * SMP function call to unbind a device
424 static void __clockevents_unbind(void *arg)
426 struct ce_unbind *cu = arg;
427 int res;
429 raw_spin_lock(&clockevents_lock);
430 res = __clockevents_try_unbind(cu->ce, smp_processor_id());
431 if (res == -EAGAIN)
432 res = clockevents_replace(cu->ce);
433 cu->res = res;
434 raw_spin_unlock(&clockevents_lock);
438 * Issues smp function call to unbind a per cpu device. Called with
439 * clockevents_mutex held.
441 static int clockevents_unbind(struct clock_event_device *ced, int cpu)
443 struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
445 smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
446 return cu.res;
450 * Unbind a clockevents device.
452 int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
454 int ret;
456 mutex_lock(&clockevents_mutex);
457 ret = clockevents_unbind(ced, cpu);
458 mutex_unlock(&clockevents_mutex);
459 return ret;
461 EXPORT_SYMBOL_GPL(clockevents_unbind_device);
463 /* Sanity check of state transition callbacks */
464 static int clockevents_sanity_check(struct clock_event_device *dev)
466 /* Legacy set_mode() callback */
467 if (dev->set_mode) {
468 /* We shouldn't be supporting new modes now */
469 WARN_ON(dev->set_state_periodic || dev->set_state_oneshot ||
470 dev->set_state_shutdown || dev->tick_resume ||
471 dev->set_state_oneshot_stopped);
473 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
474 return 0;
477 if (dev->features & CLOCK_EVT_FEAT_DUMMY)
478 return 0;
480 return 0;
484 * clockevents_register_device - register a clock event device
485 * @dev: device to register
487 void clockevents_register_device(struct clock_event_device *dev)
489 unsigned long flags;
491 BUG_ON(clockevents_sanity_check(dev));
493 /* Initialize state to DETACHED */
494 clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
496 if (!dev->cpumask) {
497 WARN_ON(num_possible_cpus() > 1);
498 dev->cpumask = cpumask_of(smp_processor_id());
501 raw_spin_lock_irqsave(&clockevents_lock, flags);
503 list_add(&dev->list, &clockevent_devices);
504 tick_check_new_device(dev);
505 clockevents_notify_released();
507 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
509 EXPORT_SYMBOL_GPL(clockevents_register_device);
511 void clockevents_config(struct clock_event_device *dev, u32 freq)
513 u64 sec;
515 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
516 return;
519 * Calculate the maximum number of seconds we can sleep. Limit
520 * to 10 minutes for hardware which can program more than
521 * 32bit ticks so we still get reasonable conversion values.
523 sec = dev->max_delta_ticks;
524 do_div(sec, freq);
525 if (!sec)
526 sec = 1;
527 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
528 sec = 600;
530 clockevents_calc_mult_shift(dev, freq, sec);
531 dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
532 dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
536 * clockevents_config_and_register - Configure and register a clock event device
537 * @dev: device to register
538 * @freq: The clock frequency
539 * @min_delta: The minimum clock ticks to program in oneshot mode
540 * @max_delta: The maximum clock ticks to program in oneshot mode
542 * min/max_delta can be 0 for devices which do not support oneshot mode.
544 void clockevents_config_and_register(struct clock_event_device *dev,
545 u32 freq, unsigned long min_delta,
546 unsigned long max_delta)
548 dev->min_delta_ticks = min_delta;
549 dev->max_delta_ticks = max_delta;
550 clockevents_config(dev, freq);
551 clockevents_register_device(dev);
553 EXPORT_SYMBOL_GPL(clockevents_config_and_register);
555 int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
557 clockevents_config(dev, freq);
559 if (clockevent_state_oneshot(dev))
560 return clockevents_program_event(dev, dev->next_event, false);
562 if (clockevent_state_periodic(dev))
563 return __clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
565 return 0;
569 * clockevents_update_freq - Update frequency and reprogram a clock event device.
570 * @dev: device to modify
571 * @freq: new device frequency
573 * Reconfigure and reprogram a clock event device in oneshot
574 * mode. Must be called on the cpu for which the device delivers per
575 * cpu timer events. If called for the broadcast device the core takes
576 * care of serialization.
578 * Returns 0 on success, -ETIME when the event is in the past.
580 int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
582 unsigned long flags;
583 int ret;
585 local_irq_save(flags);
586 ret = tick_broadcast_update_freq(dev, freq);
587 if (ret == -ENODEV)
588 ret = __clockevents_update_freq(dev, freq);
589 local_irq_restore(flags);
590 return ret;
594 * Noop handler when we shut down an event device
596 void clockevents_handle_noop(struct clock_event_device *dev)
601 * clockevents_exchange_device - release and request clock devices
602 * @old: device to release (can be NULL)
603 * @new: device to request (can be NULL)
605 * Called from various tick functions with clockevents_lock held and
606 * interrupts disabled.
608 void clockevents_exchange_device(struct clock_event_device *old,
609 struct clock_event_device *new)
612 * Caller releases a clock event device. We queue it into the
613 * released list and do a notify add later.
615 if (old) {
616 module_put(old->owner);
617 clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED);
618 list_del(&old->list);
619 list_add(&old->list, &clockevents_released);
622 if (new) {
623 BUG_ON(!clockevent_state_detached(new));
624 clockevents_shutdown(new);
629 * clockevents_suspend - suspend clock devices
631 void clockevents_suspend(void)
633 struct clock_event_device *dev;
635 list_for_each_entry_reverse(dev, &clockevent_devices, list)
636 if (dev->suspend && !clockevent_state_detached(dev))
637 dev->suspend(dev);
641 * clockevents_resume - resume clock devices
643 void clockevents_resume(void)
645 struct clock_event_device *dev;
647 list_for_each_entry(dev, &clockevent_devices, list)
648 if (dev->resume && !clockevent_state_detached(dev))
649 dev->resume(dev);
652 #ifdef CONFIG_HOTPLUG_CPU
654 * tick_cleanup_dead_cpu - Cleanup the tick and clockevents of a dead cpu
656 void tick_cleanup_dead_cpu(int cpu)
658 struct clock_event_device *dev, *tmp;
659 unsigned long flags;
661 raw_spin_lock_irqsave(&clockevents_lock, flags);
663 tick_shutdown_broadcast_oneshot(cpu);
664 tick_shutdown_broadcast(cpu);
665 tick_shutdown(cpu);
667 * Unregister the clock event devices which were
668 * released from the users in the notify chain.
670 list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
671 list_del(&dev->list);
673 * Now check whether the CPU has left unused per cpu devices
675 list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
676 if (cpumask_test_cpu(cpu, dev->cpumask) &&
677 cpumask_weight(dev->cpumask) == 1 &&
678 !tick_is_broadcast_device(dev)) {
679 BUG_ON(!clockevent_state_detached(dev));
680 list_del(&dev->list);
683 raw_spin_unlock_irqrestore(&clockevents_lock, flags);
685 #endif
687 #ifdef CONFIG_SYSFS
688 struct bus_type clockevents_subsys = {
689 .name = "clockevents",
690 .dev_name = "clockevent",
693 static DEFINE_PER_CPU(struct device, tick_percpu_dev);
694 static struct tick_device *tick_get_tick_dev(struct device *dev);
696 static ssize_t sysfs_show_current_tick_dev(struct device *dev,
697 struct device_attribute *attr,
698 char *buf)
700 struct tick_device *td;
701 ssize_t count = 0;
703 raw_spin_lock_irq(&clockevents_lock);
704 td = tick_get_tick_dev(dev);
705 if (td && td->evtdev)
706 count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
707 raw_spin_unlock_irq(&clockevents_lock);
708 return count;
710 static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
712 /* We don't support the abomination of removable broadcast devices */
713 static ssize_t sysfs_unbind_tick_dev(struct device *dev,
714 struct device_attribute *attr,
715 const char *buf, size_t count)
717 char name[CS_NAME_LEN];
718 ssize_t ret = sysfs_get_uname(buf, name, count);
719 struct clock_event_device *ce;
721 if (ret < 0)
722 return ret;
724 ret = -ENODEV;
725 mutex_lock(&clockevents_mutex);
726 raw_spin_lock_irq(&clockevents_lock);
727 list_for_each_entry(ce, &clockevent_devices, list) {
728 if (!strcmp(ce->name, name)) {
729 ret = __clockevents_try_unbind(ce, dev->id);
730 break;
733 raw_spin_unlock_irq(&clockevents_lock);
735 * We hold clockevents_mutex, so ce can't go away
737 if (ret == -EAGAIN)
738 ret = clockevents_unbind(ce, dev->id);
739 mutex_unlock(&clockevents_mutex);
740 return ret ? ret : count;
742 static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
744 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
745 static struct device tick_bc_dev = {
746 .init_name = "broadcast",
747 .id = 0,
748 .bus = &clockevents_subsys,
751 static struct tick_device *tick_get_tick_dev(struct device *dev)
753 return dev == &tick_bc_dev ? tick_get_broadcast_device() :
754 &per_cpu(tick_cpu_device, dev->id);
757 static __init int tick_broadcast_init_sysfs(void)
759 int err = device_register(&tick_bc_dev);
761 if (!err)
762 err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
763 return err;
765 #else
766 static struct tick_device *tick_get_tick_dev(struct device *dev)
768 return &per_cpu(tick_cpu_device, dev->id);
770 static inline int tick_broadcast_init_sysfs(void) { return 0; }
771 #endif
773 static int __init tick_init_sysfs(void)
775 int cpu;
777 for_each_possible_cpu(cpu) {
778 struct device *dev = &per_cpu(tick_percpu_dev, cpu);
779 int err;
781 dev->id = cpu;
782 dev->bus = &clockevents_subsys;
783 err = device_register(dev);
784 if (!err)
785 err = device_create_file(dev, &dev_attr_current_device);
786 if (!err)
787 err = device_create_file(dev, &dev_attr_unbind_device);
788 if (err)
789 return err;
791 return tick_broadcast_init_sysfs();
794 static int __init clockevents_init_sysfs(void)
796 int err = subsys_system_register(&clockevents_subsys, NULL);
798 if (!err)
799 err = tick_init_sysfs();
800 return err;
802 device_initcall(clockevents_init_sysfs);
803 #endif /* SYSFS */