xref: /openbmc/linux/kernel/time/clockevents.c (revision 2966a991)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2d316c57fSThomas Gleixner /*
3d316c57fSThomas Gleixner  * This file contains functions which manage clock event devices.
4d316c57fSThomas Gleixner  *
5d316c57fSThomas Gleixner  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
6d316c57fSThomas Gleixner  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
7d316c57fSThomas Gleixner  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
8d316c57fSThomas Gleixner  */
9d316c57fSThomas Gleixner 
10d316c57fSThomas Gleixner #include <linux/clockchips.h>
11d316c57fSThomas Gleixner #include <linux/hrtimer.h>
12d316c57fSThomas Gleixner #include <linux/init.h>
13d316c57fSThomas Gleixner #include <linux/module.h>
14d316c57fSThomas Gleixner #include <linux/smp.h>
15501f8670SThomas Gleixner #include <linux/device.h>
16d316c57fSThomas Gleixner 
178e1a928aSH Hartley Sweeten #include "tick-internal.h"
188e1a928aSH Hartley Sweeten 
19d316c57fSThomas Gleixner /* The registered clock event devices */
20d316c57fSThomas Gleixner static LIST_HEAD(clockevent_devices);
21d316c57fSThomas Gleixner static LIST_HEAD(clockevents_released);
22d316c57fSThomas Gleixner /* Protection for the above */
23b5f91da0SThomas Gleixner static DEFINE_RAW_SPINLOCK(clockevents_lock);
2403e13cf5SThomas Gleixner /* Protection for unbind operations */
2503e13cf5SThomas Gleixner static DEFINE_MUTEX(clockevents_mutex);
2603e13cf5SThomas Gleixner 
2703e13cf5SThomas Gleixner struct ce_unbind {
2803e13cf5SThomas Gleixner 	struct clock_event_device *ce;
2903e13cf5SThomas Gleixner 	int res;
3003e13cf5SThomas Gleixner };
31d316c57fSThomas Gleixner 
3297b94106SThomas Gleixner static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
3397b94106SThomas Gleixner 			bool ismax)
3497b94106SThomas Gleixner {
3597b94106SThomas Gleixner 	u64 clc = (u64) latch << evt->shift;
3697b94106SThomas Gleixner 	u64 rnd;
3797b94106SThomas Gleixner 
387d9df98bSYangtao Li 	if (WARN_ON(!evt->mult))
3997b94106SThomas Gleixner 		evt->mult = 1;
4097b94106SThomas Gleixner 	rnd = (u64) evt->mult - 1;
4197b94106SThomas Gleixner 
4297b94106SThomas Gleixner 	/*
4397b94106SThomas Gleixner 	 * Upper bound sanity check. If the backwards conversion is
4497b94106SThomas Gleixner 	 * not equal latch, we know that the above shift overflowed.
4597b94106SThomas Gleixner 	 */
4697b94106SThomas Gleixner 	if ((clc >> evt->shift) != (u64)latch)
4797b94106SThomas Gleixner 		clc = ~0ULL;
4897b94106SThomas Gleixner 
4997b94106SThomas Gleixner 	/*
5097b94106SThomas Gleixner 	 * Scaled math oddities:
5197b94106SThomas Gleixner 	 *
5297b94106SThomas Gleixner 	 * For mult <= (1 << shift) we can safely add mult - 1 to
5397b94106SThomas Gleixner 	 * prevent integer rounding loss. So the backwards conversion
5497b94106SThomas Gleixner 	 * from nsec to device ticks will be correct.
5597b94106SThomas Gleixner 	 *
5697b94106SThomas Gleixner 	 * For mult > (1 << shift), i.e. device frequency is > 1GHz we
5797b94106SThomas Gleixner 	 * need to be careful. Adding mult - 1 will result in a value
5897b94106SThomas Gleixner 	 * which when converted back to device ticks can be larger
5997b94106SThomas Gleixner 	 * than latch by up to (mult - 1) >> shift. For the min_delta
6097b94106SThomas Gleixner 	 * calculation we still want to apply this in order to stay
6197b94106SThomas Gleixner 	 * above the minimum device ticks limit. For the upper limit
6297b94106SThomas Gleixner 	 * we would end up with a latch value larger than the upper
6397b94106SThomas Gleixner 	 * limit of the device, so we omit the add to stay below the
6497b94106SThomas Gleixner 	 * device upper boundary.
6597b94106SThomas Gleixner 	 *
6697b94106SThomas Gleixner 	 * Also omit the add if it would overflow the u64 boundary.
6797b94106SThomas Gleixner 	 */
6897b94106SThomas Gleixner 	if ((~0ULL - clc > rnd) &&
6910632008SThomas Gleixner 	    (!ismax || evt->mult <= (1ULL << evt->shift)))
7097b94106SThomas Gleixner 		clc += rnd;
7197b94106SThomas Gleixner 
7297b94106SThomas Gleixner 	do_div(clc, evt->mult);
7397b94106SThomas Gleixner 
7497b94106SThomas Gleixner 	/* Deltas less than 1usec are pointless noise */
7597b94106SThomas Gleixner 	return clc > 1000 ? clc : 1000;
7697b94106SThomas Gleixner }
7797b94106SThomas Gleixner 
78d316c57fSThomas Gleixner /**
79d316c57fSThomas Gleixner  * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
80d316c57fSThomas Gleixner  * @latch:	value to convert
81d316c57fSThomas Gleixner  * @evt:	pointer to clock event device descriptor
82d316c57fSThomas Gleixner  *
83d316c57fSThomas Gleixner  * Math helper, returns latch value converted to nanoseconds (bound checked)
84d316c57fSThomas Gleixner  */
8597813f2fSJon Hunter u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
86d316c57fSThomas Gleixner {
8797b94106SThomas Gleixner 	return cev_delta2ns(latch, evt, false);
88d316c57fSThomas Gleixner }
89c81fc2c3SMagnus Damm EXPORT_SYMBOL_GPL(clockevent_delta2ns);
90d316c57fSThomas Gleixner 
91d7eb231cSThomas Gleixner static int __clockevents_switch_state(struct clock_event_device *dev,
9277e32c89SViresh Kumar 				      enum clock_event_state state)
93bd624d75SViresh Kumar {
94bd624d75SViresh Kumar 	if (dev->features & CLOCK_EVT_FEAT_DUMMY)
95bd624d75SViresh Kumar 		return 0;
96bd624d75SViresh Kumar 
9777e32c89SViresh Kumar 	/* Transition with new state-specific callbacks */
9877e32c89SViresh Kumar 	switch (state) {
9977e32c89SViresh Kumar 	case CLOCK_EVT_STATE_DETACHED:
100149aabccSViresh Kumar 		/* The clockevent device is getting replaced. Shut it down. */
101bd624d75SViresh Kumar 
10277e32c89SViresh Kumar 	case CLOCK_EVT_STATE_SHUTDOWN:
1037c4a976cSViresh Kumar 		if (dev->set_state_shutdown)
10477e32c89SViresh Kumar 			return dev->set_state_shutdown(dev);
1057c4a976cSViresh Kumar 		return 0;
106bd624d75SViresh Kumar 
10777e32c89SViresh Kumar 	case CLOCK_EVT_STATE_PERIODIC:
108bd624d75SViresh Kumar 		/* Core internal bug */
109bd624d75SViresh Kumar 		if (!(dev->features & CLOCK_EVT_FEAT_PERIODIC))
110bd624d75SViresh Kumar 			return -ENOSYS;
1117c4a976cSViresh Kumar 		if (dev->set_state_periodic)
11277e32c89SViresh Kumar 			return dev->set_state_periodic(dev);
1137c4a976cSViresh Kumar 		return 0;
114bd624d75SViresh Kumar 
11577e32c89SViresh Kumar 	case CLOCK_EVT_STATE_ONESHOT:
116bd624d75SViresh Kumar 		/* Core internal bug */
117bd624d75SViresh Kumar 		if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
118bd624d75SViresh Kumar 			return -ENOSYS;
1197c4a976cSViresh Kumar 		if (dev->set_state_oneshot)
12077e32c89SViresh Kumar 			return dev->set_state_oneshot(dev);
1217c4a976cSViresh Kumar 		return 0;
122bd624d75SViresh Kumar 
1238fff52fdSViresh Kumar 	case CLOCK_EVT_STATE_ONESHOT_STOPPED:
1248fff52fdSViresh Kumar 		/* Core internal bug */
125472c4a94SViresh Kumar 		if (WARN_ONCE(!clockevent_state_oneshot(dev),
126051ebd10SThomas Gleixner 			      "Current state: %d\n",
127051ebd10SThomas Gleixner 			      clockevent_get_state(dev)))
1288fff52fdSViresh Kumar 			return -EINVAL;
1298fff52fdSViresh Kumar 
1308fff52fdSViresh Kumar 		if (dev->set_state_oneshot_stopped)
1318fff52fdSViresh Kumar 			return dev->set_state_oneshot_stopped(dev);
1328fff52fdSViresh Kumar 		else
1338fff52fdSViresh Kumar 			return -ENOSYS;
1348fff52fdSViresh Kumar 
135bd624d75SViresh Kumar 	default:
136bd624d75SViresh Kumar 		return -ENOSYS;
137bd624d75SViresh Kumar 	}
138bd624d75SViresh Kumar }
139bd624d75SViresh Kumar 
140d316c57fSThomas Gleixner /**
141d7eb231cSThomas Gleixner  * clockevents_switch_state - set the operating state of a clock event device
142d316c57fSThomas Gleixner  * @dev:	device to modify
14377e32c89SViresh Kumar  * @state:	new state
144d316c57fSThomas Gleixner  *
145d316c57fSThomas Gleixner  * Must be called with interrupts disabled !
146d316c57fSThomas Gleixner  */
147d7eb231cSThomas Gleixner void clockevents_switch_state(struct clock_event_device *dev,
14877e32c89SViresh Kumar 			      enum clock_event_state state)
149d316c57fSThomas Gleixner {
150051ebd10SThomas Gleixner 	if (clockevent_get_state(dev) != state) {
151d7eb231cSThomas Gleixner 		if (__clockevents_switch_state(dev, state))
152bd624d75SViresh Kumar 			return;
153bd624d75SViresh Kumar 
154051ebd10SThomas Gleixner 		clockevent_set_state(dev, state);
1552d68259dSMagnus Damm 
1562d68259dSMagnus Damm 		/*
1572d68259dSMagnus Damm 		 * A nsec2cyc multiplicator of 0 is invalid and we'd crash
1582d68259dSMagnus Damm 		 * on it, so fix it up and emit a warning:
1592d68259dSMagnus Damm 		 */
160472c4a94SViresh Kumar 		if (clockevent_state_oneshot(dev)) {
1617d9df98bSYangtao Li 			if (WARN_ON(!dev->mult))
1622d68259dSMagnus Damm 				dev->mult = 1;
1632d68259dSMagnus Damm 		}
164d316c57fSThomas Gleixner 	}
165d316c57fSThomas Gleixner }
166d316c57fSThomas Gleixner 
167d316c57fSThomas Gleixner /**
1682344abbcSThomas Gleixner  * clockevents_shutdown - shutdown the device and clear next_event
1692344abbcSThomas Gleixner  * @dev:	device to shutdown
1702344abbcSThomas Gleixner  */
1712344abbcSThomas Gleixner void clockevents_shutdown(struct clock_event_device *dev)
1722344abbcSThomas Gleixner {
173d7eb231cSThomas Gleixner 	clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
1742456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
1752344abbcSThomas Gleixner }
1762344abbcSThomas Gleixner 
177554ef387SViresh Kumar /**
178554ef387SViresh Kumar  * clockevents_tick_resume -	Resume the tick device before using it again
179554ef387SViresh Kumar  * @dev:			device to resume
180554ef387SViresh Kumar  */
181554ef387SViresh Kumar int clockevents_tick_resume(struct clock_event_device *dev)
182554ef387SViresh Kumar {
183554ef387SViresh Kumar 	int ret = 0;
184554ef387SViresh Kumar 
185eef7635aSViresh Kumar 	if (dev->tick_resume)
18677e32c89SViresh Kumar 		ret = dev->tick_resume(dev);
187554ef387SViresh Kumar 
188554ef387SViresh Kumar 	return ret;
189554ef387SViresh Kumar }
190554ef387SViresh Kumar 
191d1748302SMartin Schwidefsky #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
192d1748302SMartin Schwidefsky 
193d1748302SMartin Schwidefsky /* Limit min_delta to a jiffie */
194d1748302SMartin Schwidefsky #define MIN_DELTA_LIMIT		(NSEC_PER_SEC / HZ)
195d1748302SMartin Schwidefsky 
196d1748302SMartin Schwidefsky /**
197d1748302SMartin Schwidefsky  * clockevents_increase_min_delta - raise minimum delta of a clock event device
198d1748302SMartin Schwidefsky  * @dev:       device to increase the minimum delta
199d1748302SMartin Schwidefsky  *
200d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the minimum delta reached the limit.
201d1748302SMartin Schwidefsky  */
202d1748302SMartin Schwidefsky static int clockevents_increase_min_delta(struct clock_event_device *dev)
203d1748302SMartin Schwidefsky {
204d1748302SMartin Schwidefsky 	/* Nothing to do if we already reached the limit */
205d1748302SMartin Schwidefsky 	if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
206504d5874SJan Kara 		printk_deferred(KERN_WARNING
207504d5874SJan Kara 				"CE: Reprogramming failure. Giving up\n");
2082456e855SThomas Gleixner 		dev->next_event = KTIME_MAX;
209d1748302SMartin Schwidefsky 		return -ETIME;
210d1748302SMartin Schwidefsky 	}
211d1748302SMartin Schwidefsky 
212d1748302SMartin Schwidefsky 	if (dev->min_delta_ns < 5000)
213d1748302SMartin Schwidefsky 		dev->min_delta_ns = 5000;
214d1748302SMartin Schwidefsky 	else
215d1748302SMartin Schwidefsky 		dev->min_delta_ns += dev->min_delta_ns >> 1;
216d1748302SMartin Schwidefsky 
217d1748302SMartin Schwidefsky 	if (dev->min_delta_ns > MIN_DELTA_LIMIT)
218d1748302SMartin Schwidefsky 		dev->min_delta_ns = MIN_DELTA_LIMIT;
219d1748302SMartin Schwidefsky 
220504d5874SJan Kara 	printk_deferred(KERN_WARNING
221504d5874SJan Kara 			"CE: %s increased min_delta_ns to %llu nsec\n",
222d1748302SMartin Schwidefsky 			dev->name ? dev->name : "?",
223d1748302SMartin Schwidefsky 			(unsigned long long) dev->min_delta_ns);
224d1748302SMartin Schwidefsky 	return 0;
225d1748302SMartin Schwidefsky }
226d1748302SMartin Schwidefsky 
227d1748302SMartin Schwidefsky /**
228d1748302SMartin Schwidefsky  * clockevents_program_min_delta - Set clock event device to the minimum delay.
229d1748302SMartin Schwidefsky  * @dev:	device to program
230d1748302SMartin Schwidefsky  *
231d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the retry loop failed.
232d1748302SMartin Schwidefsky  */
233d1748302SMartin Schwidefsky static int clockevents_program_min_delta(struct clock_event_device *dev)
234d1748302SMartin Schwidefsky {
235d1748302SMartin Schwidefsky 	unsigned long long clc;
236d1748302SMartin Schwidefsky 	int64_t delta;
237d1748302SMartin Schwidefsky 	int i;
238d1748302SMartin Schwidefsky 
239d1748302SMartin Schwidefsky 	for (i = 0;;) {
240d1748302SMartin Schwidefsky 		delta = dev->min_delta_ns;
241d1748302SMartin Schwidefsky 		dev->next_event = ktime_add_ns(ktime_get(), delta);
242d1748302SMartin Schwidefsky 
243472c4a94SViresh Kumar 		if (clockevent_state_shutdown(dev))
244d1748302SMartin Schwidefsky 			return 0;
245d1748302SMartin Schwidefsky 
246d1748302SMartin Schwidefsky 		dev->retries++;
247d1748302SMartin Schwidefsky 		clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
248d1748302SMartin Schwidefsky 		if (dev->set_next_event((unsigned long) clc, dev) == 0)
249d1748302SMartin Schwidefsky 			return 0;
250d1748302SMartin Schwidefsky 
251d1748302SMartin Schwidefsky 		if (++i > 2) {
252d1748302SMartin Schwidefsky 			/*
253d1748302SMartin Schwidefsky 			 * We tried 3 times to program the device with the
254d1748302SMartin Schwidefsky 			 * given min_delta_ns. Try to increase the minimum
255d1748302SMartin Schwidefsky 			 * delta, if that fails as well get out of here.
256d1748302SMartin Schwidefsky 			 */
257d1748302SMartin Schwidefsky 			if (clockevents_increase_min_delta(dev))
258d1748302SMartin Schwidefsky 				return -ETIME;
259d1748302SMartin Schwidefsky 			i = 0;
260d1748302SMartin Schwidefsky 		}
261d1748302SMartin Schwidefsky 	}
262d1748302SMartin Schwidefsky }
263d1748302SMartin Schwidefsky 
264d1748302SMartin Schwidefsky #else  /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
265d1748302SMartin Schwidefsky 
266d1748302SMartin Schwidefsky /**
267d1748302SMartin Schwidefsky  * clockevents_program_min_delta - Set clock event device to the minimum delay.
268d1748302SMartin Schwidefsky  * @dev:	device to program
269d1748302SMartin Schwidefsky  *
270d1748302SMartin Schwidefsky  * Returns 0 on success, -ETIME when the retry loop failed.
271d1748302SMartin Schwidefsky  */
272d1748302SMartin Schwidefsky static int clockevents_program_min_delta(struct clock_event_device *dev)
273d1748302SMartin Schwidefsky {
274d1748302SMartin Schwidefsky 	unsigned long long clc;
2753a29ddb1SJames Hogan 	int64_t delta = 0;
2763a29ddb1SJames Hogan 	int i;
277d1748302SMartin Schwidefsky 
2783a29ddb1SJames Hogan 	for (i = 0; i < 10; i++) {
2793a29ddb1SJames Hogan 		delta += dev->min_delta_ns;
280d1748302SMartin Schwidefsky 		dev->next_event = ktime_add_ns(ktime_get(), delta);
281d1748302SMartin Schwidefsky 
282472c4a94SViresh Kumar 		if (clockevent_state_shutdown(dev))
283d1748302SMartin Schwidefsky 			return 0;
284d1748302SMartin Schwidefsky 
285d1748302SMartin Schwidefsky 		dev->retries++;
286d1748302SMartin Schwidefsky 		clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
2873a29ddb1SJames Hogan 		if (dev->set_next_event((unsigned long) clc, dev) == 0)
2883a29ddb1SJames Hogan 			return 0;
2893a29ddb1SJames Hogan 	}
2903a29ddb1SJames Hogan 	return -ETIME;
291d1748302SMartin Schwidefsky }
292d1748302SMartin Schwidefsky 
293d1748302SMartin Schwidefsky #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
294d1748302SMartin Schwidefsky 
2952344abbcSThomas Gleixner /**
296d316c57fSThomas Gleixner  * clockevents_program_event - Reprogram the clock event device.
297d1748302SMartin Schwidefsky  * @dev:	device to program
298d316c57fSThomas Gleixner  * @expires:	absolute expiry time (monotonic clock)
299d1748302SMartin Schwidefsky  * @force:	program minimum delay if expires can not be set
300d316c57fSThomas Gleixner  *
301d316c57fSThomas Gleixner  * Returns 0 on success, -ETIME when the event is in the past.
302d316c57fSThomas Gleixner  */
303d316c57fSThomas Gleixner int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
304d1748302SMartin Schwidefsky 			      bool force)
305d316c57fSThomas Gleixner {
306d316c57fSThomas Gleixner 	unsigned long long clc;
307d316c57fSThomas Gleixner 	int64_t delta;
308d1748302SMartin Schwidefsky 	int rc;
309d316c57fSThomas Gleixner 
3107d9df98bSYangtao Li 	if (WARN_ON_ONCE(expires < 0))
311167b1de3SThomas Gleixner 		return -ETIME;
312167b1de3SThomas Gleixner 
313d316c57fSThomas Gleixner 	dev->next_event = expires;
314d316c57fSThomas Gleixner 
315472c4a94SViresh Kumar 	if (clockevent_state_shutdown(dev))
316d316c57fSThomas Gleixner 		return 0;
317d316c57fSThomas Gleixner 
318d2540875SViresh Kumar 	/* We must be in ONESHOT state here */
319472c4a94SViresh Kumar 	WARN_ONCE(!clockevent_state_oneshot(dev), "Current state: %d\n",
320051ebd10SThomas Gleixner 		  clockevent_get_state(dev));
321d2540875SViresh Kumar 
32265516f8aSMartin Schwidefsky 	/* Shortcut for clockevent devices that can deal with ktime. */
32365516f8aSMartin Schwidefsky 	if (dev->features & CLOCK_EVT_FEAT_KTIME)
32465516f8aSMartin Schwidefsky 		return dev->set_next_ktime(expires, dev);
32565516f8aSMartin Schwidefsky 
326d1748302SMartin Schwidefsky 	delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
327d1748302SMartin Schwidefsky 	if (delta <= 0)
328d1748302SMartin Schwidefsky 		return force ? clockevents_program_min_delta(dev) : -ETIME;
329d316c57fSThomas Gleixner 
330d1748302SMartin Schwidefsky 	delta = min(delta, (int64_t) dev->max_delta_ns);
331d1748302SMartin Schwidefsky 	delta = max(delta, (int64_t) dev->min_delta_ns);
332d316c57fSThomas Gleixner 
333d1748302SMartin Schwidefsky 	clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
334d1748302SMartin Schwidefsky 	rc = dev->set_next_event((unsigned long) clc, dev);
335d1748302SMartin Schwidefsky 
336d1748302SMartin Schwidefsky 	return (rc && force) ? clockevents_program_min_delta(dev) : rc;
337d316c57fSThomas Gleixner }
338d316c57fSThomas Gleixner 
339d316c57fSThomas Gleixner /*
3403eb05676SLi Zefan  * Called after a notify add to make devices available which were
341d316c57fSThomas Gleixner  * released from the notifier call.
342d316c57fSThomas Gleixner  */
343d316c57fSThomas Gleixner static void clockevents_notify_released(void)
344d316c57fSThomas Gleixner {
345d316c57fSThomas Gleixner 	struct clock_event_device *dev;
346d316c57fSThomas Gleixner 
347d316c57fSThomas Gleixner 	while (!list_empty(&clockevents_released)) {
348d316c57fSThomas Gleixner 		dev = list_entry(clockevents_released.next,
349d316c57fSThomas Gleixner 				 struct clock_event_device, list);
3504e82d2e2SBaokun Li 		list_move(&dev->list, &clockevent_devices);
3517172a286SThomas Gleixner 		tick_check_new_device(dev);
352d316c57fSThomas Gleixner 	}
353d316c57fSThomas Gleixner }
354d316c57fSThomas Gleixner 
35503e13cf5SThomas Gleixner /*
35603e13cf5SThomas Gleixner  * Try to install a replacement clock event device
35703e13cf5SThomas Gleixner  */
35803e13cf5SThomas Gleixner static int clockevents_replace(struct clock_event_device *ced)
35903e13cf5SThomas Gleixner {
36003e13cf5SThomas Gleixner 	struct clock_event_device *dev, *newdev = NULL;
36103e13cf5SThomas Gleixner 
36203e13cf5SThomas Gleixner 	list_for_each_entry(dev, &clockevent_devices, list) {
363472c4a94SViresh Kumar 		if (dev == ced || !clockevent_state_detached(dev))
36403e13cf5SThomas Gleixner 			continue;
36503e13cf5SThomas Gleixner 
36603e13cf5SThomas Gleixner 		if (!tick_check_replacement(newdev, dev))
36703e13cf5SThomas Gleixner 			continue;
36803e13cf5SThomas Gleixner 
36903e13cf5SThomas Gleixner 		if (!try_module_get(dev->owner))
37003e13cf5SThomas Gleixner 			continue;
37103e13cf5SThomas Gleixner 
37203e13cf5SThomas Gleixner 		if (newdev)
37303e13cf5SThomas Gleixner 			module_put(newdev->owner);
37403e13cf5SThomas Gleixner 		newdev = dev;
37503e13cf5SThomas Gleixner 	}
37603e13cf5SThomas Gleixner 	if (newdev) {
37703e13cf5SThomas Gleixner 		tick_install_replacement(newdev);
37803e13cf5SThomas Gleixner 		list_del_init(&ced->list);
37903e13cf5SThomas Gleixner 	}
38003e13cf5SThomas Gleixner 	return newdev ? 0 : -EBUSY;
38103e13cf5SThomas Gleixner }
38203e13cf5SThomas Gleixner 
38303e13cf5SThomas Gleixner /*
38403e13cf5SThomas Gleixner  * Called with clockevents_mutex and clockevents_lock held
38503e13cf5SThomas Gleixner  */
38603e13cf5SThomas Gleixner static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
38703e13cf5SThomas Gleixner {
38803e13cf5SThomas Gleixner 	/* Fast track. Device is unused */
389472c4a94SViresh Kumar 	if (clockevent_state_detached(ced)) {
39003e13cf5SThomas Gleixner 		list_del_init(&ced->list);
39103e13cf5SThomas Gleixner 		return 0;
39203e13cf5SThomas Gleixner 	}
39303e13cf5SThomas Gleixner 
39403e13cf5SThomas Gleixner 	return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
39503e13cf5SThomas Gleixner }
39603e13cf5SThomas Gleixner 
39703e13cf5SThomas Gleixner /*
39803e13cf5SThomas Gleixner  * SMP function call to unbind a device
39903e13cf5SThomas Gleixner  */
40003e13cf5SThomas Gleixner static void __clockevents_unbind(void *arg)
40103e13cf5SThomas Gleixner {
40203e13cf5SThomas Gleixner 	struct ce_unbind *cu = arg;
40303e13cf5SThomas Gleixner 	int res;
40403e13cf5SThomas Gleixner 
40503e13cf5SThomas Gleixner 	raw_spin_lock(&clockevents_lock);
40603e13cf5SThomas Gleixner 	res = __clockevents_try_unbind(cu->ce, smp_processor_id());
40703e13cf5SThomas Gleixner 	if (res == -EAGAIN)
40803e13cf5SThomas Gleixner 		res = clockevents_replace(cu->ce);
40903e13cf5SThomas Gleixner 	cu->res = res;
41003e13cf5SThomas Gleixner 	raw_spin_unlock(&clockevents_lock);
41103e13cf5SThomas Gleixner }
41203e13cf5SThomas Gleixner 
41303e13cf5SThomas Gleixner /*
41403e13cf5SThomas Gleixner  * Issues smp function call to unbind a per cpu device. Called with
41503e13cf5SThomas Gleixner  * clockevents_mutex held.
41603e13cf5SThomas Gleixner  */
41703e13cf5SThomas Gleixner static int clockevents_unbind(struct clock_event_device *ced, int cpu)
41803e13cf5SThomas Gleixner {
41903e13cf5SThomas Gleixner 	struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
42003e13cf5SThomas Gleixner 
42103e13cf5SThomas Gleixner 	smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
42203e13cf5SThomas Gleixner 	return cu.res;
42303e13cf5SThomas Gleixner }
42403e13cf5SThomas Gleixner 
42503e13cf5SThomas Gleixner /*
42603e13cf5SThomas Gleixner  * Unbind a clockevents device.
42703e13cf5SThomas Gleixner  */
42803e13cf5SThomas Gleixner int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
42903e13cf5SThomas Gleixner {
43003e13cf5SThomas Gleixner 	int ret;
43103e13cf5SThomas Gleixner 
43203e13cf5SThomas Gleixner 	mutex_lock(&clockevents_mutex);
43303e13cf5SThomas Gleixner 	ret = clockevents_unbind(ced, cpu);
43403e13cf5SThomas Gleixner 	mutex_unlock(&clockevents_mutex);
43503e13cf5SThomas Gleixner 	return ret;
43603e13cf5SThomas Gleixner }
43732a15832SVitaly Kuznetsov EXPORT_SYMBOL_GPL(clockevents_unbind_device);
43803e13cf5SThomas Gleixner 
439d316c57fSThomas Gleixner /**
440d316c57fSThomas Gleixner  * clockevents_register_device - register a clock event device
441d316c57fSThomas Gleixner  * @dev:	device to register
442d316c57fSThomas Gleixner  */
443d316c57fSThomas Gleixner void clockevents_register_device(struct clock_event_device *dev)
444d316c57fSThomas Gleixner {
445f833bab8SSuresh Siddha 	unsigned long flags;
446f833bab8SSuresh Siddha 
44777e32c89SViresh Kumar 	/* Initialize state to DETACHED */
448051ebd10SThomas Gleixner 	clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
44977e32c89SViresh Kumar 
4501b054b67SThomas Gleixner 	if (!dev->cpumask) {
4511b054b67SThomas Gleixner 		WARN_ON(num_possible_cpus() > 1);
4521b054b67SThomas Gleixner 		dev->cpumask = cpumask_of(smp_processor_id());
4531b054b67SThomas Gleixner 	}
454320ab2b0SRusty Russell 
455fbfa9260SSudeep Holla 	if (dev->cpumask == cpu_all_mask) {
456fbfa9260SSudeep Holla 		WARN(1, "%s cpumask == cpu_all_mask, using cpu_possible_mask instead\n",
457fbfa9260SSudeep Holla 		     dev->name);
458fbfa9260SSudeep Holla 		dev->cpumask = cpu_possible_mask;
459fbfa9260SSudeep Holla 	}
460fbfa9260SSudeep Holla 
461b5f91da0SThomas Gleixner 	raw_spin_lock_irqsave(&clockevents_lock, flags);
462d316c57fSThomas Gleixner 
463d316c57fSThomas Gleixner 	list_add(&dev->list, &clockevent_devices);
4647172a286SThomas Gleixner 	tick_check_new_device(dev);
465d316c57fSThomas Gleixner 	clockevents_notify_released();
466d316c57fSThomas Gleixner 
467b5f91da0SThomas Gleixner 	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
468d316c57fSThomas Gleixner }
469c81fc2c3SMagnus Damm EXPORT_SYMBOL_GPL(clockevents_register_device);
470d316c57fSThomas Gleixner 
4710695bd99SNicolai Stange static void clockevents_config(struct clock_event_device *dev, u32 freq)
47257f0fcbeSThomas Gleixner {
473c0e299b1SThomas Gleixner 	u64 sec;
47457f0fcbeSThomas Gleixner 
47557f0fcbeSThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
47657f0fcbeSThomas Gleixner 		return;
47757f0fcbeSThomas Gleixner 
47857f0fcbeSThomas Gleixner 	/*
47957f0fcbeSThomas Gleixner 	 * Calculate the maximum number of seconds we can sleep. Limit
48057f0fcbeSThomas Gleixner 	 * to 10 minutes for hardware which can program more than
48157f0fcbeSThomas Gleixner 	 * 32bit ticks so we still get reasonable conversion values.
48257f0fcbeSThomas Gleixner 	 */
48357f0fcbeSThomas Gleixner 	sec = dev->max_delta_ticks;
48457f0fcbeSThomas Gleixner 	do_div(sec, freq);
48557f0fcbeSThomas Gleixner 	if (!sec)
48657f0fcbeSThomas Gleixner 		sec = 1;
48757f0fcbeSThomas Gleixner 	else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
48857f0fcbeSThomas Gleixner 		sec = 600;
48957f0fcbeSThomas Gleixner 
49057f0fcbeSThomas Gleixner 	clockevents_calc_mult_shift(dev, freq, sec);
49197b94106SThomas Gleixner 	dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
49297b94106SThomas Gleixner 	dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
49357f0fcbeSThomas Gleixner }
49457f0fcbeSThomas Gleixner 
49557f0fcbeSThomas Gleixner /**
49657f0fcbeSThomas Gleixner  * clockevents_config_and_register - Configure and register a clock event device
49757f0fcbeSThomas Gleixner  * @dev:	device to register
49857f0fcbeSThomas Gleixner  * @freq:	The clock frequency
49957f0fcbeSThomas Gleixner  * @min_delta:	The minimum clock ticks to program in oneshot mode
50057f0fcbeSThomas Gleixner  * @max_delta:	The maximum clock ticks to program in oneshot mode
50157f0fcbeSThomas Gleixner  *
50257f0fcbeSThomas Gleixner  * min/max_delta can be 0 for devices which do not support oneshot mode.
50357f0fcbeSThomas Gleixner  */
50457f0fcbeSThomas Gleixner void clockevents_config_and_register(struct clock_event_device *dev,
50557f0fcbeSThomas Gleixner 				     u32 freq, unsigned long min_delta,
50657f0fcbeSThomas Gleixner 				     unsigned long max_delta)
50757f0fcbeSThomas Gleixner {
50857f0fcbeSThomas Gleixner 	dev->min_delta_ticks = min_delta;
50957f0fcbeSThomas Gleixner 	dev->max_delta_ticks = max_delta;
51057f0fcbeSThomas Gleixner 	clockevents_config(dev, freq);
51157f0fcbeSThomas Gleixner 	clockevents_register_device(dev);
51257f0fcbeSThomas Gleixner }
513c35ef95cSShawn Guo EXPORT_SYMBOL_GPL(clockevents_config_and_register);
51457f0fcbeSThomas Gleixner 
515627ee794SThomas Gleixner int __clockevents_update_freq(struct clock_event_device *dev, u32 freq)
51680b816b7SThomas Gleixner {
51780b816b7SThomas Gleixner 	clockevents_config(dev, freq);
51880b816b7SThomas Gleixner 
519472c4a94SViresh Kumar 	if (clockevent_state_oneshot(dev))
520d1748302SMartin Schwidefsky 		return clockevents_program_event(dev, dev->next_event, false);
521fe79a9baSSoren Brinkmann 
522472c4a94SViresh Kumar 	if (clockevent_state_periodic(dev))
523d7eb231cSThomas Gleixner 		return __clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
524fe79a9baSSoren Brinkmann 
525fe79a9baSSoren Brinkmann 	return 0;
52680b816b7SThomas Gleixner }
52780b816b7SThomas Gleixner 
528627ee794SThomas Gleixner /**
529627ee794SThomas Gleixner  * clockevents_update_freq - Update frequency and reprogram a clock event device.
530627ee794SThomas Gleixner  * @dev:	device to modify
531627ee794SThomas Gleixner  * @freq:	new device frequency
532627ee794SThomas Gleixner  *
533627ee794SThomas Gleixner  * Reconfigure and reprogram a clock event device in oneshot
534627ee794SThomas Gleixner  * mode. Must be called on the cpu for which the device delivers per
535627ee794SThomas Gleixner  * cpu timer events. If called for the broadcast device the core takes
536627ee794SThomas Gleixner  * care of serialization.
537627ee794SThomas Gleixner  *
538627ee794SThomas Gleixner  * Returns 0 on success, -ETIME when the event is in the past.
539627ee794SThomas Gleixner  */
540627ee794SThomas Gleixner int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
541627ee794SThomas Gleixner {
542627ee794SThomas Gleixner 	unsigned long flags;
543627ee794SThomas Gleixner 	int ret;
544627ee794SThomas Gleixner 
545627ee794SThomas Gleixner 	local_irq_save(flags);
546627ee794SThomas Gleixner 	ret = tick_broadcast_update_freq(dev, freq);
547627ee794SThomas Gleixner 	if (ret == -ENODEV)
548627ee794SThomas Gleixner 		ret = __clockevents_update_freq(dev, freq);
549627ee794SThomas Gleixner 	local_irq_restore(flags);
550627ee794SThomas Gleixner 	return ret;
551627ee794SThomas Gleixner }
552627ee794SThomas Gleixner 
553d316c57fSThomas Gleixner /*
554d316c57fSThomas Gleixner  * Noop handler when we shut down an event device
555d316c57fSThomas Gleixner  */
5567c1e7689SVenkatesh Pallipadi void clockevents_handle_noop(struct clock_event_device *dev)
557d316c57fSThomas Gleixner {
558d316c57fSThomas Gleixner }
559d316c57fSThomas Gleixner 
560d316c57fSThomas Gleixner /**
561d316c57fSThomas Gleixner  * clockevents_exchange_device - release and request clock devices
562d316c57fSThomas Gleixner  * @old:	device to release (can be NULL)
563d316c57fSThomas Gleixner  * @new:	device to request (can be NULL)
564d316c57fSThomas Gleixner  *
565db6f672eSThomas Gleixner  * Called from various tick functions with clockevents_lock held and
566db6f672eSThomas Gleixner  * interrupts disabled.
567d316c57fSThomas Gleixner  */
568d316c57fSThomas Gleixner void clockevents_exchange_device(struct clock_event_device *old,
569d316c57fSThomas Gleixner 				 struct clock_event_device *new)
570d316c57fSThomas Gleixner {
571d316c57fSThomas Gleixner 	/*
572d316c57fSThomas Gleixner 	 * Caller releases a clock event device. We queue it into the
573d316c57fSThomas Gleixner 	 * released list and do a notify add later.
574d316c57fSThomas Gleixner 	 */
575d316c57fSThomas Gleixner 	if (old) {
576ccf33d68SThomas Gleixner 		module_put(old->owner);
577d7eb231cSThomas Gleixner 		clockevents_switch_state(old, CLOCK_EVT_STATE_DETACHED);
5784e82d2e2SBaokun Li 		list_move(&old->list, &clockevents_released);
579d316c57fSThomas Gleixner 	}
580d316c57fSThomas Gleixner 
581d316c57fSThomas Gleixner 	if (new) {
582472c4a94SViresh Kumar 		BUG_ON(!clockevent_state_detached(new));
5832344abbcSThomas Gleixner 		clockevents_shutdown(new);
584d316c57fSThomas Gleixner 	}
585d316c57fSThomas Gleixner }
586d316c57fSThomas Gleixner 
587adc78e6bSRafael J. Wysocki /**
588adc78e6bSRafael J. Wysocki  * clockevents_suspend - suspend clock devices
589adc78e6bSRafael J. Wysocki  */
590adc78e6bSRafael J. Wysocki void clockevents_suspend(void)
591adc78e6bSRafael J. Wysocki {
592adc78e6bSRafael J. Wysocki 	struct clock_event_device *dev;
593adc78e6bSRafael J. Wysocki 
594adc78e6bSRafael J. Wysocki 	list_for_each_entry_reverse(dev, &clockevent_devices, list)
595a9d20988SViresh Kumar 		if (dev->suspend && !clockevent_state_detached(dev))
596adc78e6bSRafael J. Wysocki 			dev->suspend(dev);
597adc78e6bSRafael J. Wysocki }
598adc78e6bSRafael J. Wysocki 
599adc78e6bSRafael J. Wysocki /**
600adc78e6bSRafael J. Wysocki  * clockevents_resume - resume clock devices
601adc78e6bSRafael J. Wysocki  */
602adc78e6bSRafael J. Wysocki void clockevents_resume(void)
603adc78e6bSRafael J. Wysocki {
604adc78e6bSRafael J. Wysocki 	struct clock_event_device *dev;
605adc78e6bSRafael J. Wysocki 
606adc78e6bSRafael J. Wysocki 	list_for_each_entry(dev, &clockevent_devices, list)
607a9d20988SViresh Kumar 		if (dev->resume && !clockevent_state_detached(dev))
608adc78e6bSRafael J. Wysocki 			dev->resume(dev);
609adc78e6bSRafael J. Wysocki }
610adc78e6bSRafael J. Wysocki 
611a49b116dSThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
6121b72d432SThomas Gleixner 
6131b72d432SThomas Gleixner # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
6141b72d432SThomas Gleixner /**
6151b72d432SThomas Gleixner  * tick_offline_cpu - Take CPU out of the broadcast mechanism
6161b72d432SThomas Gleixner  * @cpu:	The outgoing CPU
6171b72d432SThomas Gleixner  *
6181b72d432SThomas Gleixner  * Called on the outgoing CPU after it took itself offline.
6191b72d432SThomas Gleixner  */
6201b72d432SThomas Gleixner void tick_offline_cpu(unsigned int cpu)
6211b72d432SThomas Gleixner {
6221b72d432SThomas Gleixner 	raw_spin_lock(&clockevents_lock);
6231b72d432SThomas Gleixner 	tick_broadcast_offline(cpu);
6241b72d432SThomas Gleixner 	raw_spin_unlock(&clockevents_lock);
6251b72d432SThomas Gleixner }
6261b72d432SThomas Gleixner # endif
6271b72d432SThomas Gleixner 
628d316c57fSThomas Gleixner /**
629a49b116dSThomas Gleixner  * tick_cleanup_dead_cpu - Cleanup the tick and clockevents of a dead cpu
63064ab7071SBaokun Li  * @cpu:	The dead CPU
631d316c57fSThomas Gleixner  */
632a49b116dSThomas Gleixner void tick_cleanup_dead_cpu(int cpu)
633d316c57fSThomas Gleixner {
634bb6eddf7SThomas Gleixner 	struct clock_event_device *dev, *tmp;
635f833bab8SSuresh Siddha 	unsigned long flags;
6360b858e6fSLi Zefan 
637b5f91da0SThomas Gleixner 	raw_spin_lock_irqsave(&clockevents_lock, flags);
638d316c57fSThomas Gleixner 
639a49b116dSThomas Gleixner 	tick_shutdown(cpu);
640d316c57fSThomas Gleixner 	/*
641d316c57fSThomas Gleixner 	 * Unregister the clock event devices which were
642d316c57fSThomas Gleixner 	 * released from the users in the notify chain.
643d316c57fSThomas Gleixner 	 */
644bb6eddf7SThomas Gleixner 	list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
645bb6eddf7SThomas Gleixner 		list_del(&dev->list);
646bb6eddf7SThomas Gleixner 	/*
647bb6eddf7SThomas Gleixner 	 * Now check whether the CPU has left unused per cpu devices
648bb6eddf7SThomas Gleixner 	 */
649bb6eddf7SThomas Gleixner 	list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
650bb6eddf7SThomas Gleixner 		if (cpumask_test_cpu(cpu, dev->cpumask) &&
651ea9d8e3fSXiaotian Feng 		    cpumask_weight(dev->cpumask) == 1 &&
652ea9d8e3fSXiaotian Feng 		    !tick_is_broadcast_device(dev)) {
653472c4a94SViresh Kumar 			BUG_ON(!clockevent_state_detached(dev));
654bb6eddf7SThomas Gleixner 			list_del(&dev->list);
655bb6eddf7SThomas Gleixner 		}
656bb6eddf7SThomas Gleixner 	}
657b5f91da0SThomas Gleixner 	raw_spin_unlock_irqrestore(&clockevents_lock, flags);
658d316c57fSThomas Gleixner }
659a49b116dSThomas Gleixner #endif
660501f8670SThomas Gleixner 
661501f8670SThomas Gleixner #ifdef CONFIG_SYSFS
662775be506SBen Dooks static struct bus_type clockevents_subsys = {
663501f8670SThomas Gleixner 	.name		= "clockevents",
664501f8670SThomas Gleixner 	.dev_name       = "clockevent",
665501f8670SThomas Gleixner };
666501f8670SThomas Gleixner 
667501f8670SThomas Gleixner static DEFINE_PER_CPU(struct device, tick_percpu_dev);
668501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev);
669501f8670SThomas Gleixner 
6701fa98d96SYueHaibing static ssize_t current_device_show(struct device *dev,
671501f8670SThomas Gleixner 				   struct device_attribute *attr,
672501f8670SThomas Gleixner 				   char *buf)
673501f8670SThomas Gleixner {
674501f8670SThomas Gleixner 	struct tick_device *td;
675501f8670SThomas Gleixner 	ssize_t count = 0;
676501f8670SThomas Gleixner 
677501f8670SThomas Gleixner 	raw_spin_lock_irq(&clockevents_lock);
678501f8670SThomas Gleixner 	td = tick_get_tick_dev(dev);
679501f8670SThomas Gleixner 	if (td && td->evtdev)
680501f8670SThomas Gleixner 		count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
681501f8670SThomas Gleixner 	raw_spin_unlock_irq(&clockevents_lock);
682501f8670SThomas Gleixner 	return count;
683501f8670SThomas Gleixner }
6841fa98d96SYueHaibing static DEVICE_ATTR_RO(current_device);
685501f8670SThomas Gleixner 
68603e13cf5SThomas Gleixner /* We don't support the abomination of removable broadcast devices */
6871fa98d96SYueHaibing static ssize_t unbind_device_store(struct device *dev,
68803e13cf5SThomas Gleixner 				   struct device_attribute *attr,
68903e13cf5SThomas Gleixner 				   const char *buf, size_t count)
69003e13cf5SThomas Gleixner {
69103e13cf5SThomas Gleixner 	char name[CS_NAME_LEN];
692891292a7SPatrick Palka 	ssize_t ret = sysfs_get_uname(buf, name, count);
693*2966a991SJakob Koschel 	struct clock_event_device *ce = NULL, *iter;
69403e13cf5SThomas Gleixner 
69503e13cf5SThomas Gleixner 	if (ret < 0)
69603e13cf5SThomas Gleixner 		return ret;
69703e13cf5SThomas Gleixner 
69803e13cf5SThomas Gleixner 	ret = -ENODEV;
69903e13cf5SThomas Gleixner 	mutex_lock(&clockevents_mutex);
70003e13cf5SThomas Gleixner 	raw_spin_lock_irq(&clockevents_lock);
701*2966a991SJakob Koschel 	list_for_each_entry(iter, &clockevent_devices, list) {
702*2966a991SJakob Koschel 		if (!strcmp(iter->name, name)) {
703*2966a991SJakob Koschel 			ret = __clockevents_try_unbind(iter, dev->id);
704*2966a991SJakob Koschel 			ce = iter;
70503e13cf5SThomas Gleixner 			break;
70603e13cf5SThomas Gleixner 		}
70703e13cf5SThomas Gleixner 	}
70803e13cf5SThomas Gleixner 	raw_spin_unlock_irq(&clockevents_lock);
70903e13cf5SThomas Gleixner 	/*
71003e13cf5SThomas Gleixner 	 * We hold clockevents_mutex, so ce can't go away
71103e13cf5SThomas Gleixner 	 */
71203e13cf5SThomas Gleixner 	if (ret == -EAGAIN)
71303e13cf5SThomas Gleixner 		ret = clockevents_unbind(ce, dev->id);
71403e13cf5SThomas Gleixner 	mutex_unlock(&clockevents_mutex);
71503e13cf5SThomas Gleixner 	return ret ? ret : count;
71603e13cf5SThomas Gleixner }
7171fa98d96SYueHaibing static DEVICE_ATTR_WO(unbind_device);
71803e13cf5SThomas Gleixner 
719501f8670SThomas Gleixner #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
720501f8670SThomas Gleixner static struct device tick_bc_dev = {
721501f8670SThomas Gleixner 	.init_name	= "broadcast",
722501f8670SThomas Gleixner 	.id		= 0,
723501f8670SThomas Gleixner 	.bus		= &clockevents_subsys,
724501f8670SThomas Gleixner };
725501f8670SThomas Gleixner 
726501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev)
727501f8670SThomas Gleixner {
728501f8670SThomas Gleixner 	return dev == &tick_bc_dev ? tick_get_broadcast_device() :
729501f8670SThomas Gleixner 		&per_cpu(tick_cpu_device, dev->id);
730501f8670SThomas Gleixner }
731501f8670SThomas Gleixner 
732501f8670SThomas Gleixner static __init int tick_broadcast_init_sysfs(void)
733501f8670SThomas Gleixner {
734501f8670SThomas Gleixner 	int err = device_register(&tick_bc_dev);
735501f8670SThomas Gleixner 
736501f8670SThomas Gleixner 	if (!err)
737501f8670SThomas Gleixner 		err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
738501f8670SThomas Gleixner 	return err;
739501f8670SThomas Gleixner }
740501f8670SThomas Gleixner #else
741501f8670SThomas Gleixner static struct tick_device *tick_get_tick_dev(struct device *dev)
742501f8670SThomas Gleixner {
743501f8670SThomas Gleixner 	return &per_cpu(tick_cpu_device, dev->id);
744501f8670SThomas Gleixner }
745501f8670SThomas Gleixner static inline int tick_broadcast_init_sysfs(void) { return 0; }
746de68d9b1SThomas Gleixner #endif
747501f8670SThomas Gleixner 
748501f8670SThomas Gleixner static int __init tick_init_sysfs(void)
749501f8670SThomas Gleixner {
750501f8670SThomas Gleixner 	int cpu;
751501f8670SThomas Gleixner 
752501f8670SThomas Gleixner 	for_each_possible_cpu(cpu) {
753501f8670SThomas Gleixner 		struct device *dev = &per_cpu(tick_percpu_dev, cpu);
754501f8670SThomas Gleixner 		int err;
755501f8670SThomas Gleixner 
756501f8670SThomas Gleixner 		dev->id = cpu;
757501f8670SThomas Gleixner 		dev->bus = &clockevents_subsys;
758501f8670SThomas Gleixner 		err = device_register(dev);
759501f8670SThomas Gleixner 		if (!err)
760501f8670SThomas Gleixner 			err = device_create_file(dev, &dev_attr_current_device);
76103e13cf5SThomas Gleixner 		if (!err)
76203e13cf5SThomas Gleixner 			err = device_create_file(dev, &dev_attr_unbind_device);
763501f8670SThomas Gleixner 		if (err)
764501f8670SThomas Gleixner 			return err;
765501f8670SThomas Gleixner 	}
766501f8670SThomas Gleixner 	return tick_broadcast_init_sysfs();
767501f8670SThomas Gleixner }
768501f8670SThomas Gleixner 
769501f8670SThomas Gleixner static int __init clockevents_init_sysfs(void)
770501f8670SThomas Gleixner {
771501f8670SThomas Gleixner 	int err = subsys_system_register(&clockevents_subsys, NULL);
772501f8670SThomas Gleixner 
773501f8670SThomas Gleixner 	if (!err)
774501f8670SThomas Gleixner 		err = tick_init_sysfs();
775501f8670SThomas Gleixner 	return err;
776501f8670SThomas Gleixner }
777501f8670SThomas Gleixner device_initcall(clockevents_init_sysfs);
778501f8670SThomas Gleixner #endif /* SYSFS */
779