| Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/kernel/time/clockevents.c | 
 | 3 |  * | 
 | 4 |  * This file contains functions which manage clock event devices. | 
 | 5 |  * | 
 | 6 |  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> | 
 | 7 |  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar | 
 | 8 |  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner | 
 | 9 |  * | 
 | 10 |  * This code is licenced under the GPL version 2. For details see | 
 | 11 |  * kernel-base/COPYING. | 
 | 12 |  */ | 
 | 13 |  | 
 | 14 | #include <linux/clockchips.h> | 
 | 15 | #include <linux/hrtimer.h> | 
 | 16 | #include <linux/init.h> | 
 | 17 | #include <linux/module.h> | 
 | 18 | #include <linux/notifier.h> | 
 | 19 | #include <linux/smp.h> | 
 | 20 | #include <linux/sysdev.h> | 
 | 21 |  | 
 | 22 | /* The registered clock event devices */ | 
 | 23 | static LIST_HEAD(clockevent_devices); | 
 | 24 | static LIST_HEAD(clockevents_released); | 
 | 25 |  | 
 | 26 | /* Notification for clock events */ | 
 | 27 | static RAW_NOTIFIER_HEAD(clockevents_chain); | 
 | 28 |  | 
 | 29 | /* Protection for the above */ | 
 | 30 | static DEFINE_SPINLOCK(clockevents_lock); | 
 | 31 |  | 
 | 32 | /** | 
 | 33 |  * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds | 
 | 34 |  * @latch:	value to convert | 
 | 35 |  * @evt:	pointer to clock event device descriptor | 
 | 36 |  * | 
 | 37 |  * Math helper, returns latch value converted to nanoseconds (bound checked) | 
 | 38 |  */ | 
 | 39 | unsigned long clockevent_delta2ns(unsigned long latch, | 
 | 40 | 				  struct clock_event_device *evt) | 
 | 41 | { | 
 | 42 | 	u64 clc = ((u64) latch << evt->shift); | 
 | 43 |  | 
 | 44 | 	do_div(clc, evt->mult); | 
 | 45 | 	if (clc < 1000) | 
 | 46 | 		clc = 1000; | 
 | 47 | 	if (clc > LONG_MAX) | 
 | 48 | 		clc = LONG_MAX; | 
 | 49 |  | 
 | 50 | 	return (unsigned long) clc; | 
 | 51 | } | 
 | 52 |  | 
 | 53 | /** | 
 | 54 |  * clockevents_set_mode - set the operating mode of a clock event device | 
 | 55 |  * @dev:	device to modify | 
 | 56 |  * @mode:	new mode | 
 | 57 |  * | 
 | 58 |  * Must be called with interrupts disabled ! | 
 | 59 |  */ | 
 | 60 | void clockevents_set_mode(struct clock_event_device *dev, | 
 | 61 | 				 enum clock_event_mode mode) | 
 | 62 | { | 
 | 63 | 	if (dev->mode != mode) { | 
 | 64 | 		dev->set_mode(mode, dev); | 
 | 65 | 		dev->mode = mode; | 
 | 66 | 	} | 
 | 67 | } | 
 | 68 |  | 
 | 69 | /** | 
 | 70 |  * clockevents_program_event - Reprogram the clock event device. | 
 | 71 |  * @expires:	absolute expiry time (monotonic clock) | 
 | 72 |  * | 
 | 73 |  * Returns 0 on success, -ETIME when the event is in the past. | 
 | 74 |  */ | 
 | 75 | int clockevents_program_event(struct clock_event_device *dev, ktime_t expires, | 
 | 76 | 			      ktime_t now) | 
 | 77 | { | 
 | 78 | 	unsigned long long clc; | 
 | 79 | 	int64_t delta; | 
 | 80 |  | 
 | 81 | 	delta = ktime_to_ns(ktime_sub(expires, now)); | 
 | 82 |  | 
 | 83 | 	if (delta <= 0) | 
 | 84 | 		return -ETIME; | 
 | 85 |  | 
 | 86 | 	dev->next_event = expires; | 
 | 87 |  | 
 | 88 | 	if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN) | 
 | 89 | 		return 0; | 
 | 90 |  | 
 | 91 | 	if (delta > dev->max_delta_ns) | 
 | 92 | 		delta = dev->max_delta_ns; | 
 | 93 | 	if (delta < dev->min_delta_ns) | 
 | 94 | 		delta = dev->min_delta_ns; | 
 | 95 |  | 
 | 96 | 	clc = delta * dev->mult; | 
 | 97 | 	clc >>= dev->shift; | 
 | 98 |  | 
 | 99 | 	return dev->set_next_event((unsigned long) clc, dev); | 
 | 100 | } | 
 | 101 |  | 
 | 102 | /** | 
 | 103 |  * clockevents_register_notifier - register a clock events change listener | 
 | 104 |  */ | 
 | 105 | int clockevents_register_notifier(struct notifier_block *nb) | 
 | 106 | { | 
 | 107 | 	int ret; | 
 | 108 |  | 
 | 109 | 	spin_lock(&clockevents_lock); | 
 | 110 | 	ret = raw_notifier_chain_register(&clockevents_chain, nb); | 
 | 111 | 	spin_unlock(&clockevents_lock); | 
 | 112 |  | 
 | 113 | 	return ret; | 
 | 114 | } | 
 | 115 |  | 
| Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 116 | /* | 
 | 117 |  * Notify about a clock event change. Called with clockevents_lock | 
 | 118 |  * held. | 
 | 119 |  */ | 
 | 120 | static void clockevents_do_notify(unsigned long reason, void *dev) | 
 | 121 | { | 
 | 122 | 	raw_notifier_call_chain(&clockevents_chain, reason, dev); | 
 | 123 | } | 
 | 124 |  | 
 | 125 | /* | 
 | 126 |  * Called after a notify add to make devices availble which were | 
 | 127 |  * released from the notifier call. | 
 | 128 |  */ | 
 | 129 | static void clockevents_notify_released(void) | 
 | 130 | { | 
 | 131 | 	struct clock_event_device *dev; | 
 | 132 |  | 
 | 133 | 	while (!list_empty(&clockevents_released)) { | 
 | 134 | 		dev = list_entry(clockevents_released.next, | 
 | 135 | 				 struct clock_event_device, list); | 
 | 136 | 		list_del(&dev->list); | 
 | 137 | 		list_add(&dev->list, &clockevent_devices); | 
 | 138 | 		clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); | 
 | 139 | 	} | 
 | 140 | } | 
 | 141 |  | 
 | 142 | /** | 
 | 143 |  * clockevents_register_device - register a clock event device | 
 | 144 |  * @dev:	device to register | 
 | 145 |  */ | 
 | 146 | void clockevents_register_device(struct clock_event_device *dev) | 
 | 147 | { | 
 | 148 | 	BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); | 
 | 149 |  | 
 | 150 | 	spin_lock(&clockevents_lock); | 
 | 151 |  | 
 | 152 | 	list_add(&dev->list, &clockevent_devices); | 
 | 153 | 	clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); | 
 | 154 | 	clockevents_notify_released(); | 
 | 155 |  | 
 | 156 | 	spin_unlock(&clockevents_lock); | 
 | 157 | } | 
 | 158 |  | 
 | 159 | /* | 
 | 160 |  * Noop handler when we shut down an event device | 
 | 161 |  */ | 
 | 162 | static void clockevents_handle_noop(struct clock_event_device *dev) | 
 | 163 | { | 
 | 164 | } | 
 | 165 |  | 
 | 166 | /** | 
 | 167 |  * clockevents_exchange_device - release and request clock devices | 
 | 168 |  * @old:	device to release (can be NULL) | 
 | 169 |  * @new:	device to request (can be NULL) | 
 | 170 |  * | 
 | 171 |  * Called from the notifier chain. clockevents_lock is held already | 
 | 172 |  */ | 
 | 173 | void clockevents_exchange_device(struct clock_event_device *old, | 
 | 174 | 				 struct clock_event_device *new) | 
 | 175 | { | 
 | 176 | 	unsigned long flags; | 
 | 177 |  | 
 | 178 | 	local_irq_save(flags); | 
 | 179 | 	/* | 
 | 180 | 	 * Caller releases a clock event device. We queue it into the | 
 | 181 | 	 * released list and do a notify add later. | 
 | 182 | 	 */ | 
 | 183 | 	if (old) { | 
 | 184 | 		old->event_handler = clockevents_handle_noop; | 
 | 185 | 		clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); | 
 | 186 | 		list_del(&old->list); | 
 | 187 | 		list_add(&old->list, &clockevents_released); | 
 | 188 | 	} | 
 | 189 |  | 
 | 190 | 	if (new) { | 
 | 191 | 		BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED); | 
 | 192 | 		clockevents_set_mode(new, CLOCK_EVT_MODE_SHUTDOWN); | 
 | 193 | 	} | 
 | 194 | 	local_irq_restore(flags); | 
 | 195 | } | 
 | 196 |  | 
 | 197 | /** | 
| Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 198 |  * clockevents_notify - notification about relevant events | 
 | 199 |  */ | 
 | 200 | void clockevents_notify(unsigned long reason, void *arg) | 
 | 201 | { | 
 | 202 | 	spin_lock(&clockevents_lock); | 
 | 203 | 	clockevents_do_notify(reason, arg); | 
 | 204 |  | 
 | 205 | 	switch (reason) { | 
 | 206 | 	case CLOCK_EVT_NOTIFY_CPU_DEAD: | 
 | 207 | 		/* | 
 | 208 | 		 * Unregister the clock event devices which were | 
 | 209 | 		 * released from the users in the notify chain. | 
 | 210 | 		 */ | 
 | 211 | 		while (!list_empty(&clockevents_released)) { | 
 | 212 | 			struct clock_event_device *dev; | 
 | 213 |  | 
 | 214 | 			dev = list_entry(clockevents_released.next, | 
 | 215 | 					 struct clock_event_device, list); | 
 | 216 | 			list_del(&dev->list); | 
 | 217 | 		} | 
 | 218 | 		break; | 
 | 219 | 	default: | 
 | 220 | 		break; | 
 | 221 | 	} | 
 | 222 | 	spin_unlock(&clockevents_lock); | 
 | 223 | } | 
 | 224 | EXPORT_SYMBOL_GPL(clockevents_notify); | 
 | 225 |  |