1 /* 2 * Common time routines among all ppc machines. 3 * 4 * Written by Cort Dougan (cort@cs.nmt.edu) to merge 5 * Paul Mackerras' version and mine for PReP and Pmac. 6 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). 7 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) 8 * 9 * First round of bugfixes by Gabriel Paubert (paubert@iram.es) 10 * to make clock more stable (2.4.0-test5). The only thing 11 * that this code assumes is that the timebases have been synchronized 12 * by firmware on SMP and are never stopped (never do sleep 13 * on SMP then, nap and doze are OK). 14 * 15 * Speeded up do_gettimeofday by getting rid of references to 16 * xtime (which required locks for consistency). (mikejc@us.ibm.com) 17 * 18 * TODO (not necessarily in this file): 19 * - improve precision and reproducibility of timebase frequency 20 * measurement at boot time. 21 * - for astronomical applications: add a new function to get 22 * non ambiguous timestamps even around leap seconds. This needs 23 * a new timestamp format and a good name. 24 * 25 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 26 * "A Kernel Model for Precision Timekeeping" by Dave Mills 27 * 28 * This program is free software; you can redistribute it and/or 29 * modify it under the terms of the GNU General Public License 30 * as published by the Free Software Foundation; either version 31 * 2 of the License, or (at your option) any later version. 32 */ 33 34 #include <linux/errno.h> 35 #include <linux/export.h> 36 #include <linux/sched.h> 37 #include <linux/kernel.h> 38 #include <linux/param.h> 39 #include <linux/string.h> 40 #include <linux/mm.h> 41 #include <linux/interrupt.h> 42 #include <linux/timex.h> 43 #include <linux/kernel_stat.h> 44 #include <linux/time.h> 45 #include <linux/clockchips.h> 46 #include <linux/init.h> 47 #include <linux/profile.h> 48 #include <linux/cpu.h> 49 #include <linux/security.h> 50 #include <linux/percpu.h> 51 #include <linux/rtc.h> 52 #include <linux/jiffies.h> 53 #include <linux/posix-timers.h> 54 #include <linux/irq.h> 55 #include <linux/delay.h> 56 #include <linux/irq_work.h> 57 #include <linux/clk-provider.h> 58 #include <linux/suspend.h> 59 #include <asm/trace.h> 60 61 #include <asm/io.h> 62 #include <asm/processor.h> 63 #include <asm/nvram.h> 64 #include <asm/cache.h> 65 #include <asm/machdep.h> 66 #include <asm/uaccess.h> 67 #include <asm/time.h> 68 #include <asm/prom.h> 69 #include <asm/irq.h> 70 #include <asm/div64.h> 71 #include <asm/smp.h> 72 #include <asm/vdso_datapage.h> 73 #include <asm/firmware.h> 74 #include <asm/cputime.h> 75 76 /* powerpc clocksource/clockevent code */ 77 78 #include <linux/clockchips.h> 79 #include <linux/timekeeper_internal.h> 80 81 static cycle_t rtc_read(struct clocksource *); 82 static struct clocksource clocksource_rtc = { 83 .name = "rtc", 84 .rating = 400, 85 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 86 .mask = CLOCKSOURCE_MASK(64), 87 .read = rtc_read, 88 }; 89 90 static cycle_t timebase_read(struct clocksource *); 91 static struct clocksource clocksource_timebase = { 92 .name = "timebase", 93 .rating = 400, 94 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 95 .mask = CLOCKSOURCE_MASK(64), 96 .read = timebase_read, 97 }; 98 99 #define DECREMENTER_MAX 0x7fffffff 100 101 static int decrementer_set_next_event(unsigned long evt, 102 struct clock_event_device *dev); 103 static int decrementer_shutdown(struct clock_event_device *evt); 104 105 struct clock_event_device decrementer_clockevent = { 106 .name = "decrementer", 107 .rating = 200, 108 .irq = 0, 109 .set_next_event = decrementer_set_next_event, 110 .set_state_shutdown = decrementer_shutdown, 111 .tick_resume = decrementer_shutdown, 112 .features = CLOCK_EVT_FEAT_ONESHOT | 113 CLOCK_EVT_FEAT_C3STOP, 114 }; 115 EXPORT_SYMBOL(decrementer_clockevent); 116 117 DEFINE_PER_CPU(u64, decrementers_next_tb); 118 static DEFINE_PER_CPU(struct clock_event_device, decrementers); 119 120 #define XSEC_PER_SEC (1024*1024) 121 122 #ifdef CONFIG_PPC64 123 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 124 #else 125 /* compute ((xsec << 12) * max) >> 32 */ 126 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 127 #endif 128 129 unsigned long tb_ticks_per_jiffy; 130 unsigned long tb_ticks_per_usec = 100; /* sane default */ 131 EXPORT_SYMBOL(tb_ticks_per_usec); 132 unsigned long tb_ticks_per_sec; 133 EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 134 135 DEFINE_SPINLOCK(rtc_lock); 136 EXPORT_SYMBOL_GPL(rtc_lock); 137 138 static u64 tb_to_ns_scale __read_mostly; 139 static unsigned tb_to_ns_shift __read_mostly; 140 static u64 boot_tb __read_mostly; 141 142 extern struct timezone sys_tz; 143 static long timezone_offset; 144 145 unsigned long ppc_proc_freq; 146 EXPORT_SYMBOL_GPL(ppc_proc_freq); 147 unsigned long ppc_tb_freq; 148 EXPORT_SYMBOL_GPL(ppc_tb_freq); 149 150 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 151 /* 152 * Factors for converting from cputime_t (timebase ticks) to 153 * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds). 154 * These are all stored as 0.64 fixed-point binary fractions. 155 */ 156 u64 __cputime_jiffies_factor; 157 EXPORT_SYMBOL(__cputime_jiffies_factor); 158 u64 __cputime_usec_factor; 159 EXPORT_SYMBOL(__cputime_usec_factor); 160 u64 __cputime_sec_factor; 161 EXPORT_SYMBOL(__cputime_sec_factor); 162 u64 __cputime_clockt_factor; 163 EXPORT_SYMBOL(__cputime_clockt_factor); 164 DEFINE_PER_CPU(unsigned long, cputime_last_delta); 165 DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta); 166 167 cputime_t cputime_one_jiffy; 168 169 void (*dtl_consumer)(struct dtl_entry *, u64); 170 171 static void calc_cputime_factors(void) 172 { 173 struct div_result res; 174 175 div128_by_32(HZ, 0, tb_ticks_per_sec, &res); 176 __cputime_jiffies_factor = res.result_low; 177 div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 178 __cputime_usec_factor = res.result_low; 179 div128_by_32(1, 0, tb_ticks_per_sec, &res); 180 __cputime_sec_factor = res.result_low; 181 div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res); 182 __cputime_clockt_factor = res.result_low; 183 } 184 185 /* 186 * Read the SPURR on systems that have it, otherwise the PURR, 187 * or if that doesn't exist return the timebase value passed in. 188 */ 189 static u64 read_spurr(u64 tb) 190 { 191 if (cpu_has_feature(CPU_FTR_SPURR)) 192 return mfspr(SPRN_SPURR); 193 if (cpu_has_feature(CPU_FTR_PURR)) 194 return mfspr(SPRN_PURR); 195 return tb; 196 } 197 198 #ifdef CONFIG_PPC_SPLPAR 199 200 /* 201 * Scan the dispatch trace log and count up the stolen time. 202 * Should be called with interrupts disabled. 203 */ 204 static u64 scan_dispatch_log(u64 stop_tb) 205 { 206 u64 i = local_paca->dtl_ridx; 207 struct dtl_entry *dtl = local_paca->dtl_curr; 208 struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 209 struct lppaca *vpa = local_paca->lppaca_ptr; 210 u64 tb_delta; 211 u64 stolen = 0; 212 u64 dtb; 213 214 if (!dtl) 215 return 0; 216 217 if (i == be64_to_cpu(vpa->dtl_idx)) 218 return 0; 219 while (i < be64_to_cpu(vpa->dtl_idx)) { 220 dtb = be64_to_cpu(dtl->timebase); 221 tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 222 be32_to_cpu(dtl->ready_to_enqueue_time); 223 barrier(); 224 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 225 /* buffer has overflowed */ 226 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 227 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 228 continue; 229 } 230 if (dtb > stop_tb) 231 break; 232 if (dtl_consumer) 233 dtl_consumer(dtl, i); 234 stolen += tb_delta; 235 ++i; 236 ++dtl; 237 if (dtl == dtl_end) 238 dtl = local_paca->dispatch_log; 239 } 240 local_paca->dtl_ridx = i; 241 local_paca->dtl_curr = dtl; 242 return stolen; 243 } 244 245 /* 246 * Accumulate stolen time by scanning the dispatch trace log. 247 * Called on entry from user mode. 248 */ 249 void accumulate_stolen_time(void) 250 { 251 u64 sst, ust; 252 253 u8 save_soft_enabled = local_paca->soft_enabled; 254 255 /* We are called early in the exception entry, before 256 * soft/hard_enabled are sync'ed to the expected state 257 * for the exception. We are hard disabled but the PACA 258 * needs to reflect that so various debug stuff doesn't 259 * complain 260 */ 261 local_paca->soft_enabled = 0; 262 263 sst = scan_dispatch_log(local_paca->starttime_user); 264 ust = scan_dispatch_log(local_paca->starttime); 265 local_paca->system_time -= sst; 266 local_paca->user_time -= ust; 267 local_paca->stolen_time += ust + sst; 268 269 local_paca->soft_enabled = save_soft_enabled; 270 } 271 272 static inline u64 calculate_stolen_time(u64 stop_tb) 273 { 274 u64 stolen = 0; 275 276 if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) { 277 stolen = scan_dispatch_log(stop_tb); 278 get_paca()->system_time -= stolen; 279 } 280 281 stolen += get_paca()->stolen_time; 282 get_paca()->stolen_time = 0; 283 return stolen; 284 } 285 286 #else /* CONFIG_PPC_SPLPAR */ 287 static inline u64 calculate_stolen_time(u64 stop_tb) 288 { 289 return 0; 290 } 291 292 #endif /* CONFIG_PPC_SPLPAR */ 293 294 /* 295 * Account time for a transition between system, hard irq 296 * or soft irq state. 297 */ 298 static u64 vtime_delta(struct task_struct *tsk, 299 u64 *sys_scaled, u64 *stolen) 300 { 301 u64 now, nowscaled, deltascaled; 302 u64 udelta, delta, user_scaled; 303 304 WARN_ON_ONCE(!irqs_disabled()); 305 306 now = mftb(); 307 nowscaled = read_spurr(now); 308 get_paca()->system_time += now - get_paca()->starttime; 309 get_paca()->starttime = now; 310 deltascaled = nowscaled - get_paca()->startspurr; 311 get_paca()->startspurr = nowscaled; 312 313 *stolen = calculate_stolen_time(now); 314 315 delta = get_paca()->system_time; 316 get_paca()->system_time = 0; 317 udelta = get_paca()->user_time - get_paca()->utime_sspurr; 318 get_paca()->utime_sspurr = get_paca()->user_time; 319 320 /* 321 * Because we don't read the SPURR on every kernel entry/exit, 322 * deltascaled includes both user and system SPURR ticks. 323 * Apportion these ticks to system SPURR ticks and user 324 * SPURR ticks in the same ratio as the system time (delta) 325 * and user time (udelta) values obtained from the timebase 326 * over the same interval. The system ticks get accounted here; 327 * the user ticks get saved up in paca->user_time_scaled to be 328 * used by account_process_tick. 329 */ 330 *sys_scaled = delta; 331 user_scaled = udelta; 332 if (deltascaled != delta + udelta) { 333 if (udelta) { 334 *sys_scaled = deltascaled * delta / (delta + udelta); 335 user_scaled = deltascaled - *sys_scaled; 336 } else { 337 *sys_scaled = deltascaled; 338 } 339 } 340 get_paca()->user_time_scaled += user_scaled; 341 342 return delta; 343 } 344 345 void vtime_account_system(struct task_struct *tsk) 346 { 347 u64 delta, sys_scaled, stolen; 348 349 delta = vtime_delta(tsk, &sys_scaled, &stolen); 350 account_system_time(tsk, 0, delta, sys_scaled); 351 if (stolen) 352 account_steal_time(stolen); 353 } 354 EXPORT_SYMBOL_GPL(vtime_account_system); 355 356 void vtime_account_idle(struct task_struct *tsk) 357 { 358 u64 delta, sys_scaled, stolen; 359 360 delta = vtime_delta(tsk, &sys_scaled, &stolen); 361 account_idle_time(delta + stolen); 362 } 363 364 /* 365 * Transfer the user time accumulated in the paca 366 * by the exception entry and exit code to the generic 367 * process user time records. 368 * Must be called with interrupts disabled. 369 * Assumes that vtime_account_system/idle() has been called 370 * recently (i.e. since the last entry from usermode) so that 371 * get_paca()->user_time_scaled is up to date. 372 */ 373 void vtime_account_user(struct task_struct *tsk) 374 { 375 cputime_t utime, utimescaled; 376 377 utime = get_paca()->user_time; 378 utimescaled = get_paca()->user_time_scaled; 379 get_paca()->user_time = 0; 380 get_paca()->user_time_scaled = 0; 381 get_paca()->utime_sspurr = 0; 382 account_user_time(tsk, utime, utimescaled); 383 } 384 385 #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 386 #define calc_cputime_factors() 387 #endif 388 389 void __delay(unsigned long loops) 390 { 391 unsigned long start; 392 int diff; 393 394 if (__USE_RTC()) { 395 start = get_rtcl(); 396 do { 397 /* the RTCL register wraps at 1000000000 */ 398 diff = get_rtcl() - start; 399 if (diff < 0) 400 diff += 1000000000; 401 } while (diff < loops); 402 } else { 403 start = get_tbl(); 404 while (get_tbl() - start < loops) 405 HMT_low(); 406 HMT_medium(); 407 } 408 } 409 EXPORT_SYMBOL(__delay); 410 411 void udelay(unsigned long usecs) 412 { 413 __delay(tb_ticks_per_usec * usecs); 414 } 415 EXPORT_SYMBOL(udelay); 416 417 #ifdef CONFIG_SMP 418 unsigned long profile_pc(struct pt_regs *regs) 419 { 420 unsigned long pc = instruction_pointer(regs); 421 422 if (in_lock_functions(pc)) 423 return regs->link; 424 425 return pc; 426 } 427 EXPORT_SYMBOL(profile_pc); 428 #endif 429 430 #ifdef CONFIG_IRQ_WORK 431 432 /* 433 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 434 */ 435 #ifdef CONFIG_PPC64 436 static inline unsigned long test_irq_work_pending(void) 437 { 438 unsigned long x; 439 440 asm volatile("lbz %0,%1(13)" 441 : "=r" (x) 442 : "i" (offsetof(struct paca_struct, irq_work_pending))); 443 return x; 444 } 445 446 static inline void set_irq_work_pending_flag(void) 447 { 448 asm volatile("stb %0,%1(13)" : : 449 "r" (1), 450 "i" (offsetof(struct paca_struct, irq_work_pending))); 451 } 452 453 static inline void clear_irq_work_pending(void) 454 { 455 asm volatile("stb %0,%1(13)" : : 456 "r" (0), 457 "i" (offsetof(struct paca_struct, irq_work_pending))); 458 } 459 460 #else /* 32-bit */ 461 462 DEFINE_PER_CPU(u8, irq_work_pending); 463 464 #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 465 #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 466 #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 467 468 #endif /* 32 vs 64 bit */ 469 470 void arch_irq_work_raise(void) 471 { 472 preempt_disable(); 473 set_irq_work_pending_flag(); 474 set_dec(1); 475 preempt_enable(); 476 } 477 478 #else /* CONFIG_IRQ_WORK */ 479 480 #define test_irq_work_pending() 0 481 #define clear_irq_work_pending() 482 483 #endif /* CONFIG_IRQ_WORK */ 484 485 static void __timer_interrupt(void) 486 { 487 struct pt_regs *regs = get_irq_regs(); 488 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 489 struct clock_event_device *evt = this_cpu_ptr(&decrementers); 490 u64 now; 491 492 trace_timer_interrupt_entry(regs); 493 494 if (test_irq_work_pending()) { 495 clear_irq_work_pending(); 496 irq_work_run(); 497 } 498 499 now = get_tb_or_rtc(); 500 if (now >= *next_tb) { 501 *next_tb = ~(u64)0; 502 if (evt->event_handler) 503 evt->event_handler(evt); 504 __this_cpu_inc(irq_stat.timer_irqs_event); 505 } else { 506 now = *next_tb - now; 507 if (now <= DECREMENTER_MAX) 508 set_dec((int)now); 509 /* We may have raced with new irq work */ 510 if (test_irq_work_pending()) 511 set_dec(1); 512 __this_cpu_inc(irq_stat.timer_irqs_others); 513 } 514 515 #ifdef CONFIG_PPC64 516 /* collect purr register values often, for accurate calculations */ 517 if (firmware_has_feature(FW_FEATURE_SPLPAR)) { 518 struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array); 519 cu->current_tb = mfspr(SPRN_PURR); 520 } 521 #endif 522 523 trace_timer_interrupt_exit(regs); 524 } 525 526 /* 527 * timer_interrupt - gets called when the decrementer overflows, 528 * with interrupts disabled. 529 */ 530 void timer_interrupt(struct pt_regs * regs) 531 { 532 struct pt_regs *old_regs; 533 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 534 535 /* Ensure a positive value is written to the decrementer, or else 536 * some CPUs will continue to take decrementer exceptions. 537 */ 538 set_dec(DECREMENTER_MAX); 539 540 /* Some implementations of hotplug will get timer interrupts while 541 * offline, just ignore these and we also need to set 542 * decrementers_next_tb as MAX to make sure __check_irq_replay 543 * don't replay timer interrupt when return, otherwise we'll trap 544 * here infinitely :( 545 */ 546 if (!cpu_online(smp_processor_id())) { 547 *next_tb = ~(u64)0; 548 return; 549 } 550 551 /* Conditionally hard-enable interrupts now that the DEC has been 552 * bumped to its maximum value 553 */ 554 may_hard_irq_enable(); 555 556 557 #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 558 if (atomic_read(&ppc_n_lost_interrupts) != 0) 559 do_IRQ(regs); 560 #endif 561 562 old_regs = set_irq_regs(regs); 563 irq_enter(); 564 565 __timer_interrupt(); 566 irq_exit(); 567 set_irq_regs(old_regs); 568 } 569 570 /* 571 * Hypervisor decrementer interrupts shouldn't occur but are sometimes 572 * left pending on exit from a KVM guest. We don't need to do anything 573 * to clear them, as they are edge-triggered. 574 */ 575 void hdec_interrupt(struct pt_regs *regs) 576 { 577 } 578 579 #ifdef CONFIG_SUSPEND 580 static void generic_suspend_disable_irqs(void) 581 { 582 /* Disable the decrementer, so that it doesn't interfere 583 * with suspending. 584 */ 585 586 set_dec(DECREMENTER_MAX); 587 local_irq_disable(); 588 set_dec(DECREMENTER_MAX); 589 } 590 591 static void generic_suspend_enable_irqs(void) 592 { 593 local_irq_enable(); 594 } 595 596 /* Overrides the weak version in kernel/power/main.c */ 597 void arch_suspend_disable_irqs(void) 598 { 599 if (ppc_md.suspend_disable_irqs) 600 ppc_md.suspend_disable_irqs(); 601 generic_suspend_disable_irqs(); 602 } 603 604 /* Overrides the weak version in kernel/power/main.c */ 605 void arch_suspend_enable_irqs(void) 606 { 607 generic_suspend_enable_irqs(); 608 if (ppc_md.suspend_enable_irqs) 609 ppc_md.suspend_enable_irqs(); 610 } 611 #endif 612 613 unsigned long long tb_to_ns(unsigned long long ticks) 614 { 615 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 616 } 617 EXPORT_SYMBOL_GPL(tb_to_ns); 618 619 /* 620 * Scheduler clock - returns current time in nanosec units. 621 * 622 * Note: mulhdu(a, b) (multiply high double unsigned) returns 623 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 624 * are 64-bit unsigned numbers. 625 */ 626 unsigned long long sched_clock(void) 627 { 628 if (__USE_RTC()) 629 return get_rtc(); 630 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 631 } 632 633 634 #ifdef CONFIG_PPC_PSERIES 635 636 /* 637 * Running clock - attempts to give a view of time passing for a virtualised 638 * kernels. 639 * Uses the VTB register if available otherwise a next best guess. 640 */ 641 unsigned long long running_clock(void) 642 { 643 /* 644 * Don't read the VTB as a host since KVM does not switch in host 645 * timebase into the VTB when it takes a guest off the CPU, reading the 646 * VTB would result in reading 'last switched out' guest VTB. 647 * 648 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 649 * would be unsafe to rely only on the #ifdef above. 650 */ 651 if (firmware_has_feature(FW_FEATURE_LPAR) && 652 cpu_has_feature(CPU_FTR_ARCH_207S)) 653 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 654 655 /* 656 * This is a next best approximation without a VTB. 657 * On a host which is running bare metal there should never be any stolen 658 * time and on a host which doesn't do any virtualisation TB *should* equal 659 * VTB so it makes no difference anyway. 660 */ 661 return local_clock() - cputime_to_nsecs(kcpustat_this_cpu->cpustat[CPUTIME_STEAL]); 662 } 663 #endif 664 665 static int __init get_freq(char *name, int cells, unsigned long *val) 666 { 667 struct device_node *cpu; 668 const __be32 *fp; 669 int found = 0; 670 671 /* The cpu node should have timebase and clock frequency properties */ 672 cpu = of_find_node_by_type(NULL, "cpu"); 673 674 if (cpu) { 675 fp = of_get_property(cpu, name, NULL); 676 if (fp) { 677 found = 1; 678 *val = of_read_ulong(fp, cells); 679 } 680 681 of_node_put(cpu); 682 } 683 684 return found; 685 } 686 687 static void start_cpu_decrementer(void) 688 { 689 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 690 /* Clear any pending timer interrupts */ 691 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 692 693 /* Enable decrementer interrupt */ 694 mtspr(SPRN_TCR, TCR_DIE); 695 #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */ 696 } 697 698 void __init generic_calibrate_decr(void) 699 { 700 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 701 702 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 703 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 704 705 printk(KERN_ERR "WARNING: Estimating decrementer frequency " 706 "(not found)\n"); 707 } 708 709 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 710 711 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 712 !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 713 714 printk(KERN_ERR "WARNING: Estimating processor frequency " 715 "(not found)\n"); 716 } 717 } 718 719 int update_persistent_clock(struct timespec now) 720 { 721 struct rtc_time tm; 722 723 if (!ppc_md.set_rtc_time) 724 return -ENODEV; 725 726 to_tm(now.tv_sec + 1 + timezone_offset, &tm); 727 tm.tm_year -= 1900; 728 tm.tm_mon -= 1; 729 730 return ppc_md.set_rtc_time(&tm); 731 } 732 733 static void __read_persistent_clock(struct timespec *ts) 734 { 735 struct rtc_time tm; 736 static int first = 1; 737 738 ts->tv_nsec = 0; 739 /* XXX this is a litle fragile but will work okay in the short term */ 740 if (first) { 741 first = 0; 742 if (ppc_md.time_init) 743 timezone_offset = ppc_md.time_init(); 744 745 /* get_boot_time() isn't guaranteed to be safe to call late */ 746 if (ppc_md.get_boot_time) { 747 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 748 return; 749 } 750 } 751 if (!ppc_md.get_rtc_time) { 752 ts->tv_sec = 0; 753 return; 754 } 755 ppc_md.get_rtc_time(&tm); 756 757 ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, 758 tm.tm_hour, tm.tm_min, tm.tm_sec); 759 } 760 761 void read_persistent_clock(struct timespec *ts) 762 { 763 __read_persistent_clock(ts); 764 765 /* Sanitize it in case real time clock is set below EPOCH */ 766 if (ts->tv_sec < 0) { 767 ts->tv_sec = 0; 768 ts->tv_nsec = 0; 769 } 770 771 } 772 773 /* clocksource code */ 774 static cycle_t rtc_read(struct clocksource *cs) 775 { 776 return (cycle_t)get_rtc(); 777 } 778 779 static cycle_t timebase_read(struct clocksource *cs) 780 { 781 return (cycle_t)get_tb(); 782 } 783 784 void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm, 785 struct clocksource *clock, u32 mult, cycle_t cycle_last) 786 { 787 u64 new_tb_to_xs, new_stamp_xsec; 788 u32 frac_sec; 789 790 if (clock != &clocksource_timebase) 791 return; 792 793 /* Make userspace gettimeofday spin until we're done. */ 794 ++vdso_data->tb_update_count; 795 smp_mb(); 796 797 /* 19342813113834067 ~= 2^(20+64) / 1e9 */ 798 new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift); 799 new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC; 800 do_div(new_stamp_xsec, 1000000000); 801 new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC; 802 803 BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC); 804 /* this is tv_nsec / 1e9 as a 0.32 fraction */ 805 frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32; 806 807 /* 808 * tb_update_count is used to allow the userspace gettimeofday code 809 * to assure itself that it sees a consistent view of the tb_to_xs and 810 * stamp_xsec variables. It reads the tb_update_count, then reads 811 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If 812 * the two values of tb_update_count match and are even then the 813 * tb_to_xs and stamp_xsec values are consistent. If not, then it 814 * loops back and reads them again until this criteria is met. 815 * We expect the caller to have done the first increment of 816 * vdso_data->tb_update_count already. 817 */ 818 vdso_data->tb_orig_stamp = cycle_last; 819 vdso_data->stamp_xsec = new_stamp_xsec; 820 vdso_data->tb_to_xs = new_tb_to_xs; 821 vdso_data->wtom_clock_sec = wtm->tv_sec; 822 vdso_data->wtom_clock_nsec = wtm->tv_nsec; 823 vdso_data->stamp_xtime = *wall_time; 824 vdso_data->stamp_sec_fraction = frac_sec; 825 smp_wmb(); 826 ++(vdso_data->tb_update_count); 827 } 828 829 void update_vsyscall_tz(void) 830 { 831 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 832 vdso_data->tz_dsttime = sys_tz.tz_dsttime; 833 } 834 835 static void __init clocksource_init(void) 836 { 837 struct clocksource *clock; 838 839 if (__USE_RTC()) 840 clock = &clocksource_rtc; 841 else 842 clock = &clocksource_timebase; 843 844 if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 845 printk(KERN_ERR "clocksource: %s is already registered\n", 846 clock->name); 847 return; 848 } 849 850 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 851 clock->name, clock->mult, clock->shift); 852 } 853 854 static int decrementer_set_next_event(unsigned long evt, 855 struct clock_event_device *dev) 856 { 857 __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt); 858 set_dec(evt); 859 860 /* We may have raced with new irq work */ 861 if (test_irq_work_pending()) 862 set_dec(1); 863 864 return 0; 865 } 866 867 static int decrementer_shutdown(struct clock_event_device *dev) 868 { 869 decrementer_set_next_event(DECREMENTER_MAX, dev); 870 return 0; 871 } 872 873 /* Interrupt handler for the timer broadcast IPI */ 874 void tick_broadcast_ipi_handler(void) 875 { 876 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 877 878 *next_tb = get_tb_or_rtc(); 879 __timer_interrupt(); 880 } 881 882 static void register_decrementer_clockevent(int cpu) 883 { 884 struct clock_event_device *dec = &per_cpu(decrementers, cpu); 885 886 *dec = decrementer_clockevent; 887 dec->cpumask = cpumask_of(cpu); 888 889 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 890 dec->name, dec->mult, dec->shift, cpu); 891 892 clockevents_register_device(dec); 893 } 894 895 static void __init init_decrementer_clockevent(void) 896 { 897 int cpu = smp_processor_id(); 898 899 clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4); 900 901 decrementer_clockevent.max_delta_ns = 902 clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent); 903 decrementer_clockevent.min_delta_ns = 904 clockevent_delta2ns(2, &decrementer_clockevent); 905 906 register_decrementer_clockevent(cpu); 907 } 908 909 void secondary_cpu_time_init(void) 910 { 911 /* Start the decrementer on CPUs that have manual control 912 * such as BookE 913 */ 914 start_cpu_decrementer(); 915 916 /* FIME: Should make unrelatred change to move snapshot_timebase 917 * call here ! */ 918 register_decrementer_clockevent(smp_processor_id()); 919 } 920 921 /* This function is only called on the boot processor */ 922 void __init time_init(void) 923 { 924 struct div_result res; 925 u64 scale; 926 unsigned shift; 927 928 if (__USE_RTC()) { 929 /* 601 processor: dec counts down by 128 every 128ns */ 930 ppc_tb_freq = 1000000000; 931 } else { 932 /* Normal PowerPC with timebase register */ 933 ppc_md.calibrate_decr(); 934 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 935 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 936 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 937 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 938 } 939 940 tb_ticks_per_jiffy = ppc_tb_freq / HZ; 941 tb_ticks_per_sec = ppc_tb_freq; 942 tb_ticks_per_usec = ppc_tb_freq / 1000000; 943 calc_cputime_factors(); 944 setup_cputime_one_jiffy(); 945 946 /* 947 * Compute scale factor for sched_clock. 948 * The calibrate_decr() function has set tb_ticks_per_sec, 949 * which is the timebase frequency. 950 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 951 * the 128-bit result as a 64.64 fixed-point number. 952 * We then shift that number right until it is less than 1.0, 953 * giving us the scale factor and shift count to use in 954 * sched_clock(). 955 */ 956 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 957 scale = res.result_low; 958 for (shift = 0; res.result_high != 0; ++shift) { 959 scale = (scale >> 1) | (res.result_high << 63); 960 res.result_high >>= 1; 961 } 962 tb_to_ns_scale = scale; 963 tb_to_ns_shift = shift; 964 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 965 boot_tb = get_tb_or_rtc(); 966 967 /* If platform provided a timezone (pmac), we correct the time */ 968 if (timezone_offset) { 969 sys_tz.tz_minuteswest = -timezone_offset / 60; 970 sys_tz.tz_dsttime = 0; 971 } 972 973 vdso_data->tb_update_count = 0; 974 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 975 976 /* Start the decrementer on CPUs that have manual control 977 * such as BookE 978 */ 979 start_cpu_decrementer(); 980 981 /* Register the clocksource */ 982 clocksource_init(); 983 984 init_decrementer_clockevent(); 985 tick_setup_hrtimer_broadcast(); 986 987 #ifdef CONFIG_COMMON_CLK 988 of_clk_init(NULL); 989 #endif 990 } 991 992 993 #define FEBRUARY 2 994 #define STARTOFTIME 1970 995 #define SECDAY 86400L 996 #define SECYR (SECDAY * 365) 997 #define leapyear(year) ((year) % 4 == 0 && \ 998 ((year) % 100 != 0 || (year) % 400 == 0)) 999 #define days_in_year(a) (leapyear(a) ? 366 : 365) 1000 #define days_in_month(a) (month_days[(a) - 1]) 1001 1002 static int month_days[12] = { 1003 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 1004 }; 1005 1006 void to_tm(int tim, struct rtc_time * tm) 1007 { 1008 register int i; 1009 register long hms, day; 1010 1011 day = tim / SECDAY; 1012 hms = tim % SECDAY; 1013 1014 /* Hours, minutes, seconds are easy */ 1015 tm->tm_hour = hms / 3600; 1016 tm->tm_min = (hms % 3600) / 60; 1017 tm->tm_sec = (hms % 3600) % 60; 1018 1019 /* Number of years in days */ 1020 for (i = STARTOFTIME; day >= days_in_year(i); i++) 1021 day -= days_in_year(i); 1022 tm->tm_year = i; 1023 1024 /* Number of months in days left */ 1025 if (leapyear(tm->tm_year)) 1026 days_in_month(FEBRUARY) = 29; 1027 for (i = 1; day >= days_in_month(i); i++) 1028 day -= days_in_month(i); 1029 days_in_month(FEBRUARY) = 28; 1030 tm->tm_mon = i; 1031 1032 /* Days are what is left over (+1) from all that. */ 1033 tm->tm_mday = day + 1; 1034 1035 /* 1036 * No-one uses the day of the week. 1037 */ 1038 tm->tm_wday = -1; 1039 } 1040 EXPORT_SYMBOL(to_tm); 1041 1042 /* 1043 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1044 * result. 1045 */ 1046 void div128_by_32(u64 dividend_high, u64 dividend_low, 1047 unsigned divisor, struct div_result *dr) 1048 { 1049 unsigned long a, b, c, d; 1050 unsigned long w, x, y, z; 1051 u64 ra, rb, rc; 1052 1053 a = dividend_high >> 32; 1054 b = dividend_high & 0xffffffff; 1055 c = dividend_low >> 32; 1056 d = dividend_low & 0xffffffff; 1057 1058 w = a / divisor; 1059 ra = ((u64)(a - (w * divisor)) << 32) + b; 1060 1061 rb = ((u64) do_div(ra, divisor) << 32) + c; 1062 x = ra; 1063 1064 rc = ((u64) do_div(rb, divisor) << 32) + d; 1065 y = rb; 1066 1067 do_div(rc, divisor); 1068 z = rc; 1069 1070 dr->result_high = ((u64)w << 32) + x; 1071 dr->result_low = ((u64)y << 32) + z; 1072 1073 } 1074 1075 /* We don't need to calibrate delay, we use the CPU timebase for that */ 1076 void calibrate_delay(void) 1077 { 1078 /* Some generic code (such as spinlock debug) use loops_per_jiffy 1079 * as the number of __delay(1) in a jiffy, so make it so 1080 */ 1081 loops_per_jiffy = tb_ticks_per_jiffy; 1082 } 1083 1084 static int __init rtc_init(void) 1085 { 1086 struct platform_device *pdev; 1087 1088 if (!ppc_md.get_rtc_time) 1089 return -ENODEV; 1090 1091 pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0); 1092 1093 return PTR_ERR_OR_ZERO(pdev); 1094 } 1095 1096 device_initcall(rtc_init); 1097