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