1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * This file contains the functions which manage clocksource drivers. 4 * 5 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/clocksource.h> 11 #include <linux/cpu.h> 12 #include <linux/delay.h> 13 #include <linux/device.h> 14 #include <linux/init.h> 15 #include <linux/kthread.h> 16 #include <linux/module.h> 17 #include <linux/prandom.h> 18 #include <linux/sched.h> 19 #include <linux/tick.h> 20 #include <linux/topology.h> 21 22 #include "tick-internal.h" 23 #include "timekeeping_internal.h" 24 25 static void clocksource_enqueue(struct clocksource *cs); 26 27 static noinline u64 cycles_to_nsec_safe(struct clocksource *cs, u64 start, u64 end) 28 { 29 u64 delta = clocksource_delta(end, start, cs->mask, cs->max_raw_delta); 30 31 if (likely(delta < cs->max_cycles)) 32 return clocksource_cyc2ns(delta, cs->mult, cs->shift); 33 34 return mul_u64_u32_shr(delta, cs->mult, cs->shift); 35 } 36 37 /** 38 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks 39 * @mult: pointer to mult variable 40 * @shift: pointer to shift variable 41 * @from: frequency to convert from 42 * @to: frequency to convert to 43 * @maxsec: guaranteed runtime conversion range in seconds 44 * 45 * The function evaluates the shift/mult pair for the scaled math 46 * operations of clocksources and clockevents. 47 * 48 * @to and @from are frequency values in HZ. For clock sources @to is 49 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock 50 * event @to is the counter frequency and @from is NSEC_PER_SEC. 51 * 52 * The @maxsec conversion range argument controls the time frame in 53 * seconds which must be covered by the runtime conversion with the 54 * calculated mult and shift factors. This guarantees that no 64bit 55 * overflow happens when the input value of the conversion is 56 * multiplied with the calculated mult factor. Larger ranges may 57 * reduce the conversion accuracy by choosing smaller mult and shift 58 * factors. 59 */ 60 void 61 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec) 62 { 63 u64 tmp; 64 u32 sft, sftacc= 32; 65 66 /* 67 * Calculate the shift factor which is limiting the conversion 68 * range: 69 */ 70 tmp = ((u64)maxsec * from) >> 32; 71 while (tmp) { 72 tmp >>=1; 73 sftacc--; 74 } 75 76 /* 77 * Find the conversion shift/mult pair which has the best 78 * accuracy and fits the maxsec conversion range: 79 */ 80 for (sft = 32; sft > 0; sft--) { 81 tmp = (u64) to << sft; 82 tmp += from / 2; 83 do_div(tmp, from); 84 if ((tmp >> sftacc) == 0) 85 break; 86 } 87 *mult = tmp; 88 *shift = sft; 89 } 90 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift); 91 92 /*[Clocksource internal variables]--------- 93 * curr_clocksource: 94 * currently selected clocksource. 95 * suspend_clocksource: 96 * used to calculate the suspend time. 97 * clocksource_list: 98 * linked list with the registered clocksources 99 * clocksource_mutex: 100 * protects manipulations to curr_clocksource and the clocksource_list 101 * override_name: 102 * Name of the user-specified clocksource. 103 */ 104 static struct clocksource *curr_clocksource; 105 static struct clocksource *suspend_clocksource; 106 static LIST_HEAD(clocksource_list); 107 static DEFINE_MUTEX(clocksource_mutex); 108 static char override_name[CS_NAME_LEN]; 109 static int finished_booting; 110 static u64 suspend_start; 111 112 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG 113 static void clocksource_watchdog_work(struct work_struct *work); 114 static void clocksource_select(void); 115 116 static LIST_HEAD(watchdog_list); 117 static struct clocksource *watchdog; 118 static struct timer_list watchdog_timer; 119 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); 120 static DEFINE_SPINLOCK(watchdog_lock); 121 static int watchdog_running; 122 static atomic_t watchdog_reset_pending; 123 124 /* Watchdog interval: 0.5sec. */ 125 #define WATCHDOG_INTERVAL (HZ >> 1) 126 127 /* Maximum time between two reference watchdog readouts */ 128 #define WATCHDOG_READOUT_MAX_NS (50U * NSEC_PER_USEC) 129 130 /* 131 * Maximum time between two remote readouts for NUMA=n. On NUMA enabled systems 132 * the timeout is calculated from the numa distance. 133 */ 134 #define WATCHDOG_DEFAULT_TIMEOUT_NS (50U * NSEC_PER_USEC) 135 136 /* 137 * Remote timeout NUMA distance multiplier. The local distance is 10. The 138 * default remote distance is 20. ACPI tables provide more accurate numbers 139 * which are guaranteed to be greater than the local distance. 140 * 141 * This results in a 5us base value, which is equivalent to the above !NUMA 142 * default. 143 */ 144 #define WATCHDOG_NUMA_MULTIPLIER_NS ((u64)(WATCHDOG_DEFAULT_TIMEOUT_NS / LOCAL_DISTANCE)) 145 146 /* Limit the NUMA timeout in case the distance values are insanely big */ 147 #define WATCHDOG_NUMA_MAX_TIMEOUT_NS ((u64)(500U * NSEC_PER_USEC)) 148 149 /* Shift values to calculate the approximate $N ppm of a given delta. */ 150 #define SHIFT_500PPM 11 151 #define SHIFT_4000PPM 8 152 153 /* Number of attempts to read the watchdog */ 154 #define WATCHDOG_FREQ_RETRIES 3 155 156 /* Five reads local and remote for inter CPU skew detection */ 157 #define WATCHDOG_REMOTE_MAX_SEQ 10 158 159 static inline void clocksource_watchdog_lock(unsigned long *flags) 160 { 161 spin_lock_irqsave(&watchdog_lock, *flags); 162 } 163 164 static inline void clocksource_watchdog_unlock(unsigned long *flags) 165 { 166 spin_unlock_irqrestore(&watchdog_lock, *flags); 167 } 168 169 static int clocksource_watchdog_kthread(void *data); 170 171 static void clocksource_watchdog_work(struct work_struct *work) 172 { 173 /* 174 * We cannot directly run clocksource_watchdog_kthread() here, because 175 * clocksource_select() calls timekeeping_notify() which uses 176 * stop_machine(). One cannot use stop_machine() from a workqueue() due 177 * lock inversions wrt CPU hotplug. 178 * 179 * Also, we only ever run this work once or twice during the lifetime 180 * of the kernel, so there is no point in creating a more permanent 181 * kthread for this. 182 * 183 * If kthread_run fails the next watchdog scan over the 184 * watchdog_list will find the unstable clock again. 185 */ 186 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog"); 187 } 188 189 static void clocksource_change_rating(struct clocksource *cs, int rating) 190 { 191 list_del(&cs->list); 192 cs->rating = rating; 193 clocksource_enqueue(cs); 194 } 195 196 static void __clocksource_unstable(struct clocksource *cs) 197 { 198 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); 199 cs->flags |= CLOCK_SOURCE_UNSTABLE; 200 201 /* 202 * If the clocksource is registered clocksource_watchdog_kthread() will 203 * re-rate and re-select. 204 */ 205 if (list_empty(&cs->list)) { 206 cs->rating = 0; 207 return; 208 } 209 210 if (cs->mark_unstable) 211 cs->mark_unstable(cs); 212 213 /* kick clocksource_watchdog_kthread() */ 214 if (finished_booting) 215 schedule_work(&watchdog_work); 216 } 217 218 /** 219 * clocksource_mark_unstable - mark clocksource unstable via watchdog 220 * @cs: clocksource to be marked unstable 221 * 222 * This function is called by the x86 TSC code to mark clocksources as unstable; 223 * it defers demotion and re-selection to a kthread. 224 */ 225 void clocksource_mark_unstable(struct clocksource *cs) 226 { 227 unsigned long flags; 228 229 spin_lock_irqsave(&watchdog_lock, flags); 230 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) { 231 if (!list_empty(&cs->list) && list_empty(&cs->wd_list)) 232 list_add(&cs->wd_list, &watchdog_list); 233 __clocksource_unstable(cs); 234 } 235 spin_unlock_irqrestore(&watchdog_lock, flags); 236 } 237 238 static inline void clocksource_reset_watchdog(void) 239 { 240 struct clocksource *cs; 241 242 list_for_each_entry(cs, &watchdog_list, wd_list) 243 cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 244 } 245 246 enum wd_result { 247 WD_SUCCESS, 248 WD_FREQ_NO_WATCHDOG, 249 WD_FREQ_TIMEOUT, 250 WD_FREQ_RESET, 251 WD_FREQ_SKEWED, 252 WD_CPU_TIMEOUT, 253 WD_CPU_SKEWED, 254 }; 255 256 struct watchdog_cpu_data { 257 /* Keep first as it is 32 byte aligned */ 258 call_single_data_t csd; 259 atomic_t remote_inprogress; 260 enum wd_result result; 261 u64 cpu_ts[2]; 262 struct clocksource *cs; 263 /* Ensure that the sequence is in a separate cache line */ 264 atomic_t seq ____cacheline_aligned; 265 /* Set by the control CPU according to NUMA distance */ 266 u64 timeout_ns; 267 }; 268 269 struct watchdog_data { 270 raw_spinlock_t lock; 271 enum wd_result result; 272 273 u64 wd_seq; 274 u64 wd_delta; 275 u64 cs_delta; 276 u64 cpu_ts[2]; 277 278 unsigned int curr_cpu; 279 } ____cacheline_aligned_in_smp; 280 281 static void watchdog_check_skew_remote(void *unused); 282 283 static DEFINE_PER_CPU_ALIGNED(struct watchdog_cpu_data, watchdog_cpu_data) = { 284 .csd = CSD_INIT(watchdog_check_skew_remote, NULL), 285 }; 286 287 static struct watchdog_data watchdog_data = { 288 .lock = __RAW_SPIN_LOCK_UNLOCKED(watchdog_data.lock), 289 }; 290 291 static inline void watchdog_set_result(struct watchdog_cpu_data *wd, enum wd_result result) 292 { 293 guard(raw_spinlock)(&watchdog_data.lock); 294 if (!wd->result) { 295 atomic_set(&wd->seq, WATCHDOG_REMOTE_MAX_SEQ); 296 WRITE_ONCE(wd->result, result); 297 } 298 } 299 300 /* Wait for the sequence number to hand over control. */ 301 static bool watchdog_wait_seq(struct watchdog_cpu_data *wd, u64 start, int seq) 302 { 303 for(int cnt = 0; atomic_read(&wd->seq) < seq; cnt++) { 304 /* Bail if the other side set an error result */ 305 if (READ_ONCE(wd->result) != WD_SUCCESS) 306 return false; 307 308 /* Prevent endless loops if the other CPU does not react. */ 309 if (cnt == 5000) { 310 u64 nsecs = ktime_get_raw_fast_ns(); 311 312 if (nsecs - start >=wd->timeout_ns) { 313 watchdog_set_result(wd, WD_CPU_TIMEOUT); 314 return false; 315 } 316 cnt = 0; 317 } 318 cpu_relax(); 319 } 320 return seq < WATCHDOG_REMOTE_MAX_SEQ; 321 } 322 323 static void watchdog_check_skew(struct watchdog_cpu_data *wd, int index) 324 { 325 u64 prev, now, delta, start = ktime_get_raw_fast_ns(); 326 int local = index, remote = (index + 1) & 0x1; 327 struct clocksource *cs = wd->cs; 328 329 /* Set the local timestamp so that the first iteration works correctly */ 330 wd->cpu_ts[local] = cs->read(cs); 331 332 /* Signal arrival */ 333 atomic_inc(&wd->seq); 334 335 for (int seq = local + 2; seq < WATCHDOG_REMOTE_MAX_SEQ; seq += 2) { 336 if (!watchdog_wait_seq(wd, start, seq)) 337 return; 338 339 /* Capture local timestamp before possible non-local coherency overhead */ 340 now = cs->read(cs); 341 342 /* Store local timestamp before reading remote to limit coherency stalls */ 343 wd->cpu_ts[local] = now; 344 345 prev = wd->cpu_ts[remote]; 346 delta = (now - prev) & cs->mask; 347 348 if (delta > cs->max_raw_delta) { 349 watchdog_set_result(wd, WD_CPU_SKEWED); 350 return; 351 } 352 353 /* Hand over to the remote CPU */ 354 atomic_inc(&wd->seq); 355 } 356 } 357 358 static void watchdog_check_skew_remote(void *unused) 359 { 360 struct watchdog_cpu_data *wd = this_cpu_ptr(&watchdog_cpu_data); 361 362 atomic_inc(&wd->remote_inprogress); 363 watchdog_check_skew(wd, 1); 364 atomic_dec(&wd->remote_inprogress); 365 } 366 367 static inline bool wd_csd_locked(struct watchdog_cpu_data *wd) 368 { 369 return READ_ONCE(wd->csd.node.u_flags) & CSD_FLAG_LOCK; 370 } 371 372 /* 373 * This is only invoked for remote CPUs. See watchdog_check_cpu_skew(). 374 */ 375 static inline u64 wd_get_remote_timeout(unsigned int remote_cpu) 376 { 377 unsigned int n1, n2; 378 u64 ns; 379 380 if (nr_node_ids == 1) 381 return WATCHDOG_DEFAULT_TIMEOUT_NS; 382 383 n1 = cpu_to_node(smp_processor_id()); 384 n2 = cpu_to_node(remote_cpu); 385 ns = WATCHDOG_NUMA_MULTIPLIER_NS * node_distance(n1, n2); 386 return min(ns, WATCHDOG_NUMA_MAX_TIMEOUT_NS); 387 } 388 389 static void __watchdog_check_cpu_skew(struct clocksource *cs, unsigned int cpu) 390 { 391 struct watchdog_cpu_data *wd; 392 393 wd = per_cpu_ptr(&watchdog_cpu_data, cpu); 394 if (atomic_read(&wd->remote_inprogress) || wd_csd_locked(wd)) { 395 watchdog_data.result = WD_CPU_TIMEOUT; 396 return; 397 } 398 399 atomic_set(&wd->seq, 0); 400 wd->result = WD_SUCCESS; 401 wd->cs = cs; 402 /* Store the current CPU ID for the watchdog test unit */ 403 cs->wd_cpu = smp_processor_id(); 404 405 wd->timeout_ns = wd_get_remote_timeout(cpu); 406 407 /* Kick the remote CPU into the watchdog function */ 408 if (WARN_ON_ONCE(smp_call_function_single_async(cpu, &wd->csd))) { 409 watchdog_data.result = WD_CPU_TIMEOUT; 410 return; 411 } 412 413 scoped_guard(irq) 414 watchdog_check_skew(wd, 0); 415 416 scoped_guard(raw_spinlock_irq, &watchdog_data.lock) { 417 watchdog_data.result = wd->result; 418 memcpy(watchdog_data.cpu_ts, wd->cpu_ts, sizeof(wd->cpu_ts)); 419 } 420 } 421 422 static void watchdog_check_cpu_skew(struct clocksource *cs) 423 { 424 unsigned int cpu = watchdog_data.curr_cpu; 425 426 cpu = cpumask_next_wrap(cpu, cpu_online_mask); 427 watchdog_data.curr_cpu = cpu; 428 429 /* Skip the current CPU. Handles num_online_cpus() == 1 as well */ 430 if (cpu == smp_processor_id()) 431 return; 432 433 /* Don't interfere with the test mechanics */ 434 if ((cs->flags & CLOCK_SOURCE_WDTEST) && !(cs->flags & CLOCK_SOURCE_WDTEST_PERCPU)) 435 return; 436 437 __watchdog_check_cpu_skew(cs, cpu); 438 } 439 440 static bool watchdog_check_freq(struct clocksource *cs, bool reset_pending) 441 { 442 unsigned int ppm_shift = SHIFT_4000PPM; 443 u64 wd_ts0, wd_ts1, cs_ts; 444 445 watchdog_data.result = WD_SUCCESS; 446 if (!watchdog) { 447 watchdog_data.result = WD_FREQ_NO_WATCHDOG; 448 return false; 449 } 450 451 if (cs->flags & CLOCK_SOURCE_WDTEST_PERCPU) 452 return true; 453 454 /* 455 * If both the clocksource and the watchdog claim they are 456 * calibrated use 500ppm limit. Uncalibrated clocksources need a 457 * larger allowance because thefirmware supplied frequencies can be 458 * way off. 459 */ 460 if (watchdog->flags & CLOCK_SOURCE_CALIBRATED && cs->flags & CLOCK_SOURCE_CALIBRATED) 461 ppm_shift = SHIFT_500PPM; 462 463 for (int retries = 0; retries < WATCHDOG_FREQ_RETRIES; retries++) { 464 s64 wd_last, cs_last, wd_seq, wd_delta, cs_delta, max_delta; 465 466 scoped_guard(irq) { 467 wd_ts0 = watchdog->read(watchdog); 468 cs_ts = cs->read(cs); 469 wd_ts1 = watchdog->read(watchdog); 470 } 471 472 wd_last = cs->wd_last; 473 cs_last = cs->cs_last; 474 475 /* Validate the watchdog readout window */ 476 wd_seq = cycles_to_nsec_safe(watchdog, wd_ts0, wd_ts1); 477 if (wd_seq > WATCHDOG_READOUT_MAX_NS) { 478 /* Store for printout in case all retries fail */ 479 watchdog_data.wd_seq = wd_seq; 480 continue; 481 } 482 483 /* Store for subsequent processing */ 484 cs->wd_last = wd_ts0; 485 cs->cs_last = cs_ts; 486 487 /* First round or reset pending? */ 488 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) || reset_pending) 489 goto reset; 490 491 /* Calculate the nanosecond deltas from the last invocation */ 492 wd_delta = cycles_to_nsec_safe(watchdog, wd_last, wd_ts0); 493 cs_delta = cycles_to_nsec_safe(cs, cs_last, cs_ts); 494 495 watchdog_data.wd_delta = wd_delta; 496 watchdog_data.cs_delta = cs_delta; 497 498 /* 499 * Ensure that the deltas are within the readout limits of 500 * the clocksource and the watchdog. Long delays can cause 501 * clocksources to overflow. 502 */ 503 max_delta = max(wd_delta, cs_delta); 504 if (max_delta > cs->max_idle_ns || max_delta > watchdog->max_idle_ns) 505 goto reset; 506 507 /* 508 * Calculate and validate the skew against the allowed PPM 509 * value of the maximum delta plus the watchdog readout 510 * time. 511 */ 512 if (abs(wd_delta - cs_delta) < (max_delta >> ppm_shift) + wd_seq) 513 return true; 514 515 watchdog_data.result = WD_FREQ_SKEWED; 516 return false; 517 } 518 519 watchdog_data.result = WD_FREQ_TIMEOUT; 520 return false; 521 522 reset: 523 cs->flags |= CLOCK_SOURCE_WATCHDOG; 524 watchdog_data.result = WD_FREQ_RESET; 525 return false; 526 } 527 528 /* Synchronization for sched clock */ 529 static void clocksource_tick_stable(struct clocksource *cs) 530 { 531 if (cs == curr_clocksource && cs->tick_stable) 532 cs->tick_stable(cs); 533 } 534 535 /* Conditionaly enable high resolution mode */ 536 static void clocksource_enable_highres(struct clocksource *cs) 537 { 538 if ((cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) || 539 !(cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) || 540 !watchdog || !(watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) 541 return; 542 543 /* Mark it valid for high-res. */ 544 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 545 546 /* 547 * Can't schedule work before finished_booting is 548 * true. clocksource_done_booting will take care of it. 549 */ 550 if (!finished_booting) 551 return; 552 553 if (cs->flags & CLOCK_SOURCE_WDTEST) 554 return; 555 556 /* 557 * If this is not the current clocksource let the watchdog thread 558 * reselect it. Due to the change to high res this clocksource 559 * might be preferred now. If it is the current clocksource let the 560 * tick code know about that change. 561 */ 562 if (cs != curr_clocksource) { 563 cs->flags |= CLOCK_SOURCE_RESELECT; 564 schedule_work(&watchdog_work); 565 } else { 566 tick_clock_notify(); 567 } 568 } 569 570 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 2); 571 572 static void watchdog_print_freq_timeout(struct clocksource *cs) 573 { 574 if (!__ratelimit(&ratelimit_state)) 575 return; 576 pr_info("Watchdog %s read timed out. Readout sequence took: %lluns\n", 577 watchdog->name, watchdog_data.wd_seq); 578 } 579 580 static void watchdog_print_freq_skew(struct clocksource *cs) 581 { 582 pr_warn("Marking clocksource %s unstable due to frequency skew\n", cs->name); 583 pr_warn("Watchdog %20s interval: %16lluns\n", watchdog->name, watchdog_data.wd_delta); 584 pr_warn("Clocksource %20s interval: %16lluns\n", cs->name, watchdog_data.cs_delta); 585 } 586 587 static void watchdog_handle_remote_timeout(struct clocksource *cs) 588 { 589 pr_info_once("Watchdog remote CPU %u read timed out\n", watchdog_data.curr_cpu); 590 } 591 592 static void watchdog_print_remote_skew(struct clocksource *cs) 593 { 594 pr_warn("Marking clocksource %s unstable due to inter CPU skew\n", cs->name); 595 if (watchdog_data.cpu_ts[0] < watchdog_data.cpu_ts[1]) { 596 pr_warn("CPU%u %16llu < CPU%u %16llu (cycles)\n", smp_processor_id(), 597 watchdog_data.cpu_ts[0], watchdog_data.curr_cpu, watchdog_data.cpu_ts[1]); 598 } else { 599 pr_warn("CPU%u %16llu < CPU%u %16llu (cycles)\n", watchdog_data.curr_cpu, 600 watchdog_data.cpu_ts[1], smp_processor_id(), watchdog_data.cpu_ts[0]); 601 } 602 } 603 604 static void watchdog_check_result(struct clocksource *cs) 605 { 606 switch (watchdog_data.result) { 607 case WD_SUCCESS: 608 clocksource_tick_stable(cs); 609 clocksource_enable_highres(cs); 610 return; 611 612 case WD_FREQ_TIMEOUT: 613 watchdog_print_freq_timeout(cs); 614 /* Try again later and invalidate the reference timestamps. */ 615 cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 616 return; 617 618 case WD_FREQ_NO_WATCHDOG: 619 case WD_FREQ_RESET: 620 /* 621 * Nothing to do when the reference timestamps were reset 622 * or no watchdog clocksource registered. 623 */ 624 return; 625 626 case WD_FREQ_SKEWED: 627 watchdog_print_freq_skew(cs); 628 break; 629 630 case WD_CPU_TIMEOUT: 631 /* Remote check timed out. Try again next cycle. */ 632 watchdog_handle_remote_timeout(cs); 633 return; 634 635 case WD_CPU_SKEWED: 636 watchdog_print_remote_skew(cs); 637 break; 638 } 639 __clocksource_unstable(cs); 640 } 641 642 static void clocksource_watchdog(struct timer_list *unused) 643 { 644 struct clocksource *cs; 645 bool reset_pending; 646 647 guard(spinlock)(&watchdog_lock); 648 if (!watchdog_running) 649 return; 650 651 reset_pending = atomic_read(&watchdog_reset_pending); 652 653 list_for_each_entry(cs, &watchdog_list, wd_list) { 654 /* Clocksource already marked unstable? */ 655 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 656 if (finished_booting) 657 schedule_work(&watchdog_work); 658 continue; 659 } 660 661 /* Compare against watchdog clocksource if available */ 662 if (watchdog_check_freq(cs, reset_pending)) { 663 /* Check for inter CPU skew */ 664 watchdog_check_cpu_skew(cs); 665 } 666 667 watchdog_check_result(cs); 668 } 669 670 /* Clear after the full clocksource walk */ 671 if (reset_pending) 672 atomic_dec(&watchdog_reset_pending); 673 674 /* Could have been rearmed by a stop/start cycle */ 675 if (!timer_pending(&watchdog_timer)) { 676 watchdog_timer.expires += WATCHDOG_INTERVAL; 677 add_timer_local(&watchdog_timer); 678 } 679 } 680 681 static inline void clocksource_start_watchdog(void) 682 { 683 if (watchdog_running || list_empty(&watchdog_list)) 684 return; 685 timer_setup(&watchdog_timer, clocksource_watchdog, TIMER_PINNED); 686 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; 687 688 add_timer_on(&watchdog_timer, get_boot_cpu_id()); 689 watchdog_running = 1; 690 } 691 692 static inline void clocksource_stop_watchdog(void) 693 { 694 if (!watchdog_running || !list_empty(&watchdog_list)) 695 return; 696 timer_delete(&watchdog_timer); 697 watchdog_running = 0; 698 } 699 700 static void clocksource_resume_watchdog(void) 701 { 702 atomic_inc(&watchdog_reset_pending); 703 } 704 705 static void clocksource_enqueue_watchdog(struct clocksource *cs) 706 { 707 INIT_LIST_HEAD(&cs->wd_list); 708 709 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 710 /* cs is a clocksource to be watched. */ 711 list_add(&cs->wd_list, &watchdog_list); 712 cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 713 } else { 714 /* cs is a watchdog. */ 715 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 716 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 717 } 718 } 719 720 static void clocksource_select_watchdog(bool fallback) 721 { 722 struct clocksource *cs, *old_wd; 723 unsigned long flags; 724 725 spin_lock_irqsave(&watchdog_lock, flags); 726 /* save current watchdog */ 727 old_wd = watchdog; 728 if (fallback) 729 watchdog = NULL; 730 731 list_for_each_entry(cs, &clocksource_list, list) { 732 /* cs is a clocksource to be watched. */ 733 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) 734 continue; 735 736 /* 737 * If it's not continuous, don't put the fox in charge of 738 * the henhouse. 739 */ 740 if (!(cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)) 741 continue; 742 743 /* Skip current if we were requested for a fallback. */ 744 if (fallback && cs == old_wd) 745 continue; 746 747 /* Pick the best watchdog. */ 748 if (!watchdog || cs->rating > watchdog->rating) 749 watchdog = cs; 750 } 751 /* If we failed to find a fallback restore the old one. */ 752 if (!watchdog) 753 watchdog = old_wd; 754 755 /* If we changed the watchdog we need to reset cycles. */ 756 if (watchdog != old_wd) 757 clocksource_reset_watchdog(); 758 759 /* Check if the watchdog timer needs to be started. */ 760 clocksource_start_watchdog(); 761 spin_unlock_irqrestore(&watchdog_lock, flags); 762 } 763 764 static void clocksource_dequeue_watchdog(struct clocksource *cs) 765 { 766 if (cs != watchdog) { 767 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 768 /* cs is a watched clocksource. */ 769 list_del_init(&cs->wd_list); 770 /* Check if the watchdog timer needs to be stopped. */ 771 clocksource_stop_watchdog(); 772 } 773 } 774 } 775 776 static int __clocksource_watchdog_kthread(void) 777 { 778 struct clocksource *cs, *tmp; 779 unsigned long flags; 780 int select = 0; 781 782 spin_lock_irqsave(&watchdog_lock, flags); 783 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { 784 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 785 list_del_init(&cs->wd_list); 786 clocksource_change_rating(cs, 0); 787 select = 1; 788 } 789 if (cs->flags & CLOCK_SOURCE_RESELECT) { 790 cs->flags &= ~CLOCK_SOURCE_RESELECT; 791 select = 1; 792 } 793 } 794 /* Check if the watchdog timer needs to be stopped. */ 795 clocksource_stop_watchdog(); 796 spin_unlock_irqrestore(&watchdog_lock, flags); 797 798 return select; 799 } 800 801 static int clocksource_watchdog_kthread(void *data) 802 { 803 mutex_lock(&clocksource_mutex); 804 if (__clocksource_watchdog_kthread()) 805 clocksource_select(); 806 mutex_unlock(&clocksource_mutex); 807 return 0; 808 } 809 810 static bool clocksource_is_watchdog(struct clocksource *cs) 811 { 812 return cs == watchdog; 813 } 814 815 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ 816 817 static void clocksource_enqueue_watchdog(struct clocksource *cs) 818 { 819 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 820 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 821 } 822 823 static void clocksource_select_watchdog(bool fallback) { } 824 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } 825 static inline void clocksource_resume_watchdog(void) { } 826 static inline int __clocksource_watchdog_kthread(void) { return 0; } 827 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; } 828 void clocksource_mark_unstable(struct clocksource *cs) { } 829 830 static inline void clocksource_watchdog_lock(unsigned long *flags) { } 831 static inline void clocksource_watchdog_unlock(unsigned long *flags) { } 832 833 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ 834 835 static bool clocksource_is_suspend(struct clocksource *cs) 836 { 837 return cs == suspend_clocksource; 838 } 839 840 static void __clocksource_suspend_select(struct clocksource *cs) 841 { 842 /* 843 * Skip the clocksource which will be stopped in suspend state. 844 */ 845 if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP)) 846 return; 847 848 /* 849 * The nonstop clocksource can be selected as the suspend clocksource to 850 * calculate the suspend time, so it should not supply suspend/resume 851 * interfaces to suspend the nonstop clocksource when system suspends. 852 */ 853 if (cs->suspend || cs->resume) { 854 pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n", 855 cs->name); 856 } 857 858 /* Pick the best rating. */ 859 if (!suspend_clocksource || cs->rating > suspend_clocksource->rating) 860 suspend_clocksource = cs; 861 } 862 863 /** 864 * clocksource_suspend_select - Select the best clocksource for suspend timing 865 * @fallback: if select a fallback clocksource 866 */ 867 static void clocksource_suspend_select(bool fallback) 868 { 869 struct clocksource *cs, *old_suspend; 870 871 old_suspend = suspend_clocksource; 872 if (fallback) 873 suspend_clocksource = NULL; 874 875 list_for_each_entry(cs, &clocksource_list, list) { 876 /* Skip current if we were requested for a fallback. */ 877 if (fallback && cs == old_suspend) 878 continue; 879 880 __clocksource_suspend_select(cs); 881 } 882 } 883 884 /** 885 * clocksource_start_suspend_timing - Start measuring the suspend timing 886 * @cs: current clocksource from timekeeping 887 * @start_cycles: current cycles from timekeeping 888 * 889 * This function will save the start cycle values of suspend timer to calculate 890 * the suspend time when resuming system. 891 * 892 * This function is called late in the suspend process from timekeeping_suspend(), 893 * that means processes are frozen, non-boot cpus and interrupts are disabled 894 * now. It is therefore possible to start the suspend timer without taking the 895 * clocksource mutex. 896 */ 897 void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles) 898 { 899 if (!suspend_clocksource) 900 return; 901 902 /* 903 * If current clocksource is the suspend timer, we should use the 904 * tkr_mono.cycle_last value as suspend_start to avoid same reading 905 * from suspend timer. 906 */ 907 if (clocksource_is_suspend(cs)) { 908 suspend_start = start_cycles; 909 return; 910 } 911 912 if (suspend_clocksource->enable && 913 suspend_clocksource->enable(suspend_clocksource)) { 914 pr_warn_once("Failed to enable the non-suspend-able clocksource.\n"); 915 return; 916 } 917 918 suspend_start = suspend_clocksource->read(suspend_clocksource); 919 } 920 921 /** 922 * clocksource_stop_suspend_timing - Stop measuring the suspend timing 923 * @cs: current clocksource from timekeeping 924 * @cycle_now: current cycles from timekeeping 925 * 926 * This function will calculate the suspend time from suspend timer. 927 * 928 * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource. 929 * 930 * This function is called early in the resume process from timekeeping_resume(), 931 * that means there is only one cpu, no processes are running and the interrupts 932 * are disabled. It is therefore possible to stop the suspend timer without 933 * taking the clocksource mutex. 934 */ 935 u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now) 936 { 937 u64 now, nsec = 0; 938 939 if (!suspend_clocksource) 940 return 0; 941 942 /* 943 * If current clocksource is the suspend timer, we should use the 944 * tkr_mono.cycle_last value from timekeeping as current cycle to 945 * avoid same reading from suspend timer. 946 */ 947 if (clocksource_is_suspend(cs)) 948 now = cycle_now; 949 else 950 now = suspend_clocksource->read(suspend_clocksource); 951 952 if (now > suspend_start) 953 nsec = cycles_to_nsec_safe(suspend_clocksource, suspend_start, now); 954 955 /* 956 * Disable the suspend timer to save power if current clocksource is 957 * not the suspend timer. 958 */ 959 if (!clocksource_is_suspend(cs) && suspend_clocksource->disable) 960 suspend_clocksource->disable(suspend_clocksource); 961 962 return nsec; 963 } 964 965 /** 966 * clocksource_suspend - suspend the clocksource(s) 967 */ 968 void clocksource_suspend(void) 969 { 970 struct clocksource *cs; 971 972 list_for_each_entry_reverse(cs, &clocksource_list, list) 973 if (cs->suspend) 974 cs->suspend(cs); 975 } 976 977 /** 978 * clocksource_resume - resume the clocksource(s) 979 */ 980 void clocksource_resume(void) 981 { 982 struct clocksource *cs; 983 984 list_for_each_entry(cs, &clocksource_list, list) 985 if (cs->resume) 986 cs->resume(cs); 987 988 clocksource_resume_watchdog(); 989 } 990 991 /** 992 * clocksource_touch_watchdog - Update watchdog 993 * 994 * Update the watchdog after exception contexts such as kgdb so as not 995 * to incorrectly trip the watchdog. This might fail when the kernel 996 * was stopped in code which holds watchdog_lock. 997 */ 998 void clocksource_touch_watchdog(void) 999 { 1000 clocksource_resume_watchdog(); 1001 } 1002 1003 /** 1004 * clocksource_max_adjustment- Returns max adjustment amount 1005 * @cs: Pointer to clocksource 1006 * 1007 */ 1008 static u32 clocksource_max_adjustment(struct clocksource *cs) 1009 { 1010 u64 ret; 1011 /* 1012 * We won't try to correct for more than 11% adjustments (110,000 ppm), 1013 */ 1014 ret = (u64)cs->mult * 11; 1015 do_div(ret,100); 1016 return (u32)ret; 1017 } 1018 1019 /** 1020 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted 1021 * @mult: cycle to nanosecond multiplier 1022 * @shift: cycle to nanosecond divisor (power of two) 1023 * @maxadj: maximum adjustment value to mult (~11%) 1024 * @mask: bitmask for two's complement subtraction of non 64 bit counters 1025 * @max_cyc: maximum cycle value before potential overflow (does not include 1026 * any safety margin) 1027 * 1028 * NOTE: This function includes a safety margin of 50%, in other words, we 1029 * return half the number of nanoseconds the hardware counter can technically 1030 * cover. This is done so that we can potentially detect problems caused by 1031 * delayed timers or bad hardware, which might result in time intervals that 1032 * are larger than what the math used can handle without overflows. 1033 */ 1034 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc) 1035 { 1036 u64 max_nsecs, max_cycles; 1037 1038 /* 1039 * Calculate the maximum number of cycles that we can pass to the 1040 * cyc2ns() function without overflowing a 64-bit result. 1041 */ 1042 max_cycles = ULLONG_MAX; 1043 do_div(max_cycles, mult+maxadj); 1044 1045 /* 1046 * The actual maximum number of cycles we can defer the clocksource is 1047 * determined by the minimum of max_cycles and mask. 1048 * Note: Here we subtract the maxadj to make sure we don't sleep for 1049 * too long if there's a large negative adjustment. 1050 */ 1051 max_cycles = min(max_cycles, mask); 1052 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift); 1053 1054 /* return the max_cycles value as well if requested */ 1055 if (max_cyc) 1056 *max_cyc = max_cycles; 1057 1058 /* Return 50% of the actual maximum, so we can detect bad values */ 1059 max_nsecs >>= 1; 1060 1061 return max_nsecs; 1062 } 1063 1064 /** 1065 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles 1066 * @cs: Pointer to clocksource to be updated 1067 * 1068 */ 1069 static inline void clocksource_update_max_deferment(struct clocksource *cs) 1070 { 1071 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift, 1072 cs->maxadj, cs->mask, 1073 &cs->max_cycles); 1074 1075 /* 1076 * Threshold for detecting negative motion in clocksource_delta(). 1077 * 1078 * Allow for 0.875 of the counter width so that overly long idle 1079 * sleeps, which go slightly over mask/2, do not trigger the 1080 * negative motion detection. 1081 */ 1082 cs->max_raw_delta = (cs->mask >> 1) + (cs->mask >> 2) + (cs->mask >> 3); 1083 } 1084 1085 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) 1086 { 1087 struct clocksource *cs; 1088 1089 if (!finished_booting || list_empty(&clocksource_list)) 1090 return NULL; 1091 1092 /* 1093 * We pick the clocksource with the highest rating. If oneshot 1094 * mode is active, we pick the highres valid clocksource with 1095 * the best rating. 1096 */ 1097 list_for_each_entry(cs, &clocksource_list, list) { 1098 if (skipcur && cs == curr_clocksource) 1099 continue; 1100 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 1101 continue; 1102 if (cs->flags & CLOCK_SOURCE_WDTEST) 1103 continue; 1104 return cs; 1105 } 1106 return NULL; 1107 } 1108 1109 static void __clocksource_select(bool skipcur) 1110 { 1111 bool oneshot = tick_oneshot_mode_active(); 1112 struct clocksource *best, *cs; 1113 1114 /* Find the best suitable clocksource */ 1115 best = clocksource_find_best(oneshot, skipcur); 1116 if (!best) 1117 return; 1118 1119 if (!strlen(override_name)) 1120 goto found; 1121 1122 /* Check for the override clocksource. */ 1123 list_for_each_entry(cs, &clocksource_list, list) { 1124 if (skipcur && cs == curr_clocksource) 1125 continue; 1126 if (strcmp(cs->name, override_name) != 0) 1127 continue; 1128 if (cs->flags & CLOCK_SOURCE_WDTEST) 1129 continue; 1130 /* 1131 * Check to make sure we don't switch to a non-highres 1132 * capable clocksource if the tick code is in oneshot 1133 * mode (highres or nohz) 1134 */ 1135 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { 1136 /* Override clocksource cannot be used. */ 1137 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 1138 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n", 1139 cs->name); 1140 override_name[0] = 0; 1141 } else { 1142 /* 1143 * The override cannot be currently verified. 1144 * Deferring to let the watchdog check. 1145 */ 1146 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n", 1147 cs->name); 1148 } 1149 } else 1150 /* Override clocksource can be used. */ 1151 best = cs; 1152 break; 1153 } 1154 1155 found: 1156 if (curr_clocksource != best && !timekeeping_notify(best)) { 1157 pr_info("Switched to clocksource %s\n", best->name); 1158 curr_clocksource = best; 1159 } 1160 } 1161 1162 /** 1163 * clocksource_select - Select the best clocksource available 1164 * 1165 * Private function. Must hold clocksource_mutex when called. 1166 * 1167 * Select the clocksource with the best rating, or the clocksource, 1168 * which is selected by userspace override. 1169 */ 1170 static void clocksource_select(void) 1171 { 1172 __clocksource_select(false); 1173 } 1174 1175 static void clocksource_select_fallback(void) 1176 { 1177 __clocksource_select(true); 1178 } 1179 1180 /* 1181 * clocksource_done_booting - Called near the end of core bootup 1182 * 1183 * Hack to avoid lots of clocksource churn at boot time. 1184 * We use fs_initcall because we want this to start before 1185 * device_initcall but after subsys_initcall. 1186 */ 1187 static int __init clocksource_done_booting(void) 1188 { 1189 mutex_lock(&clocksource_mutex); 1190 curr_clocksource = clocksource_default_clock(); 1191 finished_booting = 1; 1192 /* 1193 * Run the watchdog first to eliminate unstable clock sources 1194 */ 1195 __clocksource_watchdog_kthread(); 1196 clocksource_select(); 1197 mutex_unlock(&clocksource_mutex); 1198 return 0; 1199 } 1200 fs_initcall(clocksource_done_booting); 1201 1202 /* 1203 * Enqueue the clocksource sorted by rating 1204 */ 1205 static void clocksource_enqueue(struct clocksource *cs) 1206 { 1207 struct list_head *entry = &clocksource_list; 1208 struct clocksource *tmp; 1209 1210 list_for_each_entry(tmp, &clocksource_list, list) { 1211 /* Keep track of the place, where to insert */ 1212 if (tmp->rating < cs->rating) 1213 break; 1214 entry = &tmp->list; 1215 } 1216 list_add(&cs->list, entry); 1217 } 1218 1219 /** 1220 * __clocksource_update_freq_scale - Used update clocksource with new freq 1221 * @cs: clocksource to be registered 1222 * @scale: Scale factor multiplied against freq to get clocksource hz 1223 * @freq: clocksource frequency (cycles per second) divided by scale 1224 */ 1225 static void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq) 1226 { 1227 u64 sec; 1228 1229 /* 1230 * Default clocksources are *special* and self-define their mult/shift. 1231 * But, you're not special, so you should specify a freq value. 1232 */ 1233 if (freq) { 1234 /* 1235 * Calc the maximum number of seconds which we can run before 1236 * wrapping around. For clocksources which have a mask > 32-bit 1237 * we need to limit the max sleep time to have a good 1238 * conversion precision. 10 minutes is still a reasonable 1239 * amount. That results in a shift value of 24 for a 1240 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to 1241 * ~ 0.06ppm granularity for NTP. 1242 */ 1243 sec = cs->mask; 1244 do_div(sec, freq); 1245 do_div(sec, scale); 1246 if (!sec) 1247 sec = 1; 1248 else if (sec > 600 && cs->mask > UINT_MAX) 1249 sec = 600; 1250 1251 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 1252 NSEC_PER_SEC / scale, sec * scale); 1253 1254 /* Update cs::freq_khz */ 1255 cs->freq_khz = div_u64((u64)freq * scale, 1000); 1256 } 1257 1258 /* 1259 * Ensure clocksources that have large 'mult' values don't overflow 1260 * when adjusted. 1261 */ 1262 cs->maxadj = clocksource_max_adjustment(cs); 1263 while (freq && ((cs->mult + cs->maxadj < cs->mult) 1264 || (cs->mult - cs->maxadj > cs->mult))) { 1265 cs->mult >>= 1; 1266 cs->shift--; 1267 cs->maxadj = clocksource_max_adjustment(cs); 1268 } 1269 1270 /* 1271 * Only warn for *special* clocksources that self-define 1272 * their mult/shift values and don't specify a freq. 1273 */ 1274 WARN_ONCE(cs->mult + cs->maxadj < cs->mult, 1275 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n", 1276 cs->name); 1277 1278 clocksource_update_max_deferment(cs); 1279 1280 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n", 1281 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns); 1282 } 1283 1284 /** 1285 * __clocksource_register_scale - Used to install new clocksources 1286 * @cs: clocksource to be registered 1287 * @scale: Scale factor multiplied against freq to get clocksource hz 1288 * @freq: clocksource frequency (cycles per second) divided by scale 1289 * 1290 * Returns -EBUSY if registration fails, zero otherwise. 1291 * 1292 * This *SHOULD NOT* be called directly! Please use the 1293 * clocksource_register_hz() or clocksource_register_khz helper functions. 1294 */ 1295 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) 1296 { 1297 unsigned long flags; 1298 1299 clocksource_arch_init(cs); 1300 1301 if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX)) 1302 cs->id = CSID_GENERIC; 1303 1304 if (WARN_ON_ONCE(!freq && cs->flags & CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT)) 1305 cs->flags &= ~CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT; 1306 1307 if (cs->vdso_clock_mode < 0 || 1308 cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) { 1309 pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n", 1310 cs->name, cs->vdso_clock_mode); 1311 cs->vdso_clock_mode = VDSO_CLOCKMODE_NONE; 1312 } 1313 1314 /* Initialize mult/shift and max_idle_ns */ 1315 __clocksource_update_freq_scale(cs, scale, freq); 1316 1317 /* Add clocksource to the clocksource list */ 1318 mutex_lock(&clocksource_mutex); 1319 1320 clocksource_watchdog_lock(&flags); 1321 clocksource_enqueue(cs); 1322 clocksource_enqueue_watchdog(cs); 1323 clocksource_watchdog_unlock(&flags); 1324 1325 clocksource_select(); 1326 clocksource_select_watchdog(false); 1327 __clocksource_suspend_select(cs); 1328 mutex_unlock(&clocksource_mutex); 1329 return 0; 1330 } 1331 EXPORT_SYMBOL_GPL(__clocksource_register_scale); 1332 1333 static void __devm_clocksource_unregister(void *data) 1334 { 1335 struct clocksource *cs = data; 1336 1337 clocksource_unregister(cs); 1338 } 1339 1340 int __devm_clocksource_register_scale(struct device *dev, struct clocksource *cs, 1341 u32 scale, u32 freq) 1342 { 1343 int ret; 1344 1345 ret = __clocksource_register_scale(cs, scale, freq); 1346 if (ret) 1347 return ret; 1348 1349 return devm_add_action_or_reset(dev, __devm_clocksource_unregister, cs); 1350 } 1351 EXPORT_SYMBOL_GPL(__devm_clocksource_register_scale); 1352 1353 /* 1354 * Unbind clocksource @cs. Called with clocksource_mutex held 1355 */ 1356 static int clocksource_unbind(struct clocksource *cs) 1357 { 1358 unsigned long flags; 1359 1360 if (clocksource_is_watchdog(cs)) { 1361 /* Select and try to install a replacement watchdog. */ 1362 clocksource_select_watchdog(true); 1363 if (clocksource_is_watchdog(cs)) 1364 return -EBUSY; 1365 } 1366 1367 if (cs == curr_clocksource) { 1368 /* Select and try to install a replacement clock source */ 1369 clocksource_select_fallback(); 1370 if (curr_clocksource == cs) 1371 return -EBUSY; 1372 } 1373 1374 if (clocksource_is_suspend(cs)) { 1375 /* 1376 * Select and try to install a replacement suspend clocksource. 1377 * If no replacement suspend clocksource, we will just let the 1378 * clocksource go and have no suspend clocksource. 1379 */ 1380 clocksource_suspend_select(true); 1381 } 1382 1383 clocksource_watchdog_lock(&flags); 1384 clocksource_dequeue_watchdog(cs); 1385 list_del_init(&cs->list); 1386 clocksource_watchdog_unlock(&flags); 1387 1388 return 0; 1389 } 1390 1391 /** 1392 * clocksource_unregister - remove a registered clocksource 1393 * @cs: clocksource to be unregistered 1394 */ 1395 int clocksource_unregister(struct clocksource *cs) 1396 { 1397 int ret = 0; 1398 1399 mutex_lock(&clocksource_mutex); 1400 if (!list_empty(&cs->list)) 1401 ret = clocksource_unbind(cs); 1402 mutex_unlock(&clocksource_mutex); 1403 return ret; 1404 } 1405 EXPORT_SYMBOL(clocksource_unregister); 1406 1407 #ifdef CONFIG_SYSFS 1408 /** 1409 * current_clocksource_show - sysfs interface for current clocksource 1410 * @dev: unused 1411 * @attr: unused 1412 * @buf: char buffer to be filled with clocksource list 1413 * 1414 * Provides sysfs interface for listing current clocksource. 1415 */ 1416 static ssize_t current_clocksource_show(struct device *dev, 1417 struct device_attribute *attr, 1418 char *buf) 1419 { 1420 ssize_t count = 0; 1421 1422 mutex_lock(&clocksource_mutex); 1423 count = sysfs_emit(buf, "%s\n", curr_clocksource->name); 1424 mutex_unlock(&clocksource_mutex); 1425 1426 return count; 1427 } 1428 1429 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt) 1430 { 1431 size_t ret = cnt; 1432 1433 /* strings from sysfs write are not 0 terminated! */ 1434 if (!cnt || cnt >= CS_NAME_LEN) 1435 return -EINVAL; 1436 1437 /* strip of \n: */ 1438 if (buf[cnt-1] == '\n') 1439 cnt--; 1440 if (cnt > 0) 1441 memcpy(dst, buf, cnt); 1442 dst[cnt] = 0; 1443 return ret; 1444 } 1445 1446 /** 1447 * current_clocksource_store - interface for manually overriding clocksource 1448 * @dev: unused 1449 * @attr: unused 1450 * @buf: name of override clocksource 1451 * @count: length of buffer 1452 * 1453 * Takes input from sysfs interface for manually overriding the default 1454 * clocksource selection. 1455 */ 1456 static ssize_t current_clocksource_store(struct device *dev, 1457 struct device_attribute *attr, 1458 const char *buf, size_t count) 1459 { 1460 ssize_t ret; 1461 1462 mutex_lock(&clocksource_mutex); 1463 1464 ret = sysfs_get_uname(buf, override_name, count); 1465 if (ret >= 0) 1466 clocksource_select(); 1467 1468 mutex_unlock(&clocksource_mutex); 1469 1470 return ret; 1471 } 1472 static DEVICE_ATTR_RW(current_clocksource); 1473 1474 /** 1475 * unbind_clocksource_store - interface for manually unbinding clocksource 1476 * @dev: unused 1477 * @attr: unused 1478 * @buf: unused 1479 * @count: length of buffer 1480 * 1481 * Takes input from sysfs interface for manually unbinding a clocksource. 1482 */ 1483 static ssize_t unbind_clocksource_store(struct device *dev, 1484 struct device_attribute *attr, 1485 const char *buf, size_t count) 1486 { 1487 struct clocksource *cs; 1488 char name[CS_NAME_LEN]; 1489 ssize_t ret; 1490 1491 ret = sysfs_get_uname(buf, name, count); 1492 if (ret < 0) 1493 return ret; 1494 1495 ret = -ENODEV; 1496 mutex_lock(&clocksource_mutex); 1497 list_for_each_entry(cs, &clocksource_list, list) { 1498 if (strcmp(cs->name, name)) 1499 continue; 1500 ret = clocksource_unbind(cs); 1501 break; 1502 } 1503 mutex_unlock(&clocksource_mutex); 1504 1505 return ret ? ret : count; 1506 } 1507 static DEVICE_ATTR_WO(unbind_clocksource); 1508 1509 /** 1510 * available_clocksource_show - sysfs interface for listing clocksource 1511 * @dev: unused 1512 * @attr: unused 1513 * @buf: char buffer to be filled with clocksource list 1514 * 1515 * Provides sysfs interface for listing registered clocksources 1516 */ 1517 static ssize_t available_clocksource_show(struct device *dev, 1518 struct device_attribute *attr, 1519 char *buf) 1520 { 1521 struct clocksource *src; 1522 ssize_t count = 0; 1523 1524 mutex_lock(&clocksource_mutex); 1525 list_for_each_entry(src, &clocksource_list, list) { 1526 /* 1527 * Don't show non-HRES clocksource if the tick code is 1528 * in one shot mode (highres=on or nohz=on) 1529 */ 1530 if (!tick_oneshot_mode_active() || 1531 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 1532 count += snprintf(buf + count, 1533 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), 1534 "%s ", src->name); 1535 } 1536 mutex_unlock(&clocksource_mutex); 1537 1538 count += snprintf(buf + count, 1539 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); 1540 1541 return count; 1542 } 1543 static DEVICE_ATTR_RO(available_clocksource); 1544 1545 static struct attribute *clocksource_attrs[] = { 1546 &dev_attr_current_clocksource.attr, 1547 &dev_attr_unbind_clocksource.attr, 1548 &dev_attr_available_clocksource.attr, 1549 NULL 1550 }; 1551 ATTRIBUTE_GROUPS(clocksource); 1552 1553 static const struct bus_type clocksource_subsys = { 1554 .name = "clocksource", 1555 .dev_name = "clocksource", 1556 }; 1557 1558 static struct device device_clocksource = { 1559 .id = 0, 1560 .bus = &clocksource_subsys, 1561 .groups = clocksource_groups, 1562 }; 1563 1564 static int __init init_clocksource_sysfs(void) 1565 { 1566 int error = subsys_system_register(&clocksource_subsys, NULL); 1567 1568 if (error) 1569 return error; 1570 1571 error = device_register(&device_clocksource); 1572 if (error) 1573 bus_unregister(&clocksource_subsys); 1574 1575 return error; 1576 } 1577 1578 device_initcall(init_clocksource_sysfs); 1579 #endif /* CONFIG_SYSFS */ 1580 1581 /** 1582 * boot_override_clocksource - boot clock override 1583 * @str: override name 1584 * 1585 * Takes a clocksource= boot argument and uses it 1586 * as the clocksource override name. 1587 */ 1588 static int __init boot_override_clocksource(char* str) 1589 { 1590 mutex_lock(&clocksource_mutex); 1591 if (str) 1592 strscpy(override_name, str); 1593 mutex_unlock(&clocksource_mutex); 1594 return 1; 1595 } 1596 1597 __setup("clocksource=", boot_override_clocksource); 1598 1599 /** 1600 * boot_override_clock - Compatibility layer for deprecated boot option 1601 * @str: override name 1602 * 1603 * DEPRECATED! Takes a clock= boot argument and uses it 1604 * as the clocksource override name 1605 */ 1606 static int __init boot_override_clock(char* str) 1607 { 1608 if (!strcmp(str, "pmtmr")) { 1609 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n"); 1610 return boot_override_clocksource("acpi_pm"); 1611 } 1612 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n"); 1613 return boot_override_clocksource(str); 1614 } 1615 1616 __setup("clock=", boot_override_clock); 1617