1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Common time routines among all ppc machines. 4 * 5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge 6 * Paul Mackerras' version and mine for PReP and Pmac. 7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). 8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) 9 * 10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es) 11 * to make clock more stable (2.4.0-test5). The only thing 12 * that this code assumes is that the timebases have been synchronized 13 * by firmware on SMP and are never stopped (never do sleep 14 * on SMP then, nap and doze are OK). 15 * 16 * Speeded up do_gettimeofday by getting rid of references to 17 * xtime (which required locks for consistency). (mikejc@us.ibm.com) 18 * 19 * TODO (not necessarily in this file): 20 * - improve precision and reproducibility of timebase frequency 21 * measurement at boot time. 22 * - for astronomical applications: add a new function to get 23 * non ambiguous timestamps even around leap seconds. This needs 24 * a new timestamp format and a good name. 25 * 26 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 27 * "A Kernel Model for Precision Timekeeping" by Dave Mills 28 */ 29 30 #include <linux/errno.h> 31 #include <linux/export.h> 32 #include <linux/sched.h> 33 #include <linux/sched/clock.h> 34 #include <linux/sched/cputime.h> 35 #include <linux/kernel.h> 36 #include <linux/param.h> 37 #include <linux/string.h> 38 #include <linux/mm.h> 39 #include <linux/interrupt.h> 40 #include <linux/timex.h> 41 #include <linux/kernel_stat.h> 42 #include <linux/time.h> 43 #include <linux/init.h> 44 #include <linux/profile.h> 45 #include <linux/cpu.h> 46 #include <linux/security.h> 47 #include <linux/percpu.h> 48 #include <linux/rtc.h> 49 #include <linux/jiffies.h> 50 #include <linux/posix-timers.h> 51 #include <linux/irq.h> 52 #include <linux/delay.h> 53 #include <linux/irq_work.h> 54 #include <linux/of_clk.h> 55 #include <linux/suspend.h> 56 #include <linux/processor.h> 57 #include <linux/mc146818rtc.h> 58 #include <linux/platform_device.h> 59 60 #include <asm/trace.h> 61 #include <asm/interrupt.h> 62 #include <asm/io.h> 63 #include <asm/nvram.h> 64 #include <asm/cache.h> 65 #include <asm/machdep.h> 66 #include <linux/uaccess.h> 67 #include <asm/time.h> 68 #include <asm/irq.h> 69 #include <asm/div64.h> 70 #include <asm/smp.h> 71 #include <asm/vdso_datapage.h> 72 #include <asm/firmware.h> 73 #include <asm/mce.h> 74 75 /* powerpc clocksource/clockevent code */ 76 77 #include <linux/clockchips.h> 78 79 static u64 timebase_read(struct clocksource *); 80 static struct clocksource clocksource_timebase = { 81 .name = "timebase", 82 .rating = 400, 83 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 84 .mask = CLOCKSOURCE_MASK(64), 85 .read = timebase_read, 86 .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER, 87 }; 88 89 #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 90 u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 91 EXPORT_SYMBOL_GPL(decrementer_max); /* for KVM HDEC */ 92 93 static int decrementer_set_next_event(unsigned long evt, 94 struct clock_event_device *dev); 95 static int decrementer_shutdown(struct clock_event_device *evt); 96 97 struct clock_event_device decrementer_clockevent = { 98 .name = "decrementer", 99 .rating = 200, 100 .irq = 0, 101 .set_next_event = decrementer_set_next_event, 102 .set_state_oneshot_stopped = decrementer_shutdown, 103 .set_state_shutdown = decrementer_shutdown, 104 .tick_resume = decrementer_shutdown, 105 .features = CLOCK_EVT_FEAT_ONESHOT | 106 CLOCK_EVT_FEAT_C3STOP, 107 }; 108 EXPORT_SYMBOL(decrementer_clockevent); 109 110 /* 111 * This always puts next_tb beyond now, so the clock event will never fire 112 * with the usual comparison, no need for a separate test for stopped. 113 */ 114 #define DEC_CLOCKEVENT_STOPPED ~0ULL 115 DEFINE_PER_CPU(u64, decrementers_next_tb) = DEC_CLOCKEVENT_STOPPED; 116 EXPORT_SYMBOL_GPL(decrementers_next_tb); 117 static DEFINE_PER_CPU(struct clock_event_device, decrementers); 118 119 #define XSEC_PER_SEC (1024*1024) 120 121 #ifdef CONFIG_PPC64 122 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 123 #else 124 /* compute ((xsec << 12) * max) >> 32 */ 125 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 126 #endif 127 128 unsigned long tb_ticks_per_jiffy; 129 unsigned long tb_ticks_per_usec = 100; /* sane default */ 130 EXPORT_SYMBOL(tb_ticks_per_usec); 131 unsigned long tb_ticks_per_sec; 132 EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime conversions */ 133 134 DEFINE_SPINLOCK(rtc_lock); 135 EXPORT_SYMBOL_GPL(rtc_lock); 136 137 static u64 tb_to_ns_scale __read_mostly; 138 static unsigned tb_to_ns_shift __read_mostly; 139 static u64 boot_tb __read_mostly; 140 141 extern struct timezone sys_tz; 142 static long timezone_offset; 143 144 unsigned long ppc_proc_freq; 145 EXPORT_SYMBOL_GPL(ppc_proc_freq); 146 unsigned long ppc_tb_freq; 147 EXPORT_SYMBOL_GPL(ppc_tb_freq); 148 149 bool tb_invalid; 150 151 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 152 /* 153 * Read the SPURR on systems that have it, otherwise the PURR, 154 * or if that doesn't exist return the timebase value passed in. 155 */ 156 static inline unsigned long read_spurr(unsigned long tb) 157 { 158 if (cpu_has_feature(CPU_FTR_SPURR)) 159 return mfspr(SPRN_SPURR); 160 if (cpu_has_feature(CPU_FTR_PURR)) 161 return mfspr(SPRN_PURR); 162 return tb; 163 } 164 165 /* 166 * Account time for a transition between system, hard irq 167 * or soft irq state. 168 */ 169 static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct, 170 unsigned long now, unsigned long stime) 171 { 172 unsigned long stime_scaled = 0; 173 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 174 unsigned long nowscaled, deltascaled; 175 unsigned long utime, utime_scaled; 176 177 nowscaled = read_spurr(now); 178 deltascaled = nowscaled - acct->startspurr; 179 acct->startspurr = nowscaled; 180 utime = acct->utime - acct->utime_sspurr; 181 acct->utime_sspurr = acct->utime; 182 183 /* 184 * Because we don't read the SPURR on every kernel entry/exit, 185 * deltascaled includes both user and system SPURR ticks. 186 * Apportion these ticks to system SPURR ticks and user 187 * SPURR ticks in the same ratio as the system time (delta) 188 * and user time (udelta) values obtained from the timebase 189 * over the same interval. The system ticks get accounted here; 190 * the user ticks get saved up in paca->user_time_scaled to be 191 * used by account_process_tick. 192 */ 193 stime_scaled = stime; 194 utime_scaled = utime; 195 if (deltascaled != stime + utime) { 196 if (utime) { 197 stime_scaled = deltascaled * stime / (stime + utime); 198 utime_scaled = deltascaled - stime_scaled; 199 } else { 200 stime_scaled = deltascaled; 201 } 202 } 203 acct->utime_scaled += utime_scaled; 204 #endif 205 206 return stime_scaled; 207 } 208 209 static unsigned long vtime_delta(struct cpu_accounting_data *acct, 210 unsigned long *stime_scaled, 211 unsigned long *steal_time) 212 { 213 unsigned long now, stime; 214 215 WARN_ON_ONCE(!irqs_disabled()); 216 217 now = mftb(); 218 stime = now - acct->starttime; 219 acct->starttime = now; 220 221 *stime_scaled = vtime_delta_scaled(acct, now, stime); 222 223 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && 224 firmware_has_feature(FW_FEATURE_SPLPAR)) 225 *steal_time = pseries_calculate_stolen_time(now); 226 else 227 *steal_time = 0; 228 229 return stime; 230 } 231 232 static void vtime_delta_kernel(struct cpu_accounting_data *acct, 233 unsigned long *stime, unsigned long *stime_scaled) 234 { 235 unsigned long steal_time; 236 237 *stime = vtime_delta(acct, stime_scaled, &steal_time); 238 *stime -= min(*stime, steal_time); 239 acct->steal_time += steal_time; 240 } 241 242 void vtime_account_kernel(struct task_struct *tsk) 243 { 244 struct cpu_accounting_data *acct = get_accounting(tsk); 245 unsigned long stime, stime_scaled; 246 247 vtime_delta_kernel(acct, &stime, &stime_scaled); 248 249 if (tsk->flags & PF_VCPU) { 250 acct->gtime += stime; 251 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 252 acct->utime_scaled += stime_scaled; 253 #endif 254 } else { 255 acct->stime += stime; 256 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 257 acct->stime_scaled += stime_scaled; 258 #endif 259 } 260 } 261 EXPORT_SYMBOL_GPL(vtime_account_kernel); 262 263 void vtime_account_idle(struct task_struct *tsk) 264 { 265 unsigned long stime, stime_scaled, steal_time; 266 struct cpu_accounting_data *acct = get_accounting(tsk); 267 268 stime = vtime_delta(acct, &stime_scaled, &steal_time); 269 acct->idle_time += stime + steal_time; 270 } 271 272 static void vtime_account_irq_field(struct cpu_accounting_data *acct, 273 unsigned long *field) 274 { 275 unsigned long stime, stime_scaled; 276 277 vtime_delta_kernel(acct, &stime, &stime_scaled); 278 *field += stime; 279 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 280 acct->stime_scaled += stime_scaled; 281 #endif 282 } 283 284 void vtime_account_softirq(struct task_struct *tsk) 285 { 286 struct cpu_accounting_data *acct = get_accounting(tsk); 287 vtime_account_irq_field(acct, &acct->softirq_time); 288 } 289 290 void vtime_account_hardirq(struct task_struct *tsk) 291 { 292 struct cpu_accounting_data *acct = get_accounting(tsk); 293 vtime_account_irq_field(acct, &acct->hardirq_time); 294 } 295 296 static void vtime_flush_scaled(struct task_struct *tsk, 297 struct cpu_accounting_data *acct) 298 { 299 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 300 if (acct->utime_scaled) 301 tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 302 if (acct->stime_scaled) 303 tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 304 305 acct->utime_scaled = 0; 306 acct->utime_sspurr = 0; 307 acct->stime_scaled = 0; 308 #endif 309 } 310 311 /* 312 * Account the whole cputime accumulated in the paca 313 * Must be called with interrupts disabled. 314 * Assumes that vtime_account_kernel/idle() has been called 315 * recently (i.e. since the last entry from usermode) so that 316 * get_paca()->user_time_scaled is up to date. 317 */ 318 void vtime_flush(struct task_struct *tsk) 319 { 320 struct cpu_accounting_data *acct = get_accounting(tsk); 321 322 if (acct->utime) 323 account_user_time(tsk, cputime_to_nsecs(acct->utime)); 324 325 if (acct->gtime) 326 account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 327 328 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) { 329 account_steal_time(cputime_to_nsecs(acct->steal_time)); 330 acct->steal_time = 0; 331 } 332 333 if (acct->idle_time) 334 account_idle_time(cputime_to_nsecs(acct->idle_time)); 335 336 if (acct->stime) 337 account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 338 CPUTIME_SYSTEM); 339 340 if (acct->hardirq_time) 341 account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 342 CPUTIME_IRQ); 343 if (acct->softirq_time) 344 account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 345 CPUTIME_SOFTIRQ); 346 347 vtime_flush_scaled(tsk, acct); 348 349 acct->utime = 0; 350 acct->gtime = 0; 351 acct->idle_time = 0; 352 acct->stime = 0; 353 acct->hardirq_time = 0; 354 acct->softirq_time = 0; 355 } 356 357 /* 358 * Called from the context switch with interrupts disabled, to charge all 359 * accumulated times to the current process, and to prepare accounting on 360 * the next process. 361 */ 362 void vtime_task_switch(struct task_struct *prev) 363 { 364 if (is_idle_task(prev)) 365 vtime_account_idle(prev); 366 else 367 vtime_account_kernel(prev); 368 369 vtime_flush(prev); 370 371 if (!IS_ENABLED(CONFIG_PPC64)) { 372 struct cpu_accounting_data *acct = get_accounting(current); 373 struct cpu_accounting_data *acct0 = get_accounting(prev); 374 375 acct->starttime = acct0->starttime; 376 } 377 } 378 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 379 380 void __no_kcsan __delay(unsigned long loops) 381 { 382 unsigned long start; 383 384 spin_begin(); 385 if (tb_invalid) { 386 /* 387 * TB is in error state and isn't ticking anymore. 388 * HMI handler was unable to recover from TB error. 389 * Return immediately, so that kernel won't get stuck here. 390 */ 391 spin_cpu_relax(); 392 } else { 393 start = mftb(); 394 while (mftb() - start < loops) 395 spin_cpu_relax(); 396 } 397 spin_end(); 398 } 399 EXPORT_SYMBOL(__delay); 400 401 void __no_kcsan udelay(unsigned long usecs) 402 { 403 __delay(tb_ticks_per_usec * usecs); 404 } 405 EXPORT_SYMBOL(udelay); 406 407 #ifdef CONFIG_SMP 408 unsigned long profile_pc(struct pt_regs *regs) 409 { 410 unsigned long pc = instruction_pointer(regs); 411 412 if (in_lock_functions(pc)) 413 return regs->link; 414 415 return pc; 416 } 417 EXPORT_SYMBOL(profile_pc); 418 #endif 419 420 #ifdef CONFIG_IRQ_WORK 421 422 /* 423 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 424 */ 425 #ifdef CONFIG_PPC64 426 static inline unsigned long test_irq_work_pending(void) 427 { 428 unsigned long x; 429 430 asm volatile("lbz %0,%1(13)" 431 : "=r" (x) 432 : "i" (offsetof(struct paca_struct, irq_work_pending))); 433 return x; 434 } 435 436 static inline void set_irq_work_pending_flag(void) 437 { 438 asm volatile("stb %0,%1(13)" : : 439 "r" (1), 440 "i" (offsetof(struct paca_struct, irq_work_pending))); 441 } 442 443 static inline void clear_irq_work_pending(void) 444 { 445 asm volatile("stb %0,%1(13)" : : 446 "r" (0), 447 "i" (offsetof(struct paca_struct, irq_work_pending))); 448 } 449 450 #else /* 32-bit */ 451 452 DEFINE_PER_CPU(u8, irq_work_pending); 453 454 #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 455 #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 456 #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 457 458 #endif /* 32 vs 64 bit */ 459 460 void arch_irq_work_raise(void) 461 { 462 /* 463 * 64-bit code that uses irq soft-mask can just cause an immediate 464 * interrupt here that gets soft masked, if this is called under 465 * local_irq_disable(). It might be possible to prevent that happening 466 * by noticing interrupts are disabled and setting decrementer pending 467 * to be replayed when irqs are enabled. The problem there is that 468 * tracing can call irq_work_raise, including in code that does low 469 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on) 470 * which could get tangled up if we're messing with the same state 471 * here. 472 */ 473 preempt_disable(); 474 set_irq_work_pending_flag(); 475 set_dec(1); 476 preempt_enable(); 477 } 478 479 static void set_dec_or_work(u64 val) 480 { 481 set_dec(val); 482 /* We may have raced with new irq work */ 483 if (unlikely(test_irq_work_pending())) 484 set_dec(1); 485 } 486 487 #else /* CONFIG_IRQ_WORK */ 488 489 #define test_irq_work_pending() 0 490 #define clear_irq_work_pending() 491 492 static void set_dec_or_work(u64 val) 493 { 494 set_dec(val); 495 } 496 #endif /* CONFIG_IRQ_WORK */ 497 498 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 499 void timer_rearm_host_dec(u64 now) 500 { 501 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 502 503 WARN_ON_ONCE(!arch_irqs_disabled()); 504 WARN_ON_ONCE(mfmsr() & MSR_EE); 505 506 if (now >= *next_tb) { 507 local_paca->irq_happened |= PACA_IRQ_DEC; 508 } else { 509 now = *next_tb - now; 510 if (now > decrementer_max) 511 now = decrementer_max; 512 set_dec_or_work(now); 513 } 514 } 515 EXPORT_SYMBOL_GPL(timer_rearm_host_dec); 516 #endif 517 518 /* 519 * timer_interrupt - gets called when the decrementer overflows, 520 * with interrupts disabled. 521 */ 522 DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt) 523 { 524 struct clock_event_device *evt = this_cpu_ptr(&decrementers); 525 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 526 struct pt_regs *old_regs; 527 u64 now; 528 529 /* 530 * Some implementations of hotplug will get timer interrupts while 531 * offline, just ignore these. 532 */ 533 if (unlikely(!cpu_online(smp_processor_id()))) { 534 set_dec(decrementer_max); 535 return; 536 } 537 538 /* Conditionally hard-enable interrupts. */ 539 if (should_hard_irq_enable(regs)) { 540 /* 541 * Ensure a positive value is written to the decrementer, or 542 * else some CPUs will continue to take decrementer exceptions. 543 * When the PPC_WATCHDOG (decrementer based) is configured, 544 * keep this at most 31 bits, which is about 4 seconds on most 545 * systems, which gives the watchdog a chance of catching timer 546 * interrupt hard lockups. 547 */ 548 if (IS_ENABLED(CONFIG_PPC_WATCHDOG)) 549 set_dec(0x7fffffff); 550 else 551 set_dec(decrementer_max); 552 553 do_hard_irq_enable(); 554 } 555 556 #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 557 if (atomic_read(&ppc_n_lost_interrupts) != 0) 558 __do_IRQ(regs); 559 #endif 560 561 old_regs = set_irq_regs(regs); 562 563 trace_timer_interrupt_entry(regs); 564 565 if (test_irq_work_pending()) { 566 clear_irq_work_pending(); 567 mce_run_irq_context_handlers(); 568 irq_work_run(); 569 } 570 571 now = get_tb(); 572 if (now >= *next_tb) { 573 evt->event_handler(evt); 574 __this_cpu_inc(irq_stat.timer_irqs_event); 575 } else { 576 now = *next_tb - now; 577 if (now > decrementer_max) 578 now = decrementer_max; 579 set_dec_or_work(now); 580 __this_cpu_inc(irq_stat.timer_irqs_others); 581 } 582 583 trace_timer_interrupt_exit(regs); 584 585 set_irq_regs(old_regs); 586 } 587 EXPORT_SYMBOL(timer_interrupt); 588 589 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 590 void timer_broadcast_interrupt(void) 591 { 592 tick_receive_broadcast(); 593 __this_cpu_inc(irq_stat.broadcast_irqs_event); 594 } 595 #endif 596 597 #ifdef CONFIG_SUSPEND 598 /* Overrides the weak version in kernel/power/main.c */ 599 void arch_suspend_disable_irqs(void) 600 { 601 if (ppc_md.suspend_disable_irqs) 602 ppc_md.suspend_disable_irqs(); 603 604 /* Disable the decrementer, so that it doesn't interfere 605 * with suspending. 606 */ 607 608 set_dec(decrementer_max); 609 local_irq_disable(); 610 set_dec(decrementer_max); 611 } 612 613 /* Overrides the weak version in kernel/power/main.c */ 614 void arch_suspend_enable_irqs(void) 615 { 616 local_irq_enable(); 617 618 if (ppc_md.suspend_enable_irqs) 619 ppc_md.suspend_enable_irqs(); 620 } 621 #endif 622 623 unsigned long long tb_to_ns(unsigned long long ticks) 624 { 625 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 626 } 627 EXPORT_SYMBOL_GPL(tb_to_ns); 628 629 /* 630 * Scheduler clock - returns current time in nanosec units. 631 * 632 * Note: mulhdu(a, b) (multiply high double unsigned) returns 633 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 634 * are 64-bit unsigned numbers. 635 */ 636 notrace unsigned long long sched_clock(void) 637 { 638 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 639 } 640 641 642 #ifdef CONFIG_PPC_PSERIES 643 644 /* 645 * Running clock - attempts to give a view of time passing for a virtualised 646 * kernels. 647 * Uses the VTB register if available otherwise a next best guess. 648 */ 649 unsigned long long running_clock(void) 650 { 651 /* 652 * Don't read the VTB as a host since KVM does not switch in host 653 * timebase into the VTB when it takes a guest off the CPU, reading the 654 * VTB would result in reading 'last switched out' guest VTB. 655 * 656 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 657 * would be unsafe to rely only on the #ifdef above. 658 */ 659 if (firmware_has_feature(FW_FEATURE_LPAR) && 660 cpu_has_feature(CPU_FTR_ARCH_207S)) 661 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 662 663 /* 664 * This is a next best approximation without a VTB. 665 * On a host which is running bare metal there should never be any stolen 666 * time and on a host which doesn't do any virtualisation TB *should* equal 667 * VTB so it makes no difference anyway. 668 */ 669 return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 670 } 671 #endif 672 673 static int __init get_freq(char *name, int cells, unsigned long *val) 674 { 675 struct device_node *cpu; 676 const __be32 *fp; 677 int found = 0; 678 679 /* The cpu node should have timebase and clock frequency properties */ 680 cpu = of_find_node_by_type(NULL, "cpu"); 681 682 if (cpu) { 683 fp = of_get_property(cpu, name, NULL); 684 if (fp) { 685 found = 1; 686 *val = of_read_ulong(fp, cells); 687 } 688 689 of_node_put(cpu); 690 } 691 692 return found; 693 } 694 695 static void start_cpu_decrementer(void) 696 { 697 #ifdef CONFIG_BOOKE 698 unsigned int tcr; 699 700 /* Clear any pending timer interrupts */ 701 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 702 703 tcr = mfspr(SPRN_TCR); 704 /* 705 * The watchdog may have already been enabled by u-boot. So leave 706 * TRC[WP] (Watchdog Period) alone. 707 */ 708 tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 709 tcr |= TCR_DIE; /* Enable decrementer */ 710 mtspr(SPRN_TCR, tcr); 711 #endif 712 } 713 714 void __init generic_calibrate_decr(void) 715 { 716 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 717 718 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 719 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 720 721 printk(KERN_ERR "WARNING: Estimating decrementer frequency " 722 "(not found)\n"); 723 } 724 725 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 726 727 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 728 !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 729 730 printk(KERN_ERR "WARNING: Estimating processor frequency " 731 "(not found)\n"); 732 } 733 } 734 735 int update_persistent_clock64(struct timespec64 now) 736 { 737 struct rtc_time tm; 738 739 if (!ppc_md.set_rtc_time) 740 return -ENODEV; 741 742 rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm); 743 744 return ppc_md.set_rtc_time(&tm); 745 } 746 747 static void __read_persistent_clock(struct timespec64 *ts) 748 { 749 struct rtc_time tm; 750 static int first = 1; 751 752 ts->tv_nsec = 0; 753 /* XXX this is a little fragile but will work okay in the short term */ 754 if (first) { 755 first = 0; 756 if (ppc_md.time_init) 757 timezone_offset = ppc_md.time_init(); 758 759 /* get_boot_time() isn't guaranteed to be safe to call late */ 760 if (ppc_md.get_boot_time) { 761 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 762 return; 763 } 764 } 765 if (!ppc_md.get_rtc_time) { 766 ts->tv_sec = 0; 767 return; 768 } 769 ppc_md.get_rtc_time(&tm); 770 771 ts->tv_sec = rtc_tm_to_time64(&tm); 772 } 773 774 void read_persistent_clock64(struct timespec64 *ts) 775 { 776 __read_persistent_clock(ts); 777 778 /* Sanitize it in case real time clock is set below EPOCH */ 779 if (ts->tv_sec < 0) { 780 ts->tv_sec = 0; 781 ts->tv_nsec = 0; 782 } 783 784 } 785 786 /* clocksource code */ 787 static notrace u64 timebase_read(struct clocksource *cs) 788 { 789 return (u64)get_tb(); 790 } 791 792 static void __init clocksource_init(void) 793 { 794 struct clocksource *clock = &clocksource_timebase; 795 796 if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 797 printk(KERN_ERR "clocksource: %s is already registered\n", 798 clock->name); 799 return; 800 } 801 802 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 803 clock->name, clock->mult, clock->shift); 804 } 805 806 static int decrementer_set_next_event(unsigned long evt, 807 struct clock_event_device *dev) 808 { 809 __this_cpu_write(decrementers_next_tb, get_tb() + evt); 810 set_dec_or_work(evt); 811 812 return 0; 813 } 814 815 static int decrementer_shutdown(struct clock_event_device *dev) 816 { 817 __this_cpu_write(decrementers_next_tb, DEC_CLOCKEVENT_STOPPED); 818 set_dec_or_work(decrementer_max); 819 820 return 0; 821 } 822 823 static void register_decrementer_clockevent(int cpu) 824 { 825 struct clock_event_device *dec = &per_cpu(decrementers, cpu); 826 827 *dec = decrementer_clockevent; 828 dec->cpumask = cpumask_of(cpu); 829 830 clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max); 831 832 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 833 dec->name, dec->mult, dec->shift, cpu); 834 835 /* Set values for KVM, see kvm_emulate_dec() */ 836 decrementer_clockevent.mult = dec->mult; 837 decrementer_clockevent.shift = dec->shift; 838 } 839 840 static void enable_large_decrementer(void) 841 { 842 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 843 return; 844 845 if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 846 return; 847 848 /* 849 * If we're running as the hypervisor we need to enable the LD manually 850 * otherwise firmware should have done it for us. 851 */ 852 if (cpu_has_feature(CPU_FTR_HVMODE)) 853 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 854 } 855 856 static void __init set_decrementer_max(void) 857 { 858 struct device_node *cpu; 859 u32 bits = 32; 860 861 /* Prior to ISAv3 the decrementer is always 32 bit */ 862 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 863 return; 864 865 cpu = of_find_node_by_type(NULL, "cpu"); 866 867 if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 868 if (bits > 64 || bits < 32) { 869 pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 870 bits = 32; 871 } 872 873 /* calculate the signed maximum given this many bits */ 874 decrementer_max = (1ul << (bits - 1)) - 1; 875 } 876 877 of_node_put(cpu); 878 879 pr_info("time_init: %u bit decrementer (max: %llx)\n", 880 bits, decrementer_max); 881 } 882 883 static void __init init_decrementer_clockevent(void) 884 { 885 register_decrementer_clockevent(smp_processor_id()); 886 } 887 888 void secondary_cpu_time_init(void) 889 { 890 /* Enable and test the large decrementer for this cpu */ 891 enable_large_decrementer(); 892 893 /* Start the decrementer on CPUs that have manual control 894 * such as BookE 895 */ 896 start_cpu_decrementer(); 897 898 /* FIME: Should make unrelated change to move snapshot_timebase 899 * call here ! */ 900 register_decrementer_clockevent(smp_processor_id()); 901 } 902 903 /* This function is only called on the boot processor */ 904 void __init time_init(void) 905 { 906 struct div_result res; 907 u64 scale; 908 unsigned shift; 909 910 /* Normal PowerPC with timebase register */ 911 if (ppc_md.calibrate_decr) 912 ppc_md.calibrate_decr(); 913 else 914 generic_calibrate_decr(); 915 916 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 917 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 918 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 919 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 920 921 tb_ticks_per_jiffy = ppc_tb_freq / HZ; 922 tb_ticks_per_sec = ppc_tb_freq; 923 tb_ticks_per_usec = ppc_tb_freq / 1000000; 924 925 /* 926 * Compute scale factor for sched_clock. 927 * The calibrate_decr() function has set tb_ticks_per_sec, 928 * which is the timebase frequency. 929 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 930 * the 128-bit result as a 64.64 fixed-point number. 931 * We then shift that number right until it is less than 1.0, 932 * giving us the scale factor and shift count to use in 933 * sched_clock(). 934 */ 935 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 936 scale = res.result_low; 937 for (shift = 0; res.result_high != 0; ++shift) { 938 scale = (scale >> 1) | (res.result_high << 63); 939 res.result_high >>= 1; 940 } 941 tb_to_ns_scale = scale; 942 tb_to_ns_shift = shift; 943 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 944 boot_tb = get_tb(); 945 946 /* If platform provided a timezone (pmac), we correct the time */ 947 if (timezone_offset) { 948 sys_tz.tz_minuteswest = -timezone_offset / 60; 949 sys_tz.tz_dsttime = 0; 950 } 951 952 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 953 954 /* initialise and enable the large decrementer (if we have one) */ 955 set_decrementer_max(); 956 enable_large_decrementer(); 957 958 /* Start the decrementer on CPUs that have manual control 959 * such as BookE 960 */ 961 start_cpu_decrementer(); 962 963 /* Register the clocksource */ 964 clocksource_init(); 965 966 init_decrementer_clockevent(); 967 tick_setup_hrtimer_broadcast(); 968 969 of_clk_init(NULL); 970 enable_sched_clock_irqtime(); 971 } 972 973 /* 974 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 975 * result. 976 */ 977 void div128_by_32(u64 dividend_high, u64 dividend_low, 978 unsigned divisor, struct div_result *dr) 979 { 980 unsigned long a, b, c, d; 981 unsigned long w, x, y, z; 982 u64 ra, rb, rc; 983 984 a = dividend_high >> 32; 985 b = dividend_high & 0xffffffff; 986 c = dividend_low >> 32; 987 d = dividend_low & 0xffffffff; 988 989 w = a / divisor; 990 ra = ((u64)(a - (w * divisor)) << 32) + b; 991 992 rb = ((u64) do_div(ra, divisor) << 32) + c; 993 x = ra; 994 995 rc = ((u64) do_div(rb, divisor) << 32) + d; 996 y = rb; 997 998 do_div(rc, divisor); 999 z = rc; 1000 1001 dr->result_high = ((u64)w << 32) + x; 1002 dr->result_low = ((u64)y << 32) + z; 1003 1004 } 1005 1006 /* We don't need to calibrate delay, we use the CPU timebase for that */ 1007 void calibrate_delay(void) 1008 { 1009 /* Some generic code (such as spinlock debug) use loops_per_jiffy 1010 * as the number of __delay(1) in a jiffy, so make it so 1011 */ 1012 loops_per_jiffy = tb_ticks_per_jiffy; 1013 } 1014 1015 #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1016 static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1017 { 1018 ppc_md.get_rtc_time(tm); 1019 return 0; 1020 } 1021 1022 static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1023 { 1024 if (!ppc_md.set_rtc_time) 1025 return -EOPNOTSUPP; 1026 1027 if (ppc_md.set_rtc_time(tm) < 0) 1028 return -EOPNOTSUPP; 1029 1030 return 0; 1031 } 1032 1033 static const struct rtc_class_ops rtc_generic_ops = { 1034 .read_time = rtc_generic_get_time, 1035 .set_time = rtc_generic_set_time, 1036 }; 1037 1038 static int __init rtc_init(void) 1039 { 1040 struct platform_device *pdev; 1041 1042 if (!ppc_md.get_rtc_time) 1043 return -ENODEV; 1044 1045 pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1046 &rtc_generic_ops, 1047 sizeof(rtc_generic_ops)); 1048 1049 return PTR_ERR_OR_ZERO(pdev); 1050 } 1051 1052 device_initcall(rtc_init); 1053 #endif 1054