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> |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 21 | #include <linux/tick.h> |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 22 | |
| 23 | /* The registered clock event devices */ |
| 24 | static LIST_HEAD(clockevent_devices); |
| 25 | static LIST_HEAD(clockevents_released); |
| 26 | |
| 27 | /* Notification for clock events */ |
| 28 | static RAW_NOTIFIER_HEAD(clockevents_chain); |
| 29 | |
| 30 | /* Protection for the above */ |
| 31 | static DEFINE_SPINLOCK(clockevents_lock); |
| 32 | |
| 33 | /** |
| 34 | * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds |
| 35 | * @latch: value to convert |
| 36 | * @evt: pointer to clock event device descriptor |
| 37 | * |
| 38 | * Math helper, returns latch value converted to nanoseconds (bound checked) |
| 39 | */ |
| 40 | unsigned long clockevent_delta2ns(unsigned long latch, |
| 41 | struct clock_event_device *evt) |
| 42 | { |
| 43 | u64 clc = ((u64) latch << evt->shift); |
| 44 | |
Ingo Molnar | 45fe4fe | 2008-01-30 13:30:03 +0100 | [diff] [blame] | 45 | if (unlikely(!evt->mult)) { |
| 46 | evt->mult = 1; |
| 47 | WARN_ON(1); |
| 48 | } |
| 49 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 50 | do_div(clc, evt->mult); |
| 51 | if (clc < 1000) |
| 52 | clc = 1000; |
| 53 | if (clc > LONG_MAX) |
| 54 | clc = LONG_MAX; |
| 55 | |
| 56 | return (unsigned long) clc; |
| 57 | } |
Magnus Damm | c81fc2c | 2009-05-01 14:52:47 +0900 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(clockevent_delta2ns); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * clockevents_set_mode - set the operating mode of a clock event device |
| 62 | * @dev: device to modify |
| 63 | * @mode: new mode |
| 64 | * |
| 65 | * Must be called with interrupts disabled ! |
| 66 | */ |
| 67 | void clockevents_set_mode(struct clock_event_device *dev, |
| 68 | enum clock_event_mode mode) |
| 69 | { |
| 70 | if (dev->mode != mode) { |
| 71 | dev->set_mode(mode, dev); |
| 72 | dev->mode = mode; |
Magnus Damm | 2d68259 | 2009-01-16 17:14:38 +0900 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * A nsec2cyc multiplicator of 0 is invalid and we'd crash |
| 76 | * on it, so fix it up and emit a warning: |
| 77 | */ |
| 78 | if (mode == CLOCK_EVT_MODE_ONESHOT) { |
| 79 | if (unlikely(!dev->mult)) { |
| 80 | dev->mult = 1; |
| 81 | WARN_ON(1); |
| 82 | } |
| 83 | } |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 88 | * clockevents_shutdown - shutdown the device and clear next_event |
| 89 | * @dev: device to shutdown |
| 90 | */ |
| 91 | void clockevents_shutdown(struct clock_event_device *dev) |
| 92 | { |
| 93 | clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN); |
| 94 | dev->next_event.tv64 = KTIME_MAX; |
| 95 | } |
| 96 | |
| 97 | /** |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 98 | * clockevents_program_event - Reprogram the clock event device. |
| 99 | * @expires: absolute expiry time (monotonic clock) |
| 100 | * |
| 101 | * Returns 0 on success, -ETIME when the event is in the past. |
| 102 | */ |
| 103 | int clockevents_program_event(struct clock_event_device *dev, ktime_t expires, |
| 104 | ktime_t now) |
| 105 | { |
| 106 | unsigned long long clc; |
| 107 | int64_t delta; |
| 108 | |
Thomas Gleixner | 167b1de | 2007-12-07 19:16:17 +0100 | [diff] [blame] | 109 | if (unlikely(expires.tv64 < 0)) { |
| 110 | WARN_ON_ONCE(1); |
| 111 | return -ETIME; |
| 112 | } |
| 113 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 114 | delta = ktime_to_ns(ktime_sub(expires, now)); |
| 115 | |
| 116 | if (delta <= 0) |
| 117 | return -ETIME; |
| 118 | |
| 119 | dev->next_event = expires; |
| 120 | |
| 121 | if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN) |
| 122 | return 0; |
| 123 | |
| 124 | if (delta > dev->max_delta_ns) |
| 125 | delta = dev->max_delta_ns; |
| 126 | if (delta < dev->min_delta_ns) |
| 127 | delta = dev->min_delta_ns; |
| 128 | |
| 129 | clc = delta * dev->mult; |
| 130 | clc >>= dev->shift; |
| 131 | |
| 132 | return dev->set_next_event((unsigned long) clc, dev); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * clockevents_register_notifier - register a clock events change listener |
| 137 | */ |
| 138 | int clockevents_register_notifier(struct notifier_block *nb) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | spin_lock(&clockevents_lock); |
| 143 | ret = raw_notifier_chain_register(&clockevents_chain, nb); |
| 144 | spin_unlock(&clockevents_lock); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 149 | /* |
| 150 | * Notify about a clock event change. Called with clockevents_lock |
| 151 | * held. |
| 152 | */ |
| 153 | static void clockevents_do_notify(unsigned long reason, void *dev) |
| 154 | { |
| 155 | raw_notifier_call_chain(&clockevents_chain, reason, dev); |
| 156 | } |
| 157 | |
| 158 | /* |
Li Zefan | 3eb0567 | 2008-02-08 04:19:25 -0800 | [diff] [blame] | 159 | * Called after a notify add to make devices available which were |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 160 | * released from the notifier call. |
| 161 | */ |
| 162 | static void clockevents_notify_released(void) |
| 163 | { |
| 164 | struct clock_event_device *dev; |
| 165 | |
| 166 | while (!list_empty(&clockevents_released)) { |
| 167 | dev = list_entry(clockevents_released.next, |
| 168 | struct clock_event_device, list); |
| 169 | list_del(&dev->list); |
| 170 | list_add(&dev->list, &clockevent_devices); |
| 171 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * clockevents_register_device - register a clock event device |
| 177 | * @dev: device to register |
| 178 | */ |
| 179 | void clockevents_register_device(struct clock_event_device *dev) |
| 180 | { |
| 181 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 182 | BUG_ON(!dev->cpumask); |
| 183 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 184 | spin_lock(&clockevents_lock); |
| 185 | |
| 186 | list_add(&dev->list, &clockevent_devices); |
| 187 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); |
| 188 | clockevents_notify_released(); |
| 189 | |
| 190 | spin_unlock(&clockevents_lock); |
| 191 | } |
Magnus Damm | c81fc2c | 2009-05-01 14:52:47 +0900 | [diff] [blame] | 192 | EXPORT_SYMBOL_GPL(clockevents_register_device); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Noop handler when we shut down an event device |
| 196 | */ |
Venkatesh Pallipadi | 7c1e768 | 2008-09-03 21:36:50 +0000 | [diff] [blame] | 197 | void clockevents_handle_noop(struct clock_event_device *dev) |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 198 | { |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * clockevents_exchange_device - release and request clock devices |
| 203 | * @old: device to release (can be NULL) |
| 204 | * @new: device to request (can be NULL) |
| 205 | * |
| 206 | * Called from the notifier chain. clockevents_lock is held already |
| 207 | */ |
| 208 | void clockevents_exchange_device(struct clock_event_device *old, |
| 209 | struct clock_event_device *new) |
| 210 | { |
| 211 | unsigned long flags; |
| 212 | |
| 213 | local_irq_save(flags); |
| 214 | /* |
| 215 | * Caller releases a clock event device. We queue it into the |
| 216 | * released list and do a notify add later. |
| 217 | */ |
| 218 | if (old) { |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 219 | clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); |
| 220 | list_del(&old->list); |
| 221 | list_add(&old->list, &clockevents_released); |
| 222 | } |
| 223 | |
| 224 | if (new) { |
| 225 | BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED); |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 226 | clockevents_shutdown(new); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 227 | } |
| 228 | local_irq_restore(flags); |
| 229 | } |
| 230 | |
Thomas Gleixner | de68d9b | 2007-10-12 23:04:05 +0200 | [diff] [blame] | 231 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 232 | /** |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 233 | * clockevents_notify - notification about relevant events |
| 234 | */ |
| 235 | void clockevents_notify(unsigned long reason, void *arg) |
| 236 | { |
Li Zefan | 0b858e6 | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 237 | struct list_head *node, *tmp; |
| 238 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 239 | spin_lock(&clockevents_lock); |
| 240 | clockevents_do_notify(reason, arg); |
| 241 | |
| 242 | switch (reason) { |
| 243 | case CLOCK_EVT_NOTIFY_CPU_DEAD: |
| 244 | /* |
| 245 | * Unregister the clock event devices which were |
| 246 | * released from the users in the notify chain. |
| 247 | */ |
Li Zefan | 0b858e6 | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 248 | list_for_each_safe(node, tmp, &clockevents_released) |
| 249 | list_del(node); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 250 | break; |
| 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | spin_unlock(&clockevents_lock); |
| 255 | } |
| 256 | EXPORT_SYMBOL_GPL(clockevents_notify); |
Thomas Gleixner | de68d9b | 2007-10-12 23:04:05 +0200 | [diff] [blame] | 257 | #endif |