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 | |
H Hartley Sweeten | 8e1a928 | 2009-10-16 18:19:01 -0400 | [diff] [blame] | 22 | #include "tick-internal.h" |
| 23 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 24 | /* The registered clock event devices */ |
| 25 | static LIST_HEAD(clockevent_devices); |
| 26 | static LIST_HEAD(clockevents_released); |
| 27 | |
| 28 | /* Notification for clock events */ |
| 29 | static RAW_NOTIFIER_HEAD(clockevents_chain); |
| 30 | |
| 31 | /* Protection for the above */ |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 32 | static DEFINE_RAW_SPINLOCK(clockevents_lock); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds |
| 36 | * @latch: value to convert |
| 37 | * @evt: pointer to clock event device descriptor |
| 38 | * |
| 39 | * Math helper, returns latch value converted to nanoseconds (bound checked) |
| 40 | */ |
Jon Hunter | 97813f2 | 2009-08-18 12:45:11 -0500 | [diff] [blame] | 41 | u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt) |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 42 | { |
Jon Hunter | 97813f2 | 2009-08-18 12:45:11 -0500 | [diff] [blame] | 43 | u64 clc = (u64) latch << evt->shift; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 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; |
Jon Hunter | 97813f2 | 2009-08-18 12:45:11 -0500 | [diff] [blame] | 53 | if (clc > KTIME_MAX) |
| 54 | clc = KTIME_MAX; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 55 | |
Jon Hunter | 97813f2 | 2009-08-18 12:45:11 -0500 | [diff] [blame] | 56 | return clc; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 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 | { |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 140 | unsigned long flags; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 141 | int ret; |
| 142 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 143 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 144 | ret = raw_notifier_chain_register(&clockevents_chain, nb); |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 145 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 150 | /* |
| 151 | * Notify about a clock event change. Called with clockevents_lock |
| 152 | * held. |
| 153 | */ |
| 154 | static void clockevents_do_notify(unsigned long reason, void *dev) |
| 155 | { |
| 156 | raw_notifier_call_chain(&clockevents_chain, reason, dev); |
| 157 | } |
| 158 | |
| 159 | /* |
Li Zefan | 3eb0567 | 2008-02-08 04:19:25 -0800 | [diff] [blame] | 160 | * Called after a notify add to make devices available which were |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 161 | * released from the notifier call. |
| 162 | */ |
| 163 | static void clockevents_notify_released(void) |
| 164 | { |
| 165 | struct clock_event_device *dev; |
| 166 | |
| 167 | while (!list_empty(&clockevents_released)) { |
| 168 | dev = list_entry(clockevents_released.next, |
| 169 | struct clock_event_device, list); |
| 170 | list_del(&dev->list); |
| 171 | list_add(&dev->list, &clockevent_devices); |
| 172 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * clockevents_register_device - register a clock event device |
| 178 | * @dev: device to register |
| 179 | */ |
| 180 | void clockevents_register_device(struct clock_event_device *dev) |
| 181 | { |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 182 | unsigned long flags; |
| 183 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 184 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
Thomas Gleixner | 1b054b6 | 2011-06-03 11:13:33 +0200 | [diff] [blame] | 185 | if (!dev->cpumask) { |
| 186 | WARN_ON(num_possible_cpus() > 1); |
| 187 | dev->cpumask = cpumask_of(smp_processor_id()); |
| 188 | } |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 189 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 190 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 191 | |
| 192 | list_add(&dev->list, &clockevent_devices); |
| 193 | clockevents_do_notify(CLOCK_EVT_NOTIFY_ADD, dev); |
| 194 | clockevents_notify_released(); |
| 195 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 196 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 197 | } |
Magnus Damm | c81fc2c | 2009-05-01 14:52:47 +0900 | [diff] [blame] | 198 | EXPORT_SYMBOL_GPL(clockevents_register_device); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 199 | |
Thomas Gleixner | 57f0fcb | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 200 | static void clockevents_config(struct clock_event_device *dev, |
| 201 | u32 freq) |
| 202 | { |
Thomas Gleixner | c0e299b | 2011-05-20 10:50:52 +0200 | [diff] [blame] | 203 | u64 sec; |
Thomas Gleixner | 57f0fcb | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 204 | |
| 205 | if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT)) |
| 206 | return; |
| 207 | |
| 208 | /* |
| 209 | * Calculate the maximum number of seconds we can sleep. Limit |
| 210 | * to 10 minutes for hardware which can program more than |
| 211 | * 32bit ticks so we still get reasonable conversion values. |
| 212 | */ |
| 213 | sec = dev->max_delta_ticks; |
| 214 | do_div(sec, freq); |
| 215 | if (!sec) |
| 216 | sec = 1; |
| 217 | else if (sec > 600 && dev->max_delta_ticks > UINT_MAX) |
| 218 | sec = 600; |
| 219 | |
| 220 | clockevents_calc_mult_shift(dev, freq, sec); |
| 221 | dev->min_delta_ns = clockevent_delta2ns(dev->min_delta_ticks, dev); |
| 222 | dev->max_delta_ns = clockevent_delta2ns(dev->max_delta_ticks, dev); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * clockevents_config_and_register - Configure and register a clock event device |
| 227 | * @dev: device to register |
| 228 | * @freq: The clock frequency |
| 229 | * @min_delta: The minimum clock ticks to program in oneshot mode |
| 230 | * @max_delta: The maximum clock ticks to program in oneshot mode |
| 231 | * |
| 232 | * min/max_delta can be 0 for devices which do not support oneshot mode. |
| 233 | */ |
| 234 | void clockevents_config_and_register(struct clock_event_device *dev, |
| 235 | u32 freq, unsigned long min_delta, |
| 236 | unsigned long max_delta) |
| 237 | { |
| 238 | dev->min_delta_ticks = min_delta; |
| 239 | dev->max_delta_ticks = max_delta; |
| 240 | clockevents_config(dev, freq); |
| 241 | clockevents_register_device(dev); |
| 242 | } |
| 243 | |
Thomas Gleixner | 80b816b | 2011-05-18 21:33:42 +0000 | [diff] [blame] | 244 | /** |
| 245 | * clockevents_update_freq - Update frequency and reprogram a clock event device. |
| 246 | * @dev: device to modify |
| 247 | * @freq: new device frequency |
| 248 | * |
| 249 | * Reconfigure and reprogram a clock event device in oneshot |
| 250 | * mode. Must be called on the cpu for which the device delivers per |
| 251 | * cpu timer events with interrupts disabled! Returns 0 on success, |
| 252 | * -ETIME when the event is in the past. |
| 253 | */ |
| 254 | int clockevents_update_freq(struct clock_event_device *dev, u32 freq) |
| 255 | { |
| 256 | clockevents_config(dev, freq); |
| 257 | |
| 258 | if (dev->mode != CLOCK_EVT_MODE_ONESHOT) |
| 259 | return 0; |
| 260 | |
| 261 | return clockevents_program_event(dev, dev->next_event, ktime_get()); |
| 262 | } |
| 263 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 264 | /* |
| 265 | * Noop handler when we shut down an event device |
| 266 | */ |
Venkatesh Pallipadi | 7c1e768 | 2008-09-03 21:36:50 +0000 | [diff] [blame] | 267 | void clockevents_handle_noop(struct clock_event_device *dev) |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 268 | { |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * clockevents_exchange_device - release and request clock devices |
| 273 | * @old: device to release (can be NULL) |
| 274 | * @new: device to request (can be NULL) |
| 275 | * |
| 276 | * Called from the notifier chain. clockevents_lock is held already |
| 277 | */ |
| 278 | void clockevents_exchange_device(struct clock_event_device *old, |
| 279 | struct clock_event_device *new) |
| 280 | { |
| 281 | unsigned long flags; |
| 282 | |
| 283 | local_irq_save(flags); |
| 284 | /* |
| 285 | * Caller releases a clock event device. We queue it into the |
| 286 | * released list and do a notify add later. |
| 287 | */ |
| 288 | if (old) { |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 289 | clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); |
| 290 | list_del(&old->list); |
| 291 | list_add(&old->list, &clockevents_released); |
| 292 | } |
| 293 | |
| 294 | if (new) { |
| 295 | BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED); |
Thomas Gleixner | 2344abb | 2008-09-16 11:32:50 -0700 | [diff] [blame] | 296 | clockevents_shutdown(new); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 297 | } |
| 298 | local_irq_restore(flags); |
| 299 | } |
| 300 | |
Thomas Gleixner | de68d9b | 2007-10-12 23:04:05 +0200 | [diff] [blame] | 301 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 302 | /** |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 303 | * clockevents_notify - notification about relevant events |
| 304 | */ |
| 305 | void clockevents_notify(unsigned long reason, void *arg) |
| 306 | { |
Thomas Gleixner | bb6eddf | 2009-12-10 15:35:10 +0100 | [diff] [blame] | 307 | struct clock_event_device *dev, *tmp; |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 308 | unsigned long flags; |
Thomas Gleixner | bb6eddf | 2009-12-10 15:35:10 +0100 | [diff] [blame] | 309 | int cpu; |
Li Zefan | 0b858e6 | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 310 | |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 311 | raw_spin_lock_irqsave(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 312 | clockevents_do_notify(reason, arg); |
| 313 | |
| 314 | switch (reason) { |
| 315 | case CLOCK_EVT_NOTIFY_CPU_DEAD: |
| 316 | /* |
| 317 | * Unregister the clock event devices which were |
| 318 | * released from the users in the notify chain. |
| 319 | */ |
Thomas Gleixner | bb6eddf | 2009-12-10 15:35:10 +0100 | [diff] [blame] | 320 | list_for_each_entry_safe(dev, tmp, &clockevents_released, list) |
| 321 | list_del(&dev->list); |
| 322 | /* |
| 323 | * Now check whether the CPU has left unused per cpu devices |
| 324 | */ |
| 325 | cpu = *((int *)arg); |
| 326 | list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) { |
| 327 | if (cpumask_test_cpu(cpu, dev->cpumask) && |
Xiaotian Feng | ea9d8e3 | 2010-01-07 11:22:44 +0800 | [diff] [blame] | 328 | cpumask_weight(dev->cpumask) == 1 && |
| 329 | !tick_is_broadcast_device(dev)) { |
Thomas Gleixner | bb6eddf | 2009-12-10 15:35:10 +0100 | [diff] [blame] | 330 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
| 331 | list_del(&dev->list); |
| 332 | } |
| 333 | } |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 334 | break; |
| 335 | default: |
| 336 | break; |
| 337 | } |
Thomas Gleixner | b5f91da | 2009-12-08 12:40:31 +0100 | [diff] [blame] | 338 | raw_spin_unlock_irqrestore(&clockevents_lock, flags); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 339 | } |
| 340 | EXPORT_SYMBOL_GPL(clockevents_notify); |
Thomas Gleixner | de68d9b | 2007-10-12 23:04:05 +0200 | [diff] [blame] | 341 | #endif |