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#include <linux/device.h>
20
21#include "tick-internal.h"
22
23/* The registered clock event devices */
24static LIST_HEAD(clockevent_devices);
25static LIST_HEAD(clockevents_released);
26/* Protection for the above */
27static DEFINE_RAW_SPINLOCK(clockevents_lock);
28/* Protection for unbind operations */
29static DEFINE_MUTEX(clockevents_mutex);
30
31struct ce_unbind {
32	struct clock_event_device *ce;
33	int res;
34};
35
36static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
37			bool ismax)
38{
39	u64 clc = (u64) latch << evt->shift;
40	u64 rnd;
41
42	if (unlikely(!evt->mult)) {
43		evt->mult = 1;
44		WARN_ON(1);
45	}
46	rnd = (u64) evt->mult - 1;
47
48	/*
49	 * Upper bound sanity check. If the backwards conversion is
50	 * not equal latch, we know that the above shift overflowed.
51	 */
52	if ((clc >> evt->shift) != (u64)latch)
53		clc = ~0ULL;
54
55	/*
56	 * Scaled math oddities:
57	 *
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.
61	 *
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.
71	 *
72	 * Also omit the add if it would overflow the u64 boundary.
73	 */
74	if ((~0ULL - clc > rnd) &&
75	    (!ismax || evt->mult <= (1ULL << evt->shift)))
76		clc += rnd;
77
78	do_div(clc, evt->mult);
79
80	/* Deltas less than 1usec are pointless noise */
81	return clc > 1000 ? clc : 1000;
82}
83
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
88 *
89 * Math helper, returns latch value converted to nanoseconds (bound checked)
90 */
91u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
92{
93	return cev_delta2ns(latch, evt, false);
94}
95EXPORT_SYMBOL_GPL(clockevent_delta2ns);
96
97/**
98 * clockevents_set_mode - set the operating mode of a clock event device
99 * @dev:	device to modify
100 * @mode:	new mode
101 *
102 * Must be called with interrupts disabled !
103 */
104void clockevents_set_mode(struct clock_event_device *dev,
105				 enum clock_event_mode mode)
106{
107	if (dev->mode != mode) {
108		dev->set_mode(mode, dev);
109		dev->mode = mode;
110
111		/*
112		 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
113		 * on it, so fix it up and emit a warning:
114		 */
115		if (mode == CLOCK_EVT_MODE_ONESHOT) {
116			if (unlikely(!dev->mult)) {
117				dev->mult = 1;
118				WARN_ON(1);
119			}
120		}
121	}
122}
123
124/**
125 * clockevents_shutdown - shutdown the device and clear next_event
126 * @dev:	device to shutdown
127 */
128void clockevents_shutdown(struct clock_event_device *dev)
129{
130	clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
131	dev->next_event.tv64 = KTIME_MAX;
132}
133
134#ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
135
136/* Limit min_delta to a jiffie */
137#define MIN_DELTA_LIMIT		(NSEC_PER_SEC / HZ)
138
139/**
140 * clockevents_increase_min_delta - raise minimum delta of a clock event device
141 * @dev:       device to increase the minimum delta
142 *
143 * Returns 0 on success, -ETIME when the minimum delta reached the limit.
144 */
145static int clockevents_increase_min_delta(struct clock_event_device *dev)
146{
147	/* Nothing to do if we already reached the limit */
148	if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
149		printk_deferred(KERN_WARNING
150				"CE: Reprogramming failure. Giving up\n");
151		dev->next_event.tv64 = KTIME_MAX;
152		return -ETIME;
153	}
154
155	if (dev->min_delta_ns < 5000)
156		dev->min_delta_ns = 5000;
157	else
158		dev->min_delta_ns += dev->min_delta_ns >> 1;
159
160	if (dev->min_delta_ns > MIN_DELTA_LIMIT)
161		dev->min_delta_ns = MIN_DELTA_LIMIT;
162
163	printk_deferred(KERN_WARNING
164			"CE: %s increased min_delta_ns to %llu nsec\n",
165			dev->name ? dev->name : "?",
166			(unsigned long long) dev->min_delta_ns);
167	return 0;
168}
169
170/**
171 * clockevents_program_min_delta - Set clock event device to the minimum delay.
172 * @dev:	device to program
173 *
174 * Returns 0 on success, -ETIME when the retry loop failed.
175 */
176static int clockevents_program_min_delta(struct clock_event_device *dev)
177{
178	unsigned long long clc;
179	int64_t delta;
180	int i;
181
182	for (i = 0;;) {
183		delta = dev->min_delta_ns;
184		dev->next_event = ktime_add_ns(ktime_get(), delta);
185
186		if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
187			return 0;
188
189		dev->retries++;
190		clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
191		if (dev->set_next_event((unsigned long) clc, dev) == 0)
192			return 0;
193
194		if (++i > 2) {
195			/*
196			 * We tried 3 times to program the device with the
197			 * given min_delta_ns. Try to increase the minimum
198			 * delta, if that fails as well get out of here.
199			 */
200			if (clockevents_increase_min_delta(dev))
201				return -ETIME;
202			i = 0;
203		}
204	}
205}
206
207#else  /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
208
209/**
210 * clockevents_program_min_delta - Set clock event device to the minimum delay.
211 * @dev:	device to program
212 *
213 * Returns 0 on success, -ETIME when the retry loop failed.
214 */
215static int clockevents_program_min_delta(struct clock_event_device *dev)
216{
217	unsigned long long clc;
218	int64_t delta;
219
220	delta = dev->min_delta_ns;
221	dev->next_event = ktime_add_ns(ktime_get(), delta);
222
223	if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
224		return 0;
225
226	dev->retries++;
227	clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
228	return dev->set_next_event((unsigned long) clc, dev);
229}
230
231#endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
232
233/**
234 * clockevents_program_event - Reprogram the clock event device.
235 * @dev:	device to program
236 * @expires:	absolute expiry time (monotonic clock)
237 * @force:	program minimum delay if expires can not be set
238 *
239 * Returns 0 on success, -ETIME when the event is in the past.
240 */
241int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
242			      bool force)
243{
244	unsigned long long clc;
245	int64_t delta;
246	int rc;
247
248	if (unlikely(expires.tv64 < 0)) {
249		WARN_ON_ONCE(1);
250		return -ETIME;
251	}
252
253	dev->next_event = expires;
254
255	if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
256		return 0;
257
258	/* Shortcut for clockevent devices that can deal with ktime. */
259	if (dev->features & CLOCK_EVT_FEAT_KTIME)
260		return dev->set_next_ktime(expires, dev);
261
262	delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
263	if (delta <= 0)
264		return force ? clockevents_program_min_delta(dev) : -ETIME;
265
266	delta = min(delta, (int64_t) dev->max_delta_ns);
267	delta = max(delta, (int64_t) dev->min_delta_ns);
268
269	clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
270	rc = dev->set_next_event((unsigned long) clc, dev);
271
272	return (rc && force) ? clockevents_program_min_delta(dev) : rc;
273}
274
275/*
276 * Called after a notify add to make devices available which were
277 * released from the notifier call.
278 */
279static void clockevents_notify_released(void)
280{
281	struct clock_event_device *dev;
282
283	while (!list_empty(&clockevents_released)) {
284		dev = list_entry(clockevents_released.next,
285				 struct clock_event_device, list);
286		list_del(&dev->list);
287		list_add(&dev->list, &clockevent_devices);
288		tick_check_new_device(dev);
289	}
290}
291
292/*
293 * Try to install a replacement clock event device
294 */
295static int clockevents_replace(struct clock_event_device *ced)
296{
297	struct clock_event_device *dev, *newdev = NULL;
298
299	list_for_each_entry(dev, &clockevent_devices, list) {
300		if (dev == ced || dev->mode != CLOCK_EVT_MODE_UNUSED)
301			continue;
302
303		if (!tick_check_replacement(newdev, dev))
304			continue;
305
306		if (!try_module_get(dev->owner))
307			continue;
308
309		if (newdev)
310			module_put(newdev->owner);
311		newdev = dev;
312	}
313	if (newdev) {
314		tick_install_replacement(newdev);
315		list_del_init(&ced->list);
316	}
317	return newdev ? 0 : -EBUSY;
318}
319
320/*
321 * Called with clockevents_mutex and clockevents_lock held
322 */
323static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
324{
325	/* Fast track. Device is unused */
326	if (ced->mode == CLOCK_EVT_MODE_UNUSED) {
327		list_del_init(&ced->list);
328		return 0;
329	}
330
331	return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
332}
333
334/*
335 * SMP function call to unbind a device
336 */
337static void __clockevents_unbind(void *arg)
338{
339	struct ce_unbind *cu = arg;
340	int res;
341
342	raw_spin_lock(&clockevents_lock);
343	res = __clockevents_try_unbind(cu->ce, smp_processor_id());
344	if (res == -EAGAIN)
345		res = clockevents_replace(cu->ce);
346	cu->res = res;
347	raw_spin_unlock(&clockevents_lock);
348}
349
350/*
351 * Issues smp function call to unbind a per cpu device. Called with
352 * clockevents_mutex held.
353 */
354static int clockevents_unbind(struct clock_event_device *ced, int cpu)
355{
356	struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
357
358	smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
359	return cu.res;
360}
361
362/*
363 * Unbind a clockevents device.
364 */
365int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
366{
367	int ret;
368
369	mutex_lock(&clockevents_mutex);
370	ret = clockevents_unbind(ced, cpu);
371	mutex_unlock(&clockevents_mutex);
372	return ret;
373}
374EXPORT_SYMBOL_GPL(clockevents_unbind);
375
376/**
377 * clockevents_register_device - register a clock event device
378 * @dev:	device to register
379 */
380void clockevents_register_device(struct clock_event_device *dev)
381{
382	unsigned long flags;
383
384	BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
385	if (!dev->cpumask) {
386		WARN_ON(num_possible_cpus() > 1);
387		dev->cpumask = cpumask_of(smp_processor_id());
388	}
389
390	raw_spin_lock_irqsave(&clockevents_lock, flags);
391
392	list_add(&dev->list, &clockevent_devices);
393	tick_check_new_device(dev);
394	clockevents_notify_released();
395
396	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
397}
398EXPORT_SYMBOL_GPL(clockevents_register_device);
399
400void clockevents_config(struct clock_event_device *dev, u32 freq)
401{
402	u64 sec;
403
404	if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
405		return;
406
407	/*
408	 * Calculate the maximum number of seconds we can sleep. Limit
409	 * to 10 minutes for hardware which can program more than
410	 * 32bit ticks so we still get reasonable conversion values.
411	 */
412	sec = dev->max_delta_ticks;
413	do_div(sec, freq);
414	if (!sec)
415		sec = 1;
416	else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
417		sec = 600;
418
419	clockevents_calc_mult_shift(dev, freq, sec);
420	dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
421	dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
422}
423
424/**
425 * clockevents_config_and_register - Configure and register a clock event device
426 * @dev:	device to register
427 * @freq:	The clock frequency
428 * @min_delta:	The minimum clock ticks to program in oneshot mode
429 * @max_delta:	The maximum clock ticks to program in oneshot mode
430 *
431 * min/max_delta can be 0 for devices which do not support oneshot mode.
432 */
433void clockevents_config_and_register(struct clock_event_device *dev,
434				     u32 freq, unsigned long min_delta,
435				     unsigned long max_delta)
436{
437	dev->min_delta_ticks = min_delta;
438	dev->max_delta_ticks = max_delta;
439	clockevents_config(dev, freq);
440	clockevents_register_device(dev);
441}
442EXPORT_SYMBOL_GPL(clockevents_config_and_register);
443
444int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
445{
446	clockevents_config(dev, freq);
447
448	if (dev->mode == CLOCK_EVT_MODE_ONESHOT)
449		return clockevents_program_event(dev, dev->next_event, false);
450
451	if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
452		dev->set_mode(CLOCK_EVT_MODE_PERIODIC, dev);
453
454	return 0;
455}
456
457/**
458 * clockevents_update_freq - Update frequency and reprogram a clock event device.
459 * @dev:	device to modify
460 * @freq:	new device frequency
461 *
462 * Reconfigure and reprogram a clock event device in oneshot
463 * mode. Must be called on the cpu for which the device delivers per
464 * cpu timer events. If called for the broadcast device the core takes
465 * care of serialization.
466 *
467 * Returns 0 on success, -ETIME when the event is in the past.
468 */
469int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
470{
471	unsigned long flags;
472	int ret;
473
474	local_irq_save(flags);
475	ret = tick_broadcast_update_freq(dev, freq);
476	if (ret == -ENODEV)
477		ret = __clockevents_update_freq(dev, freq);
478	local_irq_restore(flags);
479	return ret;
480}
481
482/*
483 * Noop handler when we shut down an event device
484 */
485void clockevents_handle_noop(struct clock_event_device *dev)
486{
487}
488
489/**
490 * clockevents_exchange_device - release and request clock devices
491 * @old:	device to release (can be NULL)
492 * @new:	device to request (can be NULL)
493 *
494 * Called from the notifier chain. clockevents_lock is held already
495 */
496void clockevents_exchange_device(struct clock_event_device *old,
497				 struct clock_event_device *new)
498{
499	unsigned long flags;
500
501	local_irq_save(flags);
502	/*
503	 * Caller releases a clock event device. We queue it into the
504	 * released list and do a notify add later.
505	 */
506	if (old) {
507		module_put(old->owner);
508		clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
509		list_del(&old->list);
510		list_add(&old->list, &clockevents_released);
511	}
512
513	if (new) {
514		BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
515		clockevents_shutdown(new);
516	}
517	local_irq_restore(flags);
518}
519
520/**
521 * clockevents_suspend - suspend clock devices
522 */
523void clockevents_suspend(void)
524{
525	struct clock_event_device *dev;
526
527	list_for_each_entry_reverse(dev, &clockevent_devices, list)
528		if (dev->suspend)
529			dev->suspend(dev);
530}
531
532/**
533 * clockevents_resume - resume clock devices
534 */
535void clockevents_resume(void)
536{
537	struct clock_event_device *dev;
538
539	list_for_each_entry(dev, &clockevent_devices, list)
540		if (dev->resume)
541			dev->resume(dev);
542}
543
544#ifdef CONFIG_GENERIC_CLOCKEVENTS
545/**
546 * clockevents_notify - notification about relevant events
547 * Returns 0 on success, any other value on error
548 */
549int clockevents_notify(unsigned long reason, void *arg)
550{
551	struct clock_event_device *dev, *tmp;
552	unsigned long flags;
553	int cpu, ret = 0;
554
555	raw_spin_lock_irqsave(&clockevents_lock, flags);
556
557	switch (reason) {
558	case CLOCK_EVT_NOTIFY_BROADCAST_ON:
559	case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
560	case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
561		tick_broadcast_on_off(reason, arg);
562		break;
563
564	case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
565	case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
566		ret = tick_broadcast_oneshot_control(reason);
567		break;
568
569	case CLOCK_EVT_NOTIFY_CPU_DYING:
570		tick_handover_do_timer(arg);
571		break;
572
573	case CLOCK_EVT_NOTIFY_SUSPEND:
574		tick_suspend();
575		tick_suspend_broadcast();
576		break;
577
578	case CLOCK_EVT_NOTIFY_RESUME:
579		tick_resume();
580		break;
581
582	case CLOCK_EVT_NOTIFY_CPU_DEAD:
583		tick_shutdown_broadcast_oneshot(arg);
584		tick_shutdown_broadcast(arg);
585		tick_shutdown(arg);
586		/*
587		 * Unregister the clock event devices which were
588		 * released from the users in the notify chain.
589		 */
590		list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
591			list_del(&dev->list);
592		/*
593		 * Now check whether the CPU has left unused per cpu devices
594		 */
595		cpu = *((int *)arg);
596		list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
597			if (cpumask_test_cpu(cpu, dev->cpumask) &&
598			    cpumask_weight(dev->cpumask) == 1 &&
599			    !tick_is_broadcast_device(dev)) {
600				BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
601				list_del(&dev->list);
602			}
603		}
604		break;
605	default:
606		break;
607	}
608	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
609	return ret;
610}
611EXPORT_SYMBOL_GPL(clockevents_notify);
612
613#ifdef CONFIG_SYSFS
614struct bus_type clockevents_subsys = {
615	.name		= "clockevents",
616	.dev_name       = "clockevent",
617};
618
619static DEFINE_PER_CPU(struct device, tick_percpu_dev);
620static struct tick_device *tick_get_tick_dev(struct device *dev);
621
622static ssize_t sysfs_show_current_tick_dev(struct device *dev,
623					   struct device_attribute *attr,
624					   char *buf)
625{
626	struct tick_device *td;
627	ssize_t count = 0;
628
629	raw_spin_lock_irq(&clockevents_lock);
630	td = tick_get_tick_dev(dev);
631	if (td && td->evtdev)
632		count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
633	raw_spin_unlock_irq(&clockevents_lock);
634	return count;
635}
636static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
637
638/* We don't support the abomination of removable broadcast devices */
639static ssize_t sysfs_unbind_tick_dev(struct device *dev,
640				     struct device_attribute *attr,
641				     const char *buf, size_t count)
642{
643	char name[CS_NAME_LEN];
644	ssize_t ret = sysfs_get_uname(buf, name, count);
645	struct clock_event_device *ce;
646
647	if (ret < 0)
648		return ret;
649
650	ret = -ENODEV;
651	mutex_lock(&clockevents_mutex);
652	raw_spin_lock_irq(&clockevents_lock);
653	list_for_each_entry(ce, &clockevent_devices, list) {
654		if (!strcmp(ce->name, name)) {
655			ret = __clockevents_try_unbind(ce, dev->id);
656			break;
657		}
658	}
659	raw_spin_unlock_irq(&clockevents_lock);
660	/*
661	 * We hold clockevents_mutex, so ce can't go away
662	 */
663	if (ret == -EAGAIN)
664		ret = clockevents_unbind(ce, dev->id);
665	mutex_unlock(&clockevents_mutex);
666	return ret ? ret : count;
667}
668static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
669
670#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
671static struct device tick_bc_dev = {
672	.init_name	= "broadcast",
673	.id		= 0,
674	.bus		= &clockevents_subsys,
675};
676
677static struct tick_device *tick_get_tick_dev(struct device *dev)
678{
679	return dev == &tick_bc_dev ? tick_get_broadcast_device() :
680		&per_cpu(tick_cpu_device, dev->id);
681}
682
683static __init int tick_broadcast_init_sysfs(void)
684{
685	int err = device_register(&tick_bc_dev);
686
687	if (!err)
688		err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
689	return err;
690}
691#else
692static struct tick_device *tick_get_tick_dev(struct device *dev)
693{
694	return &per_cpu(tick_cpu_device, dev->id);
695}
696static inline int tick_broadcast_init_sysfs(void) { return 0; }
697#endif
698
699static int __init tick_init_sysfs(void)
700{
701	int cpu;
702
703	for_each_possible_cpu(cpu) {
704		struct device *dev = &per_cpu(tick_percpu_dev, cpu);
705		int err;
706
707		dev->id = cpu;
708		dev->bus = &clockevents_subsys;
709		err = device_register(dev);
710		if (!err)
711			err = device_create_file(dev, &dev_attr_current_device);
712		if (!err)
713			err = device_create_file(dev, &dev_attr_unbind_device);
714		if (err)
715			return err;
716	}
717	return tick_broadcast_init_sysfs();
718}
719
720static int __init clockevents_init_sysfs(void)
721{
722	int err = subsys_system_register(&clockevents_subsys, NULL);
723
724	if (!err)
725		err = tick_init_sysfs();
726	return err;
727}
728device_initcall(clockevents_init_sysfs);
729#endif /* SYSFS */
730
731#endif /* GENERIC_CLOCK_EVENTS */
732