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