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
);
32 struct clock_event_device
*ce
;
37 * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
38 * @latch: value to convert
39 * @evt: pointer to clock event device descriptor
41 * Math helper, returns latch value converted to nanoseconds (bound checked)
43 u64
clockevent_delta2ns(unsigned long latch
, struct clock_event_device
*evt
)
45 u64 clc
= (u64
) latch
<< evt
->shift
;
47 if (unlikely(!evt
->mult
)) {
52 do_div(clc
, evt
->mult
);
60 EXPORT_SYMBOL_GPL(clockevent_delta2ns
);
63 * clockevents_set_mode - set the operating mode of a clock event device
64 * @dev: device to modify
67 * Must be called with interrupts disabled !
69 void clockevents_set_mode(struct clock_event_device
*dev
,
70 enum clock_event_mode mode
)
72 if (dev
->mode
!= mode
) {
73 dev
->set_mode(mode
, dev
);
77 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
78 * on it, so fix it up and emit a warning:
80 if (mode
== CLOCK_EVT_MODE_ONESHOT
) {
81 if (unlikely(!dev
->mult
)) {
90 * clockevents_shutdown - shutdown the device and clear next_event
91 * @dev: device to shutdown
93 void clockevents_shutdown(struct clock_event_device
*dev
)
95 clockevents_set_mode(dev
, CLOCK_EVT_MODE_SHUTDOWN
);
96 dev
->next_event
.tv64
= KTIME_MAX
;
99 #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
101 /* Limit min_delta to a jiffie */
102 #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
105 * clockevents_increase_min_delta - raise minimum delta of a clock event device
106 * @dev: device to increase the minimum delta
108 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
110 static int clockevents_increase_min_delta(struct clock_event_device
*dev
)
112 /* Nothing to do if we already reached the limit */
113 if (dev
->min_delta_ns
>= MIN_DELTA_LIMIT
) {
114 printk(KERN_WARNING
"CE: Reprogramming failure. Giving up\n");
115 dev
->next_event
.tv64
= KTIME_MAX
;
119 if (dev
->min_delta_ns
< 5000)
120 dev
->min_delta_ns
= 5000;
122 dev
->min_delta_ns
+= dev
->min_delta_ns
>> 1;
124 if (dev
->min_delta_ns
> MIN_DELTA_LIMIT
)
125 dev
->min_delta_ns
= MIN_DELTA_LIMIT
;
127 printk(KERN_WARNING
"CE: %s increased min_delta_ns to %llu nsec\n",
128 dev
->name
? dev
->name
: "?",
129 (unsigned long long) dev
->min_delta_ns
);
134 * clockevents_program_min_delta - Set clock event device to the minimum delay.
135 * @dev: device to program
137 * Returns 0 on success, -ETIME when the retry loop failed.
139 static int clockevents_program_min_delta(struct clock_event_device
*dev
)
141 unsigned long long clc
;
146 delta
= dev
->min_delta_ns
;
147 dev
->next_event
= ktime_add_ns(ktime_get(), delta
);
149 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
153 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
154 if (dev
->set_next_event((unsigned long) clc
, dev
) == 0)
159 * We tried 3 times to program the device with the
160 * given min_delta_ns. Try to increase the minimum
161 * delta, if that fails as well get out of here.
163 if (clockevents_increase_min_delta(dev
))
170 #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
173 * clockevents_program_min_delta - Set clock event device to the minimum delay.
174 * @dev: device to program
176 * Returns 0 on success, -ETIME when the retry loop failed.
178 static int clockevents_program_min_delta(struct clock_event_device
*dev
)
180 unsigned long long clc
;
183 delta
= dev
->min_delta_ns
;
184 dev
->next_event
= ktime_add_ns(ktime_get(), delta
);
186 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
190 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
191 return dev
->set_next_event((unsigned long) clc
, dev
);
194 #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
197 * clockevents_program_event - Reprogram the clock event device.
198 * @dev: device to program
199 * @expires: absolute expiry time (monotonic clock)
200 * @force: program minimum delay if expires can not be set
202 * Returns 0 on success, -ETIME when the event is in the past.
204 int clockevents_program_event(struct clock_event_device
*dev
, ktime_t expires
,
207 unsigned long long clc
;
211 if (unlikely(expires
.tv64
< 0)) {
216 dev
->next_event
= expires
;
218 if (dev
->mode
== CLOCK_EVT_MODE_SHUTDOWN
)
221 /* Shortcut for clockevent devices that can deal with ktime. */
222 if (dev
->features
& CLOCK_EVT_FEAT_KTIME
)
223 return dev
->set_next_ktime(expires
, dev
);
225 delta
= ktime_to_ns(ktime_sub(expires
, ktime_get()));
227 return force
? clockevents_program_min_delta(dev
) : -ETIME
;
229 delta
= min(delta
, (int64_t) dev
->max_delta_ns
);
230 delta
= max(delta
, (int64_t) dev
->min_delta_ns
);
232 clc
= ((unsigned long long) delta
* dev
->mult
) >> dev
->shift
;
233 rc
= dev
->set_next_event((unsigned long) clc
, dev
);
235 return (rc
&& force
) ? clockevents_program_min_delta(dev
) : rc
;
239 * Called after a notify add to make devices available which were
240 * released from the notifier call.
242 static void clockevents_notify_released(void)
244 struct clock_event_device
*dev
;
246 while (!list_empty(&clockevents_released
)) {
247 dev
= list_entry(clockevents_released
.next
,
248 struct clock_event_device
, list
);
249 list_del(&dev
->list
);
250 list_add(&dev
->list
, &clockevent_devices
);
251 tick_check_new_device(dev
);
256 * Try to install a replacement clock event device
258 static int clockevents_replace(struct clock_event_device
*ced
)
260 struct clock_event_device
*dev
, *newdev
= NULL
;
262 list_for_each_entry(dev
, &clockevent_devices
, list
) {
263 if (dev
== ced
|| dev
->mode
!= CLOCK_EVT_MODE_UNUSED
)
266 if (!tick_check_replacement(newdev
, dev
))
269 if (!try_module_get(dev
->owner
))
273 module_put(newdev
->owner
);
277 tick_install_replacement(newdev
);
278 list_del_init(&ced
->list
);
280 return newdev
? 0 : -EBUSY
;
284 * Called with clockevents_mutex and clockevents_lock held
286 static int __clockevents_try_unbind(struct clock_event_device
*ced
, int cpu
)
288 /* Fast track. Device is unused */
289 if (ced
->mode
== CLOCK_EVT_MODE_UNUSED
) {
290 list_del_init(&ced
->list
);
294 return ced
== per_cpu(tick_cpu_device
, cpu
).evtdev
? -EAGAIN
: -EBUSY
;
298 * SMP function call to unbind a device
300 static void __clockevents_unbind(void *arg
)
302 struct ce_unbind
*cu
= arg
;
305 raw_spin_lock(&clockevents_lock
);
306 res
= __clockevents_try_unbind(cu
->ce
, smp_processor_id());
308 res
= clockevents_replace(cu
->ce
);
310 raw_spin_unlock(&clockevents_lock
);
314 * Issues smp function call to unbind a per cpu device. Called with
315 * clockevents_mutex held.
317 static int clockevents_unbind(struct clock_event_device
*ced
, int cpu
)
319 struct ce_unbind cu
= { .ce
= ced
, .res
= -ENODEV
};
321 smp_call_function_single(cpu
, __clockevents_unbind
, &cu
, 1);
326 * Unbind a clockevents device.
328 int clockevents_unbind_device(struct clock_event_device
*ced
, int cpu
)
332 mutex_lock(&clockevents_mutex
);
333 ret
= clockevents_unbind(ced
, cpu
);
334 mutex_unlock(&clockevents_mutex
);
337 EXPORT_SYMBOL_GPL(clockevents_unbind
);
340 * clockevents_register_device - register a clock event device
341 * @dev: device to register
343 void clockevents_register_device(struct clock_event_device
*dev
)
347 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
349 WARN_ON(num_possible_cpus() > 1);
350 dev
->cpumask
= cpumask_of(smp_processor_id());
353 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
355 list_add(&dev
->list
, &clockevent_devices
);
356 tick_check_new_device(dev
);
357 clockevents_notify_released();
359 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
361 EXPORT_SYMBOL_GPL(clockevents_register_device
);
363 void clockevents_config(struct clock_event_device
*dev
, u32 freq
)
367 if (!(dev
->features
& CLOCK_EVT_FEAT_ONESHOT
))
371 * Calculate the maximum number of seconds we can sleep. Limit
372 * to 10 minutes for hardware which can program more than
373 * 32bit ticks so we still get reasonable conversion values.
375 sec
= dev
->max_delta_ticks
;
379 else if (sec
> 600 && dev
->max_delta_ticks
> UINT_MAX
)
382 clockevents_calc_mult_shift(dev
, freq
, sec
);
383 dev
->min_delta_ns
= clockevent_delta2ns(dev
->min_delta_ticks
, dev
);
384 dev
->max_delta_ns
= clockevent_delta2ns(dev
->max_delta_ticks
, dev
);
388 * clockevents_config_and_register - Configure and register a clock event device
389 * @dev: device to register
390 * @freq: The clock frequency
391 * @min_delta: The minimum clock ticks to program in oneshot mode
392 * @max_delta: The maximum clock ticks to program in oneshot mode
394 * min/max_delta can be 0 for devices which do not support oneshot mode.
396 void clockevents_config_and_register(struct clock_event_device
*dev
,
397 u32 freq
, unsigned long min_delta
,
398 unsigned long max_delta
)
400 dev
->min_delta_ticks
= min_delta
;
401 dev
->max_delta_ticks
= max_delta
;
402 clockevents_config(dev
, freq
);
403 clockevents_register_device(dev
);
405 EXPORT_SYMBOL_GPL(clockevents_config_and_register
);
408 * clockevents_update_freq - Update frequency and reprogram a clock event device.
409 * @dev: device to modify
410 * @freq: new device frequency
412 * Reconfigure and reprogram a clock event device in oneshot
413 * mode. Must be called on the cpu for which the device delivers per
414 * cpu timer events with interrupts disabled! Returns 0 on success,
415 * -ETIME when the event is in the past.
417 int clockevents_update_freq(struct clock_event_device
*dev
, u32 freq
)
419 clockevents_config(dev
, freq
);
421 if (dev
->mode
!= CLOCK_EVT_MODE_ONESHOT
)
424 return clockevents_program_event(dev
, dev
->next_event
, false);
428 * Noop handler when we shut down an event device
430 void clockevents_handle_noop(struct clock_event_device
*dev
)
435 * clockevents_exchange_device - release and request clock devices
436 * @old: device to release (can be NULL)
437 * @new: device to request (can be NULL)
439 * Called from the notifier chain. clockevents_lock is held already
441 void clockevents_exchange_device(struct clock_event_device
*old
,
442 struct clock_event_device
*new)
446 local_irq_save(flags
);
448 * Caller releases a clock event device. We queue it into the
449 * released list and do a notify add later.
452 module_put(old
->owner
);
453 clockevents_set_mode(old
, CLOCK_EVT_MODE_UNUSED
);
454 list_del(&old
->list
);
455 list_add(&old
->list
, &clockevents_released
);
459 BUG_ON(new->mode
!= CLOCK_EVT_MODE_UNUSED
);
460 clockevents_shutdown(new);
462 local_irq_restore(flags
);
466 * clockevents_suspend - suspend clock devices
468 void clockevents_suspend(void)
470 struct clock_event_device
*dev
;
472 list_for_each_entry_reverse(dev
, &clockevent_devices
, list
)
478 * clockevents_resume - resume clock devices
480 void clockevents_resume(void)
482 struct clock_event_device
*dev
;
484 list_for_each_entry(dev
, &clockevent_devices
, list
)
489 #ifdef CONFIG_GENERIC_CLOCKEVENTS
491 * clockevents_notify - notification about relevant events
493 void clockevents_notify(unsigned long reason
, void *arg
)
495 struct clock_event_device
*dev
, *tmp
;
499 raw_spin_lock_irqsave(&clockevents_lock
, flags
);
502 case CLOCK_EVT_NOTIFY_BROADCAST_ON
:
503 case CLOCK_EVT_NOTIFY_BROADCAST_OFF
:
504 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE
:
505 tick_broadcast_on_off(reason
, arg
);
508 case CLOCK_EVT_NOTIFY_BROADCAST_ENTER
:
509 case CLOCK_EVT_NOTIFY_BROADCAST_EXIT
:
510 tick_broadcast_oneshot_control(reason
);
513 case CLOCK_EVT_NOTIFY_CPU_DYING
:
514 tick_handover_do_timer(arg
);
517 case CLOCK_EVT_NOTIFY_SUSPEND
:
519 tick_suspend_broadcast();
522 case CLOCK_EVT_NOTIFY_RESUME
:
526 case CLOCK_EVT_NOTIFY_CPU_DEAD
:
527 tick_shutdown_broadcast_oneshot(arg
);
528 tick_shutdown_broadcast(arg
);
531 * Unregister the clock event devices which were
532 * released from the users in the notify chain.
534 list_for_each_entry_safe(dev
, tmp
, &clockevents_released
, list
)
535 list_del(&dev
->list
);
537 * Now check whether the CPU has left unused per cpu devices
540 list_for_each_entry_safe(dev
, tmp
, &clockevent_devices
, list
) {
541 if (cpumask_test_cpu(cpu
, dev
->cpumask
) &&
542 cpumask_weight(dev
->cpumask
) == 1 &&
543 !tick_is_broadcast_device(dev
)) {
544 BUG_ON(dev
->mode
!= CLOCK_EVT_MODE_UNUSED
);
545 list_del(&dev
->list
);
552 raw_spin_unlock_irqrestore(&clockevents_lock
, flags
);
554 EXPORT_SYMBOL_GPL(clockevents_notify
);
557 struct bus_type clockevents_subsys
= {
558 .name
= "clockevents",
559 .dev_name
= "clockevent",
562 static DEFINE_PER_CPU(struct device
, tick_percpu_dev
);
563 static struct tick_device
*tick_get_tick_dev(struct device
*dev
);
565 static ssize_t
sysfs_show_current_tick_dev(struct device
*dev
,
566 struct device_attribute
*attr
,
569 struct tick_device
*td
;
572 raw_spin_lock_irq(&clockevents_lock
);
573 td
= tick_get_tick_dev(dev
);
574 if (td
&& td
->evtdev
)
575 count
= snprintf(buf
, PAGE_SIZE
, "%s\n", td
->evtdev
->name
);
576 raw_spin_unlock_irq(&clockevents_lock
);
579 static DEVICE_ATTR(current_device
, 0444, sysfs_show_current_tick_dev
, NULL
);
581 /* We don't support the abomination of removable broadcast devices */
582 static ssize_t
sysfs_unbind_tick_dev(struct device
*dev
,
583 struct device_attribute
*attr
,
584 const char *buf
, size_t count
)
586 char name
[CS_NAME_LEN
];
587 size_t ret
= sysfs_get_uname(buf
, name
, count
);
588 struct clock_event_device
*ce
;
594 mutex_lock(&clockevents_mutex
);
595 raw_spin_lock_irq(&clockevents_lock
);
596 list_for_each_entry(ce
, &clockevent_devices
, list
) {
597 if (!strcmp(ce
->name
, name
)) {
598 ret
= __clockevents_try_unbind(ce
, dev
->id
);
602 raw_spin_unlock_irq(&clockevents_lock
);
604 * We hold clockevents_mutex, so ce can't go away
607 ret
= clockevents_unbind(ce
, dev
->id
);
608 mutex_unlock(&clockevents_mutex
);
609 return ret
? ret
: count
;
611 static DEVICE_ATTR(unbind_device
, 0200, NULL
, sysfs_unbind_tick_dev
);
613 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
614 static struct device tick_bc_dev
= {
615 .init_name
= "broadcast",
617 .bus
= &clockevents_subsys
,
620 static struct tick_device
*tick_get_tick_dev(struct device
*dev
)
622 return dev
== &tick_bc_dev
? tick_get_broadcast_device() :
623 &per_cpu(tick_cpu_device
, dev
->id
);
626 static __init
int tick_broadcast_init_sysfs(void)
628 int err
= device_register(&tick_bc_dev
);
631 err
= device_create_file(&tick_bc_dev
, &dev_attr_current_device
);
635 static struct tick_device
*tick_get_tick_dev(struct device
*dev
)
637 return &per_cpu(tick_cpu_device
, dev
->id
);
639 static inline int tick_broadcast_init_sysfs(void) { return 0; }
642 static int __init
tick_init_sysfs(void)
646 for_each_possible_cpu(cpu
) {
647 struct device
*dev
= &per_cpu(tick_percpu_dev
, cpu
);
651 dev
->bus
= &clockevents_subsys
;
652 err
= device_register(dev
);
654 err
= device_create_file(dev
, &dev_attr_current_device
);
656 err
= device_create_file(dev
, &dev_attr_unbind_device
);
660 return tick_broadcast_init_sysfs();
663 static int __init
clockevents_init_sysfs(void)
665 int err
= subsys_system_register(&clockevents_subsys
, NULL
);
668 err
= tick_init_sysfs();
671 device_initcall(clockevents_init_sysfs
);
674 #endif /* GENERIC_CLOCK_EVENTS */