Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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