xref: /openbmc/linux/kernel/time/clockevents.c (revision 7d9df98b)
1d316c57fSThomas Gleixner /*
2d316c57fSThomas Gleixner  * linux/kernel/time/clockevents.c
3d316c57fSThomas Gleixner  *
4d316c57fSThomas Gleixner  * This file contains functions which manage clock event devices.
5d316c57fSThomas Gleixner  *
6d316c57fSThomas Gleixner  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7d316c57fSThomas Gleixner  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8d316c57fSThomas Gleixner  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
9d316c57fSThomas Gleixner  *
10d316c57fSThomas Gleixner  * This code is licenced under the GPL version 2. For details see
11d316c57fSThomas Gleixner  * kernel-base/COPYING.
12d316c57fSThomas Gleixner  */
13d316c57fSThomas Gleixner 
14d316c57fSThomas Gleixner #include <linux/clockchips.h>
15d316c57fSThomas Gleixner #include <linux/hrtimer.h>
16d316c57fSThomas Gleixner #include <linux/init.h>
17d316c57fSThomas Gleixner #include <linux/module.h>
18d316c57fSThomas Gleixner #include <linux/smp.h>
19501f8670SThomas Gleixner #include <linux/device.h>
20d316c57fSThomas Gleixner 
218e1a928aSH Hartley Sweeten #include "tick-internal.h"
228e1a928aSH Hartley Sweeten 
23d316c57fSThomas Gleixner /* The registered clock event devices */
24d316c57fSThomas Gleixner static LIST_HEAD(clockevent_devices);
25d316c57fSThomas Gleixner static LIST_HEAD(clockevents_released);
26d316c57fSThomas Gleixner /* Protection for the above */
27b5f91da0SThomas Gleixner static DEFINE_RAW_SPINLOCK(clockevents_lock);
2803e13cf5SThomas Gleixner /* Protection for unbind operations */
2903e13cf5SThomas Gleixner static DEFINE_MUTEX(clockevents_mutex);
3003e13cf5SThomas Gleixner 
3103e13cf5SThomas Gleixner struct ce_unbind {
3203e13cf5SThomas Gleixner 	struct clock_event_device *ce;
3303e13cf5SThomas Gleixner 	int res;
3403e13cf5SThomas Gleixner };
35d316c57fSThomas Gleixner 
3697b94106SThomas Gleixner static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
3797b94106SThomas Gleixner 			bool ismax)
3897b94106SThomas Gleixner {
3997b94106SThomas Gleixner 	u64 clc = (u64) latch << evt->shift;
4097b94106SThomas Gleixner 	u64 rnd;
4197b94106SThomas Gleixner 
427d9df98bSYangtao Li 	if (WARN_ON(!evt->mult))
4397b94106SThomas Gleixner 		evt->mult = 1;
4497b94106SThomas Gleixner 	rnd = (u64) evt->mult - 1;
4597b94106SThomas Gleixner 
4697b94106SThomas Gleixner 	/*
4797b94106SThomas Gleixner 	 * Upper bound sanity check. If the backwards conversion is
4897b94106SThomas Gleixner 	 * not equal latch, we know that the above shift overflowed.
4997b94106SThomas Gleixner 	 */
5097b94106SThomas Gleixner 	if ((clc >> evt->shift) != (u64)latch)
5197b94106SThomas Gleixner 		clc = ~0ULL;
5297b94106SThomas Gleixner 
5397b94106SThomas Gleixner 	/*
5497b94106SThomas Gleixner 	 * Scaled math oddities:
5597b94106SThomas Gleixner 	 *
5697b94106SThomas Gleixner 	 * For mult <= (1 << shift) we can safely add mult - 1 to
5797b94106SThomas Gleixner 	 * prevent integer rounding loss. So the backwards conversion
5897b94106SThomas Gleixner 	 * from nsec to device ticks will be correct.
5997b94106SThomas Gleixner 	 *
6097b94106SThomas Gleixner 	 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
6197b94106SThomas Gleixner 	 * need to be careful. Adding mult - 1 will result in a value
6297b94106SThomas Gleixner 	 * which when converted back to device ticks can be larger
6397b94106SThomas Gleixner 	 * than latch by up to (mult - 1) >> shift. For the min_delta
6497b94106SThomas Gleixner 	 * calculation we still want to apply this in order to stay
6597b94106SThomas Gleixner 	 * above the minimum device ticks limit. For the upper limit
6697b94106SThomas Gleixner 	 * we would end up with a latch value larger than the upper
6797b94106SThomas Gleixner 	 * limit of the device, so we omit the add to stay below the
6897b94106SThomas Gleixner 	 * device upper boundary.
6997b94106SThomas Gleixner 	 *
7097b94106SThomas Gleixner 	 * Also omit the add if it would overflow the u64 boundary.
7197b94106SThomas Gleixner 	 */
7297b94106SThomas Gleixner 	if ((~0ULL - clc > rnd) &&
7310632008SThomas Gleixner 	    (!ismax || evt->mult <= (1ULL << evt->shift)))
7497b94106SThomas Gleixner 		clc += rnd;
7597b94106SThomas Gleixner 
7697b94106SThomas Gleixner 	do_div(clc, evt->mult);
7797b94106SThomas Gleixner 
7897b94106SThomas Gleixner 	/* Deltas less than 1usec are pointless noise */
7997b94106SThomas Gleixner 	return clc > 1000 ? clc : 1000;
8097b94106SThomas Gleixner }
8197b94106SThomas Gleixner 
82d316c57fSThomas Gleixner /**
83d316c57fSThomas Gleixner  * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
84d316c57fSThomas Gleixner  * @latch:	value to convert
85d316c57fSThomas Gleixner  * @evt:	pointer to clock event device descriptor
86d316c57fSThomas Gleixner  *
87d316c57fSThomas Gleixner  * Math helper, returns latch value converted to nanoseconds (bound checked)
88d316c57fSThomas Gleixner  */
8997813f2fSJon Hunter u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
90d316c57fSThomas Gleixner {
9197b94106SThomas Gleixner 	return cev_delta2ns(latch, evt, false);
92d316c57fSThomas Gleixner }
93c81fc2c3SMagnus Damm EXPORT_SYMBOL_GPL(clockevent_delta2ns);
94d316c57fSThomas Gleixner 
95d7eb231cSThomas Gleixner static int __clockevents_switch_state(struct clock_event_device *dev,
9677e32c89SViresh Kumar 				      enum clock_event_state state)
97bd624d75SViresh Kumar {
98bd624d75SViresh Kumar 	if (dev->features & CLOCK_EVT_FEAT_DUMMY)
99bd624d75SViresh Kumar 		return 0;
100bd624d75SViresh Kumar 
10177e32c89SViresh Kumar 	/* Transition with new state-specific callbacks */
10277e32c89SViresh Kumar 	switch (state) {
10377e32c89SViresh Kumar 	case CLOCK_EVT_STATE_DETACHED:
104149aabccSViresh Kumar 		/* The clockevent device is getting replaced. Shut it down. */
105bd624d75SViresh Kumar 
10677e32c89SViresh Kumar 	case CLOCK_EVT_STATE_SHUTDOWN:
1077c4a976cSViresh Kumar 		if (dev->set_state_shutdown)
10877e32c89SViresh Kumar 			return dev->set_state_shutdown(dev);
1097c4a976cSViresh Kumar 		return 0;
110bd624d75SViresh Kumar 
11177e32c89SViresh Kumar 	case CLOCK_EVT_STATE_PERIODIC:
112bd624d75SViresh Kumar 		/* Core internal bug */
113bd624d75SViresh Kumar 		if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
114bd624d75SViresh Kumar 			return -ENOSYS;
1157c4a976cSViresh Kumar 		if (dev->set_state_periodic)
11677e32c89SViresh Kumar 			return dev->set_state_periodic(dev);
1177c4a976cSViresh Kumar 		return 0;
118bd624d75SViresh Kumar 
11977e32c89SViresh Kumar 	case CLOCK_EVT_STATE_ONESHOT:
120bd624d75SViresh Kumar 		/* Core internal bug */
121bd624d75SViresh Kumar 		if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
122bd624d75SViresh Kumar 			return -ENOSYS;
1237c4a976cSViresh Kumar 		if (dev->set_state_oneshot)
12477e32c89SViresh Kumar 			return dev->set_state_oneshot(dev);
1257c4a976cSViresh Kumar 		return 0;
126bd624d75SViresh Kumar 
1278fff52fdSViresh Kumar 	case CLOCK_EVT_STATE_ONESHOT_STOPPED:
1288fff52fdSViresh Kumar 		/* Core internal bug */
129472c4a94SViresh Kumar 		if (WARN_ONCE(!clockevent_state_oneshot(dev),
130051ebd10SThomas Gleixner 			      "Current state: %d\n",
131051ebd10SThomas Gleixner 			      clockevent_get_state(dev)))
1328fff52fdSViresh Kumar 			return -EINVAL;
1338fff52fdSViresh Kumar 
1348fff52fdSViresh Kumar 		if (dev->set_state_oneshot_stopped)
1358fff52fdSViresh Kumar 			return dev->set_state_oneshot_stopped(dev);
1368fff52fdSViresh Kumar 		else
1378fff52fdSViresh Kumar 			return -ENOSYS;
1388fff52fdSViresh Kumar 
139bd624d75SViresh Kumar 	default:
140bd624d75SViresh Kumar 		return -ENOSYS;
141bd624d75SViresh Kumar 	}
142bd624d75SViresh Kumar }
143bd624d75SViresh Kumar 
144d316c57fSThomas Gleixner /**
145d7eb231cSThomas Gleixner  * clockevents_switch_state - set the operating state of a clock event device
146d316c57fSThomas Gleixner  * @dev:	device to modify
14777e32c89SViresh Kumar  * @state:	new state
148d316c57fSThomas Gleixner  *
149d316c57fSThomas Gleixner  * Must be called with interrupts disabled !
150d316c57fSThomas Gleixner  */
151d7eb231cSThomas Gleixner void clockevents_switch_state(struct clock_event_device *dev,
15277e32c89SViresh Kumar 			      enum clock_event_state state)
153d316c57fSThomas Gleixner {
154051ebd10SThomas Gleixner 	if (clockevent_get_state(dev) != state) {
155d7eb231cSThomas Gleixner 		if (__clockevents_switch_state(dev, state))
156bd624d75SViresh Kumar 			return;
157bd624d75SViresh Kumar 
158051ebd10SThomas Gleixner 		clockevent_set_state(dev, state);
1592d68259dSMagnus Damm 
1602d68259dSMagnus Damm 		/*
1612d68259dSMagnus Damm 		 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
1622d68259dSMagnus Damm 		 * on it, so fix it up and emit a warning:
1632d68259dSMagnus Damm 		 */
164472c4a94SViresh Kumar 		if (clockevent_state_oneshot(dev)) {
1657d9df98bSYangtao Li 			if (WARN_ON(!dev->mult))
1662d68259dSMagnus Damm 				dev->mult = 1;
1672d68259dSMagnus Damm 		}
168d316c57fSThomas Gleixner 	}
169d316c57fSThomas Gleixner }
170d316c57fSThomas Gleixner 
171d316c57fSThomas Gleixner /**
1722344abbcSThomas Gleixner  * clockevents_shutdown - shutdown the device and clear next_event
1732344abbcSThomas Gleixner  * @dev:	device to shutdown
1742344abbcSThomas Gleixner  */
1752344abbcSThomas Gleixner void clockevents_shutdown(struct clock_event_device *dev)
1762344abbcSThomas Gleixner {
177d7eb231cSThomas Gleixner 	clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
1782456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
1792344abbcSThomas Gleixner }
1802344abbcSThomas Gleixner 
181554ef387SViresh Kumar /**
182554ef387SViresh Kumar  * clockevents_tick_resume -	Resume the tick device before using it again
183554ef387SViresh Kumar  * @dev:			device to resume
184554ef387SViresh Kumar  */
185554ef387SViresh Kumar int clockevents_tick_resume(struct clock_event_device *dev)
186554ef387SViresh Kumar {
187554ef387SViresh Kumar 	int ret = 0;
188554ef387SViresh Kumar 
189eef7635aSViresh Kumar 	if (dev->tick_resume)
19077e32c89SViresh Kumar 		ret = dev->tick_resume(dev);
191554ef387SViresh Kumar 
192554ef387SViresh Kumar 	return ret;
193554ef387SViresh Kumar }
194554ef387SViresh Kumar 
195d1748302SMartin Schwidefsky #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
196d1748302SMartin Schwidefsky 
197d1748302SMartin Schwidefsky /* Limit min_delta to a jiffie */
198d1748302SMartin Schwidefsky #define MIN_DELTA_LIMIT		(NSEC_PER_SEC / HZ)
199d1748302SMartin Schwidefsky 
200d1748302SMartin Schwidefsky /**
201d1748302SMartin Schwidefsky  * clockevents_increase_min_delta - raise minimum delta of a clock event device
202d1748302SMartin Schwidefsky  * @dev:       device to increase the minimum delta
203d1748302SMartin Schwidefsky  *
204d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the minimum delta reached the limit.
205d1748302SMartin Schwidefsky  */
206d1748302SMartin Schwidefsky static int clockevents_increase_min_delta(struct clock_event_device *dev)
207d1748302SMartin Schwidefsky {
208d1748302SMartin Schwidefsky 	/* Nothing to do if we already reached the limit */
209d1748302SMartin Schwidefsky 	if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
210504d5874SJan Kara 		printk_deferred(KERN_WARNING
211504d5874SJan Kara 				"CE: Reprogramming failure. Giving up\n");
2122456e855SThomas Gleixner 		dev->next_event = KTIME_MAX;
213d1748302SMartin Schwidefsky 		return -ETIME;
214d1748302SMartin Schwidefsky 	}
215d1748302SMartin Schwidefsky 
216d1748302SMartin Schwidefsky 	if (dev->min_delta_ns < 5000)
217d1748302SMartin Schwidefsky 		dev->min_delta_ns = 5000;
218d1748302SMartin Schwidefsky 	else
219d1748302SMartin Schwidefsky 		dev->min_delta_ns += dev->min_delta_ns >> 1;
220d1748302SMartin Schwidefsky 
221d1748302SMartin Schwidefsky 	if (dev->min_delta_ns > MIN_DELTA_LIMIT)
222d1748302SMartin Schwidefsky 		dev->min_delta_ns = MIN_DELTA_LIMIT;
223d1748302SMartin Schwidefsky 
224504d5874SJan Kara 	printk_deferred(KERN_WARNING
225504d5874SJan Kara 			"CE: %s increased min_delta_ns to %llu nsec\n",
226d1748302SMartin Schwidefsky 			dev->name ? dev->name : "?",
227d1748302SMartin Schwidefsky 			(unsigned long long) dev->min_delta_ns);
228d1748302SMartin Schwidefsky 	return 0;
229d1748302SMartin Schwidefsky }
230d1748302SMartin Schwidefsky 
231d1748302SMartin Schwidefsky /**
232d1748302SMartin Schwidefsky  * clockevents_program_min_delta - Set clock event device to the minimum delay.
233d1748302SMartin Schwidefsky  * @dev:	device to program
234d1748302SMartin Schwidefsky  *
235d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the retry loop failed.
236d1748302SMartin Schwidefsky  */
237d1748302SMartin Schwidefsky static int clockevents_program_min_delta(struct clock_event_device *dev)
238d1748302SMartin Schwidefsky {
239d1748302SMartin Schwidefsky 	unsigned long long clc;
240d1748302SMartin Schwidefsky 	int64_t delta;
241d1748302SMartin Schwidefsky 	int i;
242d1748302SMartin Schwidefsky 
243d1748302SMartin Schwidefsky 	for (i = 0;;) {
244d1748302SMartin Schwidefsky 		delta = dev->min_delta_ns;
245d1748302SMartin Schwidefsky 		dev->next_event = ktime_add_ns(ktime_get(), delta);
246d1748302SMartin Schwidefsky 
247472c4a94SViresh Kumar 		if (clockevent_state_shutdown(dev))
248d1748302SMartin Schwidefsky 			return 0;
249d1748302SMartin Schwidefsky 
250d1748302SMartin Schwidefsky 		dev->retries++;
251d1748302SMartin Schwidefsky 		clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
252d1748302SMartin Schwidefsky 		if (dev->set_next_event((unsigned long) clc, dev) == 0)
253d1748302SMartin Schwidefsky 			return 0;
254d1748302SMartin Schwidefsky 
255d1748302SMartin Schwidefsky 		if (++i > 2) {
256d1748302SMartin Schwidefsky 			/*
257d1748302SMartin Schwidefsky 			 * We tried 3 times to program the device with the
258d1748302SMartin Schwidefsky 			 * given min_delta_ns. Try to increase the minimum
259d1748302SMartin Schwidefsky 			 * delta, if that fails as well get out of here.
260d1748302SMartin Schwidefsky 			 */
261d1748302SMartin Schwidefsky 			if (clockevents_increase_min_delta(dev))
262d1748302SMartin Schwidefsky 				return -ETIME;
263d1748302SMartin Schwidefsky 			i = 0;
264d1748302SMartin Schwidefsky 		}
265d1748302SMartin Schwidefsky 	}
266d1748302SMartin Schwidefsky }
267d1748302SMartin Schwidefsky 
268d1748302SMartin Schwidefsky #else  /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
269d1748302SMartin Schwidefsky 
270d1748302SMartin Schwidefsky /**
271d1748302SMartin Schwidefsky  * clockevents_program_min_delta - Set clock event device to the minimum delay.
272d1748302SMartin Schwidefsky  * @dev:	device to program
273d1748302SMartin Schwidefsky  *
274d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the retry loop failed.
275d1748302SMartin Schwidefsky  */
276d1748302SMartin Schwidefsky static int clockevents_program_min_delta(struct clock_event_device *dev)
277d1748302SMartin Schwidefsky {
278d1748302SMartin Schwidefsky 	unsigned long long clc;
2793a29ddb1SJames Hogan 	int64_t delta = 0;
2803a29ddb1SJames Hogan 	int i;
281d1748302SMartin Schwidefsky 
2823a29ddb1SJames Hogan 	for (i = 0; i < 10; i++) {
2833a29ddb1SJames Hogan 		delta += dev->min_delta_ns;
284d1748302SMartin Schwidefsky 		dev->next_event = ktime_add_ns(ktime_get(), delta);
285d1748302SMartin Schwidefsky 
286472c4a94SViresh Kumar 		if (clockevent_state_shutdown(dev))
287d1748302SMartin Schwidefsky 			return 0;
288d1748302SMartin Schwidefsky 
289d1748302SMartin Schwidefsky 		dev->retries++;
290d1748302SMartin Schwidefsky 		clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
2913a29ddb1SJames Hogan 		if (dev->set_next_event((unsigned long) clc, dev) == 0)
2923a29ddb1SJames Hogan 			return 0;
2933a29ddb1SJames Hogan 	}
2943a29ddb1SJames Hogan 	return -ETIME;
295d1748302SMartin Schwidefsky }
296d1748302SMartin Schwidefsky 
297d1748302SMartin Schwidefsky #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
298d1748302SMartin Schwidefsky 
2992344abbcSThomas Gleixner /**
300d316c57fSThomas Gleixner  * clockevents_program_event - Reprogram the clock event device.
301d1748302SMartin Schwidefsky  * @dev:	device to program
302d316c57fSThomas Gleixner  * @expires:	absolute expiry time (monotonic clock)
303d1748302SMartin Schwidefsky  * @force:	program minimum delay if expires can not be set
304d316c57fSThomas Gleixner  *
305d316c57fSThomas Gleixner  * Returns 0 on success, -ETIME when the event is in the past.
306d316c57fSThomas Gleixner  */
307d316c57fSThomas Gleixner int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
308d1748302SMartin Schwidefsky 			      bool force)
309d316c57fSThomas Gleixner {
310d316c57fSThomas Gleixner 	unsigned long long clc;
311d316c57fSThomas Gleixner 	int64_t delta;
312d1748302SMartin Schwidefsky 	int rc;
313d316c57fSThomas Gleixner 
3147d9df98bSYangtao Li 	if (WARN_ON_ONCE(expires < 0))
315167b1de3SThomas Gleixner 		return -ETIME;
316167b1de3SThomas Gleixner 
317d316c57fSThomas Gleixner 	dev->next_event = expires;
318d316c57fSThomas Gleixner 
319472c4a94SViresh Kumar 	if (clockevent_state_shutdown(dev))
320d316c57fSThomas Gleixner 		return 0;
321d316c57fSThomas Gleixner 
322d2540875SViresh Kumar 	/* We must be in ONESHOT state here */
323472c4a94SViresh Kumar 	WARN_ONCE(!clockevent_state_oneshot(dev), "Current state: %d\n",
324051ebd10SThomas Gleixner 		  clockevent_get_state(dev));
325d2540875SViresh Kumar 
32665516f8aSMartin Schwidefsky 	/* Shortcut for clockevent devices that can deal with ktime. */
32765516f8aSMartin Schwidefsky 	if (dev->features & CLOCK_EVT_FEAT_KTIME)
32865516f8aSMartin Schwidefsky 		return dev->set_next_ktime(expires, dev);
32965516f8aSMartin Schwidefsky 
330d1748302SMartin Schwidefsky 	delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
331d1748302SMartin Schwidefsky 	if (delta <= 0)
332d1748302SMartin Schwidefsky 		return force ? clockevents_program_min_delta(dev) : -ETIME;
333d316c57fSThomas Gleixner 
334d1748302SMartin Schwidefsky 	delta = min(delta, (int64_t) dev->max_delta_ns);
335d1748302SMartin Schwidefsky 	delta = max(delta, (int64_t) dev->min_delta_ns);
336d316c57fSThomas Gleixner 
337d1748302SMartin Schwidefsky 	clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
338d1748302SMartin Schwidefsky 	rc = dev->set_next_event((unsigned long) clc, dev);
339d1748302SMartin Schwidefsky 
340d1748302SMartin Schwidefsky 	return (rc && force) ? clockevents_program_min_delta(dev) : rc;
341d316c57fSThomas Gleixner }
342d316c57fSThomas Gleixner 
343d316c57fSThomas Gleixner /*
3443eb05676SLi Zefan  * Called after a notify add to make devices available which were
345d316c57fSThomas Gleixner  * released from the notifier call.
346d316c57fSThomas Gleixner  */
347d316c57fSThomas Gleixner static void clockevents_notify_released(void)
348d316c57fSThomas Gleixner {
349d316c57fSThomas Gleixner 	struct clock_event_device *dev;
350d316c57fSThomas Gleixner 
351d316c57fSThomas Gleixner 	while (!list_empty(&clockevents_released)) {
352d316c57fSThomas Gleixner 		dev = list_entry(clockevents_released.next,
353d316c57fSThomas Gleixner 				 struct clock_event_device, list);
354d316c57fSThomas Gleixner 		list_del(&dev->list);
355d316c57fSThomas Gleixner 		list_add(&dev->list, &clockevent_devices);
3567172a286SThomas Gleixner 		tick_check_new_device(dev);
357d316c57fSThomas Gleixner 	}
358d316c57fSThomas Gleixner }
359d316c57fSThomas Gleixner 
36003e13cf5SThomas Gleixner /*
36103e13cf5SThomas Gleixner  * Try to install a replacement clock event device
36203e13cf5SThomas Gleixner  */
36303e13cf5SThomas Gleixner static int clockevents_replace(struct clock_event_device *ced)
36403e13cf5SThomas Gleixner {
36503e13cf5SThomas Gleixner 	struct clock_event_device *dev, *newdev = NULL;
36603e13cf5SThomas Gleixner 
36703e13cf5SThomas Gleixner 	list_for_each_entry(dev, &clockevent_devices, list) {
368472c4a94SViresh Kumar 		if (dev == ced || !clockevent_state_detached(dev))
36903e13cf5SThomas Gleixner 			continue;
37003e13cf5SThomas Gleixner 
37103e13cf5SThomas Gleixner 		if (!tick_check_replacement(newdev, dev))
37203e13cf5SThomas Gleixner 			continue;
37303e13cf5SThomas Gleixner 
37403e13cf5SThomas Gleixner 		if (!try_module_get(dev->owner))
37503e13cf5SThomas Gleixner 			continue;
37603e13cf5SThomas Gleixner 
37703e13cf5SThomas Gleixner 		if (newdev)
37803e13cf5SThomas Gleixner 			module_put(newdev->owner);
37903e13cf5SThomas Gleixner 		newdev = dev;
38003e13cf5SThomas Gleixner 	}
38103e13cf5SThomas Gleixner 	if (newdev) {
38203e13cf5SThomas Gleixner 		tick_install_replacement(newdev);
38303e13cf5SThomas Gleixner 		list_del_init(&ced->list);
38403e13cf5SThomas Gleixner 	}
38503e13cf5SThomas Gleixner 	return newdev ? 0 : -EBUSY;
38603e13cf5SThomas Gleixner }
38703e13cf5SThomas Gleixner 
38803e13cf5SThomas Gleixner /*
38903e13cf5SThomas Gleixner  * Called with clockevents_mutex and clockevents_lock held
39003e13cf5SThomas Gleixner  */
39103e13cf5SThomas Gleixner static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
39203e13cf5SThomas Gleixner {
39303e13cf5SThomas Gleixner 	/* Fast track. Device is unused */
394472c4a94SViresh Kumar 	if (clockevent_state_detached(ced)) {
39503e13cf5SThomas Gleixner 		list_del_init(&ced->list);
39603e13cf5SThomas Gleixner 		return 0;
39703e13cf5SThomas Gleixner 	}
39803e13cf5SThomas Gleixner 
39903e13cf5SThomas Gleixner 	return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
40003e13cf5SThomas Gleixner }
40103e13cf5SThomas Gleixner 
40203e13cf5SThomas Gleixner /*
40303e13cf5SThomas Gleixner  * SMP function call to unbind a device
40403e13cf5SThomas Gleixner  */
40503e13cf5SThomas Gleixner static void __clockevents_unbind(void *arg)
40603e13cf5SThomas Gleixner {
40703e13cf5SThomas Gleixner 	struct ce_unbind *cu = arg;
40803e13cf5SThomas Gleixner 	int res;
40903e13cf5SThomas Gleixner 
41003e13cf5SThomas Gleixner 	raw_spin_lock(&clockevents_lock);
41103e13cf5SThomas Gleixner 	res = __clockevents_try_unbind(cu->ce, smp_processor_id());
41203e13cf5SThomas Gleixner 	if (res == -EAGAIN)
41303e13cf5SThomas Gleixner 		res = clockevents_replace(cu->ce);
41403e13cf5SThomas Gleixner 	cu->res = res;
41503e13cf5SThomas Gleixner 	raw_spin_unlock(&clockevents_lock);
41603e13cf5SThomas Gleixner }
41703e13cf5SThomas Gleixner 
41803e13cf5SThomas Gleixner /*
41903e13cf5SThomas Gleixner  * Issues smp function call to unbind a per cpu device. Called with
42003e13cf5SThomas Gleixner  * clockevents_mutex held.
42103e13cf5SThomas Gleixner  */
42203e13cf5SThomas Gleixner static int clockevents_unbind(struct clock_event_device *ced, int cpu)
42303e13cf5SThomas Gleixner {
42403e13cf5SThomas Gleixner 	struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
42503e13cf5SThomas Gleixner 
42603e13cf5SThomas Gleixner 	smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
42703e13cf5SThomas Gleixner 	return cu.res;
42803e13cf5SThomas Gleixner }
42903e13cf5SThomas Gleixner 
43003e13cf5SThomas Gleixner /*
43103e13cf5SThomas Gleixner  * Unbind a clockevents device.
43203e13cf5SThomas Gleixner  */
43303e13cf5SThomas Gleixner int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
43403e13cf5SThomas Gleixner {
43503e13cf5SThomas Gleixner 	int ret;
43603e13cf5SThomas Gleixner 
43703e13cf5SThomas Gleixner 	mutex_lock(&clockevents_mutex);
43803e13cf5SThomas Gleixner 	ret = clockevents_unbind(ced, cpu);
43903e13cf5SThomas Gleixner 	mutex_unlock(&clockevents_mutex);
44003e13cf5SThomas Gleixner 	return ret;
44103e13cf5SThomas Gleixner }
44232a15832SVitaly Kuznetsov EXPORT_SYMBOL_GPL(clockevents_unbind_device);
44303e13cf5SThomas Gleixner 
444d316c57fSThomas Gleixner /**
445d316c57fSThomas Gleixner  * clockevents_register_device - register a clock event device
446d316c57fSThomas Gleixner  * @dev:	device to register
447d316c57fSThomas Gleixner  */
448d316c57fSThomas Gleixner void clockevents_register_device(struct clock_event_device *dev)
449d316c57fSThomas Gleixner {
450f833bab8SSuresh Siddha 	unsigned long flags;
451f833bab8SSuresh Siddha 
45277e32c89SViresh Kumar 	/* Initialize state to DETACHED */
453051ebd10SThomas Gleixner 	clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
45477e32c89SViresh Kumar 
4551b054b67SThomas Gleixner 	if (!dev->cpumask) {
4561b054b67SThomas Gleixner 		WARN_ON(num_possible_cpus() > 1);
4571b054b67SThomas Gleixner 		dev->cpumask = cpumask_of(smp_processor_id());
4581b054b67SThomas Gleixner 	}
459320ab2b0SRusty Russell 
460fbfa9260SSudeep Holla 	if (dev->cpumask == cpu_all_mask) {
461fbfa9260SSudeep Holla 		WARN(1, "%s cpumask == cpu_all_mask, using cpu_possible_mask instead\n",
462fbfa9260SSudeep Holla 		     dev->name);
463fbfa9260SSudeep Holla 		dev->cpumask = cpu_possible_mask;
464fbfa9260SSudeep Holla 	}
465fbfa9260SSudeep Holla 
466b5f91da0SThomas Gleixner 	raw_spin_lock_irqsave(&clockevents_lock, flags);
467d316c57fSThomas Gleixner 
468d316c57fSThomas Gleixner 	list_add(&dev->list, &clockevent_devices);
4697172a286SThomas Gleixner 	tick_check_new_device(dev);
470d316c57fSThomas Gleixner 	clockevents_notify_released();
471d316c57fSThomas Gleixner 
472b5f91da0SThomas Gleixner 	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
473d316c57fSThomas Gleixner }
474c81fc2c3SMagnus Damm EXPORT_SYMBOL_GPL(clockevents_register_device);
475d316c57fSThomas Gleixner 
4760695bd99SNicolai Stange static void clockevents_config(struct clock_event_device *dev, u32 freq)
47757f0fcbeSThomas Gleixner {
478c0e299b1SThomas Gleixner 	u64 sec;
47957f0fcbeSThomas Gleixner 
48057f0fcbeSThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
48157f0fcbeSThomas Gleixner 		return;
48257f0fcbeSThomas Gleixner 
48357f0fcbeSThomas Gleixner 	/*
48457f0fcbeSThomas Gleixner 	 * Calculate the maximum number of seconds we can sleep. Limit
48557f0fcbeSThomas Gleixner 	 * to 10 minutes for hardware which can program more than
48657f0fcbeSThomas Gleixner 	 * 32bit ticks so we still get reasonable conversion values.
48757f0fcbeSThomas Gleixner 	 */
48857f0fcbeSThomas Gleixner 	sec = dev->max_delta_ticks;
48957f0fcbeSThomas Gleixner 	do_div(sec, freq);
49057f0fcbeSThomas Gleixner 	if (!sec)
49157f0fcbeSThomas Gleixner 		sec = 1;
49257f0fcbeSThomas Gleixner 	else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
49357f0fcbeSThomas Gleixner 		sec = 600;
49457f0fcbeSThomas Gleixner 
49557f0fcbeSThomas Gleixner 	clockevents_calc_mult_shift(dev, freq, sec);
49697b94106SThomas Gleixner 	dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
49797b94106SThomas Gleixner 	dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
49857f0fcbeSThomas Gleixner }
49957f0fcbeSThomas Gleixner 
50057f0fcbeSThomas Gleixner /**
50157f0fcbeSThomas Gleixner  * clockevents_config_and_register - Configure and register a clock event device
50257f0fcbeSThomas Gleixner  * @dev:	device to register
50357f0fcbeSThomas Gleixner  * @freq:	The clock frequency
50457f0fcbeSThomas Gleixner  * @min_delta:	The minimum clock ticks to program in oneshot mode
50557f0fcbeSThomas Gleixner  * @max_delta:	The maximum clock ticks to program in oneshot mode
50657f0fcbeSThomas Gleixner  *
50757f0fcbeSThomas Gleixner  * min/max_delta can be 0 for devices which do not support oneshot mode.
50857f0fcbeSThomas Gleixner  */
50957f0fcbeSThomas Gleixner void clockevents_config_and_register(struct clock_event_device *dev,
51057f0fcbeSThomas Gleixner 				     u32 freq, unsigned long min_delta,
51157f0fcbeSThomas Gleixner 				     unsigned long max_delta)
51257f0fcbeSThomas Gleixner {
51357f0fcbeSThomas Gleixner 	dev->min_delta_ticks = min_delta;
51457f0fcbeSThomas Gleixner 	dev->max_delta_ticks = max_delta;
51557f0fcbeSThomas Gleixner 	clockevents_config(dev, freq);
51657f0fcbeSThomas Gleixner 	clockevents_register_device(dev);
51757f0fcbeSThomas Gleixner }
518c35ef95cSShawn Guo EXPORT_SYMBOL_GPL(clockevents_config_and_register);
51957f0fcbeSThomas Gleixner 
520627ee794SThomas Gleixner int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
52180b816b7SThomas Gleixner {
52280b816b7SThomas Gleixner 	clockevents_config(dev, freq);
52380b816b7SThomas Gleixner 
524472c4a94SViresh Kumar 	if (clockevent_state_oneshot(dev))
525d1748302SMartin Schwidefsky 		return clockevents_program_event(dev, dev->next_event, false);
526fe79a9baSSoren Brinkmann 
527472c4a94SViresh Kumar 	if (clockevent_state_periodic(dev))
528d7eb231cSThomas Gleixner 		return __clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
529fe79a9baSSoren Brinkmann 
530fe79a9baSSoren Brinkmann 	return 0;
53180b816b7SThomas Gleixner }
53280b816b7SThomas Gleixner 
533627ee794SThomas Gleixner /**
534627ee794SThomas Gleixner  * clockevents_update_freq - Update frequency and reprogram a clock event device.
535627ee794SThomas Gleixner  * @dev:	device to modify
536627ee794SThomas Gleixner  * @freq:	new device frequency
537627ee794SThomas Gleixner  *
538627ee794SThomas Gleixner  * Reconfigure and reprogram a clock event device in oneshot
539627ee794SThomas Gleixner  * mode. Must be called on the cpu for which the device delivers per
540627ee794SThomas Gleixner  * cpu timer events. If called for the broadcast device the core takes
541627ee794SThomas Gleixner  * care of serialization.
542627ee794SThomas Gleixner  *
543627ee794SThomas Gleixner  * Returns 0 on success, -ETIME when the event is in the past.
544627ee794SThomas Gleixner  */
545627ee794SThomas Gleixner int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
546627ee794SThomas Gleixner {
547627ee794SThomas Gleixner 	unsigned long flags;
548627ee794SThomas Gleixner 	int ret;
549627ee794SThomas Gleixner 
550627ee794SThomas Gleixner 	local_irq_save(flags);
551627ee794SThomas Gleixner 	ret = tick_broadcast_update_freq(dev, freq);
552627ee794SThomas Gleixner 	if (ret == -ENODEV)
553627ee794SThomas Gleixner 		ret = __clockevents_update_freq(dev, freq);
554627ee794SThomas Gleixner 	local_irq_restore(flags);
555627ee794SThomas Gleixner 	return ret;
556627ee794SThomas Gleixner }
557627ee794SThomas Gleixner 
558d316c57fSThomas Gleixner /*
559d316c57fSThomas Gleixner  * Noop handler when we shut down an event device
560d316c57fSThomas Gleixner  */
5617c1e7689SVenkatesh Pallipadi void clockevents_handle_noop(struct clock_event_device *dev)
562d316c57fSThomas Gleixner {
563d316c57fSThomas Gleixner }
564d316c57fSThomas Gleixner 
565d316c57fSThomas Gleixner /**
566d316c57fSThomas Gleixner  * clockevents_exchange_device - release and request clock devices
567d316c57fSThomas Gleixner  * @old:	device to release (can be NULL)
568d316c57fSThomas Gleixner  * @new:	device to request (can be NULL)
569d316c57fSThomas Gleixner  *
570db6f672eSThomas Gleixner  * Called from various tick functions with clockevents_lock held and
571db6f672eSThomas Gleixner  * interrupts disabled.
572d316c57fSThomas Gleixner  */
573d316c57fSThomas Gleixner void clockevents_exchange_device(struct clock_event_device *old,
574d316c57fSThomas Gleixner 				 struct clock_event_device *new)
575d316c57fSThomas Gleixner {
576d316c57fSThomas Gleixner 	/*
577d316c57fSThomas Gleixner 	 * Caller releases a clock event device. We queue it into the
578d316c57fSThomas Gleixner 	 * released list and do a notify add later.
579d316c57fSThomas Gleixner 	 */
580d316c57fSThomas Gleixner 	if (old) {
581ccf33d68SThomas Gleixner 		module_put(old->owner);
582d7eb231cSThomas Gleixner 		clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED);
583d316c57fSThomas Gleixner 		list_del(&old->list);
584d316c57fSThomas Gleixner 		list_add(&old->list, &clockevents_released);
585d316c57fSThomas Gleixner 	}
586d316c57fSThomas Gleixner 
587d316c57fSThomas Gleixner 	if (new) {
588472c4a94SViresh Kumar 		BUG_ON(!clockevent_state_detached(new));
5892344abbcSThomas Gleixner 		clockevents_shutdown(new);
590d316c57fSThomas Gleixner 	}
591d316c57fSThomas Gleixner }
592d316c57fSThomas Gleixner 
593adc78e6bSRafael J. Wysocki /**
594adc78e6bSRafael J. Wysocki  * clockevents_suspend - suspend clock devices
595adc78e6bSRafael J. Wysocki  */
596adc78e6bSRafael J. Wysocki void clockevents_suspend(void)
597adc78e6bSRafael J. Wysocki {
598adc78e6bSRafael J. Wysocki 	struct clock_event_device *dev;
599adc78e6bSRafael J. Wysocki 
600adc78e6bSRafael J. Wysocki 	list_for_each_entry_reverse(dev, &clockevent_devices, list)
601a9d20988SViresh Kumar 		if (dev->suspend && !clockevent_state_detached(dev))
602adc78e6bSRafael J. Wysocki 			dev->suspend(dev);
603adc78e6bSRafael J. Wysocki }
604adc78e6bSRafael J. Wysocki 
605adc78e6bSRafael J. Wysocki /**
606adc78e6bSRafael J. Wysocki  * clockevents_resume - resume clock devices
607adc78e6bSRafael J. Wysocki  */
608adc78e6bSRafael J. Wysocki void clockevents_resume(void)
609adc78e6bSRafael J. Wysocki {
610adc78e6bSRafael J. Wysocki 	struct clock_event_device *dev;
611adc78e6bSRafael J. Wysocki 
612adc78e6bSRafael J. Wysocki 	list_for_each_entry(dev, &clockevent_devices, list)
613a9d20988SViresh Kumar 		if (dev->resume && !clockevent_state_detached(dev))
614adc78e6bSRafael J. Wysocki 			dev->resume(dev);
615adc78e6bSRafael J. Wysocki }
616adc78e6bSRafael J. Wysocki 
617a49b116dSThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
618d316c57fSThomas Gleixner /**
619a49b116dSThomas Gleixner  * tick_cleanup_dead_cpu - Cleanup the tick and clockevents of a dead cpu
620d316c57fSThomas Gleixner  */
621a49b116dSThomas Gleixner void tick_cleanup_dead_cpu(int cpu)
622d316c57fSThomas Gleixner {
623bb6eddf7SThomas Gleixner 	struct clock_event_device *dev, *tmp;
624f833bab8SSuresh Siddha 	unsigned long flags;
6250b858e6fSLi Zefan 
626b5f91da0SThomas Gleixner 	raw_spin_lock_irqsave(&clockevents_lock, flags);
627d316c57fSThomas Gleixner 
628a49b116dSThomas Gleixner 	tick_shutdown_broadcast_oneshot(cpu);
629a49b116dSThomas Gleixner 	tick_shutdown_broadcast(cpu);
630a49b116dSThomas Gleixner 	tick_shutdown(cpu);
631d316c57fSThomas Gleixner 	/*
632d316c57fSThomas Gleixner 	 * Unregister the clock event devices which were
633d316c57fSThomas Gleixner 	 * released from the users in the notify chain.
634d316c57fSThomas Gleixner 	 */
635bb6eddf7SThomas Gleixner 	list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
636bb6eddf7SThomas Gleixner 		list_del(&dev->list);
637bb6eddf7SThomas Gleixner 	/*
638bb6eddf7SThomas Gleixner 	 * Now check whether the CPU has left unused per cpu devices
639bb6eddf7SThomas Gleixner 	 */
640bb6eddf7SThomas Gleixner 	list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
641bb6eddf7SThomas Gleixner 		if (cpumask_test_cpu(cpu, dev->cpumask) &&
642ea9d8e3fSXiaotian Feng 		    cpumask_weight(dev->cpumask) == 1 &&
643ea9d8e3fSXiaotian Feng 		    !tick_is_broadcast_device(dev)) {
644472c4a94SViresh Kumar 			BUG_ON(!clockevent_state_detached(dev));
645bb6eddf7SThomas Gleixner 			list_del(&dev->list);
646bb6eddf7SThomas Gleixner 		}
647bb6eddf7SThomas Gleixner 	}
648b5f91da0SThomas Gleixner 	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
649d316c57fSThomas Gleixner }
650a49b116dSThomas Gleixner #endif
651501f8670SThomas Gleixner 
652501f8670SThomas Gleixner #ifdef CONFIG_SYSFS
653775be506SBen Dooks static struct bus_type clockevents_subsys = {
654501f8670SThomas Gleixner 	.name		= "clockevents",
655501f8670SThomas Gleixner 	.dev_name       = "clockevent",
656501f8670SThomas Gleixner };
657501f8670SThomas Gleixner 
658501f8670SThomas Gleixner static DEFINE_PER_CPU(struct device, tick_percpu_dev);
659501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev);
660501f8670SThomas Gleixner 
661501f8670SThomas Gleixner static ssize_t sysfs_show_current_tick_dev(struct device *dev,
662501f8670SThomas Gleixner 					   struct device_attribute *attr,
663501f8670SThomas Gleixner 					   char *buf)
664501f8670SThomas Gleixner {
665501f8670SThomas Gleixner 	struct tick_device *td;
666501f8670SThomas Gleixner 	ssize_t count = 0;
667501f8670SThomas Gleixner 
668501f8670SThomas Gleixner 	raw_spin_lock_irq(&clockevents_lock);
669501f8670SThomas Gleixner 	td = tick_get_tick_dev(dev);
670501f8670SThomas Gleixner 	if (td && td->evtdev)
671501f8670SThomas Gleixner 		count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
672501f8670SThomas Gleixner 	raw_spin_unlock_irq(&clockevents_lock);
673501f8670SThomas Gleixner 	return count;
674501f8670SThomas Gleixner }
675501f8670SThomas Gleixner static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
676501f8670SThomas Gleixner 
67703e13cf5SThomas Gleixner /* We don't support the abomination of removable broadcast devices */
67803e13cf5SThomas Gleixner static ssize_t sysfs_unbind_tick_dev(struct device *dev,
67903e13cf5SThomas Gleixner 				     struct device_attribute *attr,
68003e13cf5SThomas Gleixner 				     const char *buf, size_t count)
68103e13cf5SThomas Gleixner {
68203e13cf5SThomas Gleixner 	char name[CS_NAME_LEN];
683891292a7SPatrick Palka 	ssize_t ret = sysfs_get_uname(buf, name, count);
68403e13cf5SThomas Gleixner 	struct clock_event_device *ce;
68503e13cf5SThomas Gleixner 
68603e13cf5SThomas Gleixner 	if (ret < 0)
68703e13cf5SThomas Gleixner 		return ret;
68803e13cf5SThomas Gleixner 
68903e13cf5SThomas Gleixner 	ret = -ENODEV;
69003e13cf5SThomas Gleixner 	mutex_lock(&clockevents_mutex);
69103e13cf5SThomas Gleixner 	raw_spin_lock_irq(&clockevents_lock);
69203e13cf5SThomas Gleixner 	list_for_each_entry(ce, &clockevent_devices, list) {
69303e13cf5SThomas Gleixner 		if (!strcmp(ce->name, name)) {
69403e13cf5SThomas Gleixner 			ret = __clockevents_try_unbind(ce, dev->id);
69503e13cf5SThomas Gleixner 			break;
69603e13cf5SThomas Gleixner 		}
69703e13cf5SThomas Gleixner 	}
69803e13cf5SThomas Gleixner 	raw_spin_unlock_irq(&clockevents_lock);
69903e13cf5SThomas Gleixner 	/*
70003e13cf5SThomas Gleixner 	 * We hold clockevents_mutex, so ce can't go away
70103e13cf5SThomas Gleixner 	 */
70203e13cf5SThomas Gleixner 	if (ret == -EAGAIN)
70303e13cf5SThomas Gleixner 		ret = clockevents_unbind(ce, dev->id);
70403e13cf5SThomas Gleixner 	mutex_unlock(&clockevents_mutex);
70503e13cf5SThomas Gleixner 	return ret ? ret : count;
70603e13cf5SThomas Gleixner }
70703e13cf5SThomas Gleixner static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
70803e13cf5SThomas Gleixner 
709501f8670SThomas Gleixner #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
710501f8670SThomas Gleixner static struct device tick_bc_dev = {
711501f8670SThomas Gleixner 	.init_name	= "broadcast",
712501f8670SThomas Gleixner 	.id		= 0,
713501f8670SThomas Gleixner 	.bus		= &clockevents_subsys,
714501f8670SThomas Gleixner };
715501f8670SThomas Gleixner 
716501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev)
717501f8670SThomas Gleixner {
718501f8670SThomas Gleixner 	return dev == &tick_bc_dev ? tick_get_broadcast_device() :
719501f8670SThomas Gleixner 		&per_cpu(tick_cpu_device, dev->id);
720501f8670SThomas Gleixner }
721501f8670SThomas Gleixner 
722501f8670SThomas Gleixner static __init int tick_broadcast_init_sysfs(void)
723501f8670SThomas Gleixner {
724501f8670SThomas Gleixner 	int err = device_register(&tick_bc_dev);
725501f8670SThomas Gleixner 
726501f8670SThomas Gleixner 	if (!err)
727501f8670SThomas Gleixner 		err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
728501f8670SThomas Gleixner 	return err;
729501f8670SThomas Gleixner }
730501f8670SThomas Gleixner #else
731501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev)
732501f8670SThomas Gleixner {
733501f8670SThomas Gleixner 	return &per_cpu(tick_cpu_device, dev->id);
734501f8670SThomas Gleixner }
735501f8670SThomas Gleixner static inline int tick_broadcast_init_sysfs(void) { return 0; }
736de68d9b1SThomas Gleixner #endif
737501f8670SThomas Gleixner 
738501f8670SThomas Gleixner static int __init tick_init_sysfs(void)
739501f8670SThomas Gleixner {
740501f8670SThomas Gleixner 	int cpu;
741501f8670SThomas Gleixner 
742501f8670SThomas Gleixner 	for_each_possible_cpu(cpu) {
743501f8670SThomas Gleixner 		struct device *dev = &per_cpu(tick_percpu_dev, cpu);
744501f8670SThomas Gleixner 		int err;
745501f8670SThomas Gleixner 
746501f8670SThomas Gleixner 		dev->id = cpu;
747501f8670SThomas Gleixner 		dev->bus = &clockevents_subsys;
748501f8670SThomas Gleixner 		err = device_register(dev);
749501f8670SThomas Gleixner 		if (!err)
750501f8670SThomas Gleixner 			err = device_create_file(dev, &dev_attr_current_device);
75103e13cf5SThomas Gleixner 		if (!err)
75203e13cf5SThomas Gleixner 			err = device_create_file(dev, &dev_attr_unbind_device);
753501f8670SThomas Gleixner 		if (err)
754501f8670SThomas Gleixner 			return err;
755501f8670SThomas Gleixner 	}
756501f8670SThomas Gleixner 	return tick_broadcast_init_sysfs();
757501f8670SThomas Gleixner }
758501f8670SThomas Gleixner 
759501f8670SThomas Gleixner static int __init clockevents_init_sysfs(void)
760501f8670SThomas Gleixner {
761501f8670SThomas Gleixner 	int err = subsys_system_register(&clockevents_subsys, NULL);
762501f8670SThomas Gleixner 
763501f8670SThomas Gleixner 	if (!err)
764501f8670SThomas Gleixner 		err = tick_init_sysfs();
765501f8670SThomas Gleixner 	return err;
766501f8670SThomas Gleixner }
767501f8670SThomas Gleixner device_initcall(clockevents_init_sysfs);
768501f8670SThomas Gleixner #endif /* SYSFS */
769