1f2783c15SPaul Mackerras /* 2f2783c15SPaul Mackerras * Common time routines among all ppc machines. 3f2783c15SPaul Mackerras * 4f2783c15SPaul Mackerras * Written by Cort Dougan (cort@cs.nmt.edu) to merge 5f2783c15SPaul Mackerras * Paul Mackerras' version and mine for PReP and Pmac. 6f2783c15SPaul Mackerras * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). 7f2783c15SPaul Mackerras * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) 8f2783c15SPaul Mackerras * 9f2783c15SPaul Mackerras * First round of bugfixes by Gabriel Paubert (paubert@iram.es) 10f2783c15SPaul Mackerras * to make clock more stable (2.4.0-test5). The only thing 11f2783c15SPaul Mackerras * that this code assumes is that the timebases have been synchronized 12f2783c15SPaul Mackerras * by firmware on SMP and are never stopped (never do sleep 13f2783c15SPaul Mackerras * on SMP then, nap and doze are OK). 14f2783c15SPaul Mackerras * 15f2783c15SPaul Mackerras * Speeded up do_gettimeofday by getting rid of references to 16f2783c15SPaul Mackerras * xtime (which required locks for consistency). (mikejc@us.ibm.com) 17f2783c15SPaul Mackerras * 18f2783c15SPaul Mackerras * TODO (not necessarily in this file): 19f2783c15SPaul Mackerras * - improve precision and reproducibility of timebase frequency 20f5339277SStephen Rothwell * measurement at boot time. 21f2783c15SPaul Mackerras * - for astronomical applications: add a new function to get 22f2783c15SPaul Mackerras * non ambiguous timestamps even around leap seconds. This needs 23f2783c15SPaul Mackerras * a new timestamp format and a good name. 24f2783c15SPaul Mackerras * 25f2783c15SPaul Mackerras * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 26f2783c15SPaul Mackerras * "A Kernel Model for Precision Timekeeping" by Dave Mills 27f2783c15SPaul Mackerras * 28f2783c15SPaul Mackerras * This program is free software; you can redistribute it and/or 29f2783c15SPaul Mackerras * modify it under the terms of the GNU General Public License 30f2783c15SPaul Mackerras * as published by the Free Software Foundation; either version 31f2783c15SPaul Mackerras * 2 of the License, or (at your option) any later version. 32f2783c15SPaul Mackerras */ 33f2783c15SPaul Mackerras 34f2783c15SPaul Mackerras #include <linux/errno.h> 354b16f8e2SPaul Gortmaker #include <linux/export.h> 36f2783c15SPaul Mackerras #include <linux/sched.h> 37e6017571SIngo Molnar #include <linux/sched/clock.h> 38f2783c15SPaul Mackerras #include <linux/kernel.h> 39f2783c15SPaul Mackerras #include <linux/param.h> 40f2783c15SPaul Mackerras #include <linux/string.h> 41f2783c15SPaul Mackerras #include <linux/mm.h> 42f2783c15SPaul Mackerras #include <linux/interrupt.h> 43f2783c15SPaul Mackerras #include <linux/timex.h> 44f2783c15SPaul Mackerras #include <linux/kernel_stat.h> 45f2783c15SPaul Mackerras #include <linux/time.h> 460d948730SPreeti U Murthy #include <linux/clockchips.h> 47f2783c15SPaul Mackerras #include <linux/init.h> 48f2783c15SPaul Mackerras #include <linux/profile.h> 49f2783c15SPaul Mackerras #include <linux/cpu.h> 50f2783c15SPaul Mackerras #include <linux/security.h> 51f2783c15SPaul Mackerras #include <linux/percpu.h> 52f2783c15SPaul Mackerras #include <linux/rtc.h> 53092b8f34SPaul Mackerras #include <linux/jiffies.h> 54c6622f63SPaul Mackerras #include <linux/posix-timers.h> 557d12e780SDavid Howells #include <linux/irq.h> 56177996e6SBenjamin Herrenschmidt #include <linux/delay.h> 57e360adbeSPeter Zijlstra #include <linux/irq_work.h> 58f0d37300SKevin Hao #include <linux/clk-provider.h> 597f92bc56SDaniel Axtens #include <linux/suspend.h> 60169047f4SArnd Bergmann #include <linux/rtc.h> 6132ef5517SIngo Molnar #include <linux/sched/cputime.h> 626795b85cSAnton Blanchard #include <asm/trace.h> 63f2783c15SPaul Mackerras 64f2783c15SPaul Mackerras #include <asm/io.h> 65f2783c15SPaul Mackerras #include <asm/processor.h> 66f2783c15SPaul Mackerras #include <asm/nvram.h> 67f2783c15SPaul Mackerras #include <asm/cache.h> 68f2783c15SPaul Mackerras #include <asm/machdep.h> 697c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 70f2783c15SPaul Mackerras #include <asm/time.h> 71f2783c15SPaul Mackerras #include <asm/prom.h> 72f2783c15SPaul Mackerras #include <asm/irq.h> 73f2783c15SPaul Mackerras #include <asm/div64.h> 742249ca9dSPaul Mackerras #include <asm/smp.h> 75a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h> 76f2783c15SPaul Mackerras #include <asm/firmware.h> 770545d543SDaniel Axtens #include <asm/asm-prototypes.h> 78f2783c15SPaul Mackerras 794a4cfe38STony Breeds /* powerpc clocksource/clockevent code */ 804a4cfe38STony Breeds 81d831d0b8STony Breeds #include <linux/clockchips.h> 82189374aeSJohn Stultz #include <linux/timekeeper_internal.h> 834a4cfe38STony Breeds 84a5a1d1c2SThomas Gleixner static u64 rtc_read(struct clocksource *); 854a4cfe38STony Breeds static struct clocksource clocksource_rtc = { 864a4cfe38STony Breeds .name = "rtc", 874a4cfe38STony Breeds .rating = 400, 884a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 894a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 904a4cfe38STony Breeds .read = rtc_read, 914a4cfe38STony Breeds }; 924a4cfe38STony Breeds 93a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *); 944a4cfe38STony Breeds static struct clocksource clocksource_timebase = { 954a4cfe38STony Breeds .name = "timebase", 964a4cfe38STony Breeds .rating = 400, 974a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 984a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 994a4cfe38STony Breeds .read = timebase_read, 1004a4cfe38STony Breeds }; 1014a4cfe38STony Breeds 10279901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 10379901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 104d831d0b8STony Breeds 105d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 106d831d0b8STony Breeds struct clock_event_device *dev); 10737a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt); 108d831d0b8STony Breeds 1096e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = { 110d831d0b8STony Breeds .name = "decrementer", 111d831d0b8STony Breeds .rating = 200, 112d831d0b8STony Breeds .irq = 0, 113d831d0b8STony Breeds .set_next_event = decrementer_set_next_event, 11437a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 11537a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 11637a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 11737a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 118d831d0b8STony Breeds }; 1196e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 120d831d0b8STony Breeds 1217df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb); 1227df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers); 123d831d0b8STony Breeds 124f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024) 125f2783c15SPaul Mackerras 126f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 127f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 128f2783c15SPaul Mackerras #else 129f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */ 130f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 131f2783c15SPaul Mackerras #endif 132f2783c15SPaul Mackerras 133f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy; 134f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */ 135f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec); 136f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec; 1372cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 138092b8f34SPaul Mackerras 139f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock); 140f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock); 141f2783c15SPaul Mackerras 142fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly; 143fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly; 144364a1246SHeiko Schocher static u64 boot_tb __read_mostly; 145f2783c15SPaul Mackerras 146f2783c15SPaul Mackerras extern struct timezone sys_tz; 147f2783c15SPaul Mackerras static long timezone_offset; 148f2783c15SPaul Mackerras 149f2783c15SPaul Mackerras unsigned long ppc_proc_freq; 15055ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq); 151f2783c15SPaul Mackerras unsigned long ppc_tb_freq; 15255ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq); 15396c44507SPaul Mackerras 154abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 155c6622f63SPaul Mackerras /* 156e7f340caSFrederic Weisbecker * Factor for converting from cputime_t (timebase ticks) to 157e7f340caSFrederic Weisbecker * microseconds. This is stored as 0.64 fixed-point binary fraction. 158c6622f63SPaul Mackerras */ 1599f5072d4SAndreas Schwab u64 __cputime_usec_factor; 1609f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor); 161a42548a1SStanislaw Gruszka 162c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR 163872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64); 164c223c903SChristophe Leroy #endif 165c223c903SChristophe Leroy 166c223c903SChristophe Leroy #ifdef CONFIG_PPC64 167c223c903SChristophe Leroy #define get_accounting(tsk) (&get_paca()->accounting) 168c223c903SChristophe Leroy #else 169c223c903SChristophe Leroy #define get_accounting(tsk) (&task_thread_info(tsk)->accounting) 170c223c903SChristophe Leroy #endif 171872e439aSPaul Mackerras 172c6622f63SPaul Mackerras static void calc_cputime_factors(void) 173c6622f63SPaul Mackerras { 174c6622f63SPaul Mackerras struct div_result res; 175c6622f63SPaul Mackerras 1769f5072d4SAndreas Schwab div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 1779f5072d4SAndreas Schwab __cputime_usec_factor = res.result_low; 178c6622f63SPaul Mackerras } 179c6622f63SPaul Mackerras 180c6622f63SPaul Mackerras /* 181cf9efce0SPaul Mackerras * Read the SPURR on systems that have it, otherwise the PURR, 182cf9efce0SPaul Mackerras * or if that doesn't exist return the timebase value passed in. 183c6622f63SPaul Mackerras */ 184c223c903SChristophe Leroy static unsigned long read_spurr(unsigned long tb) 185c6622f63SPaul Mackerras { 186cf9efce0SPaul Mackerras if (cpu_has_feature(CPU_FTR_SPURR)) 187cf9efce0SPaul Mackerras return mfspr(SPRN_SPURR); 188c6622f63SPaul Mackerras if (cpu_has_feature(CPU_FTR_PURR)) 189c6622f63SPaul Mackerras return mfspr(SPRN_PURR); 190cf9efce0SPaul Mackerras return tb; 191cf9efce0SPaul Mackerras } 192cf9efce0SPaul Mackerras 193cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR 194cf9efce0SPaul Mackerras 195cf9efce0SPaul Mackerras /* 196cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 197cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 198cf9efce0SPaul Mackerras */ 199cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 200cf9efce0SPaul Mackerras { 201872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 202cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 203cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 204cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 205cf9efce0SPaul Mackerras u64 tb_delta; 206cf9efce0SPaul Mackerras u64 stolen = 0; 207cf9efce0SPaul Mackerras u64 dtb; 208cf9efce0SPaul Mackerras 20984ffae55SAnton Blanchard if (!dtl) 21084ffae55SAnton Blanchard return 0; 21184ffae55SAnton Blanchard 2127ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 213cf9efce0SPaul Mackerras return 0; 2147ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2157ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2167ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2177ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 218cf9efce0SPaul Mackerras barrier(); 2197ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 220cf9efce0SPaul Mackerras /* buffer has overflowed */ 2217ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 222cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 223cf9efce0SPaul Mackerras continue; 224cf9efce0SPaul Mackerras } 225cf9efce0SPaul Mackerras if (dtb > stop_tb) 226cf9efce0SPaul Mackerras break; 22784b07386SAnton Blanchard if (dtl_consumer) 22884b07386SAnton Blanchard dtl_consumer(dtl, i); 229cf9efce0SPaul Mackerras stolen += tb_delta; 230cf9efce0SPaul Mackerras ++i; 231cf9efce0SPaul Mackerras ++dtl; 232cf9efce0SPaul Mackerras if (dtl == dtl_end) 233cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 234cf9efce0SPaul Mackerras } 235cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 236cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 237cf9efce0SPaul Mackerras return stolen; 238c6622f63SPaul Mackerras } 239c6622f63SPaul Mackerras 240c6622f63SPaul Mackerras /* 241cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 242cf9efce0SPaul Mackerras * Called on entry from user mode. 2434603ac18SMichael Neuling */ 244cf9efce0SPaul Mackerras void accumulate_stolen_time(void) 2454603ac18SMichael Neuling { 246cf9efce0SPaul Mackerras u64 sst, ust; 247b18ae08dSTejun Heo u8 save_soft_enabled = local_paca->soft_enabled; 248c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 249b18ae08dSTejun Heo 250b18ae08dSTejun Heo /* We are called early in the exception entry, before 251b18ae08dSTejun Heo * soft/hard_enabled are sync'ed to the expected state 252b18ae08dSTejun Heo * for the exception. We are hard disabled but the PACA 253b18ae08dSTejun Heo * needs to reflect that so various debug stuff doesn't 254b18ae08dSTejun Heo * complain 255b18ae08dSTejun Heo */ 256b18ae08dSTejun Heo local_paca->soft_enabled = 0; 257b18ae08dSTejun Heo 258c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 259c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2608c8b73c4SFrederic Weisbecker acct->stime -= sst; 2618c8b73c4SFrederic Weisbecker acct->utime -= ust; 262f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 263b18ae08dSTejun Heo 264b18ae08dSTejun Heo local_paca->soft_enabled = save_soft_enabled; 2654603ac18SMichael Neuling } 2664603ac18SMichael Neuling 267cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 268cf9efce0SPaul Mackerras { 269a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 270a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 271cf9efce0SPaul Mackerras 272a19ff1a2SFrederic Weisbecker return 0; 273cf9efce0SPaul Mackerras } 274cf9efce0SPaul Mackerras 275cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 276cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 277cf9efce0SPaul Mackerras { 278cf9efce0SPaul Mackerras return 0; 279cf9efce0SPaul Mackerras } 280cf9efce0SPaul Mackerras 281cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 282cf9efce0SPaul Mackerras 2834603ac18SMichael Neuling /* 284c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 285c6622f63SPaul Mackerras * or soft irq state. 286c6622f63SPaul Mackerras */ 287c223c903SChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk, 288a19ff1a2SFrederic Weisbecker unsigned long *stime_scaled, 289a19ff1a2SFrederic Weisbecker unsigned long *steal_time) 290c6622f63SPaul Mackerras { 291c223c903SChristophe Leroy unsigned long now, nowscaled, deltascaled; 292a19ff1a2SFrederic Weisbecker unsigned long stime; 293a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 294c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 295c6622f63SPaul Mackerras 2961b2852b1SFrederic Weisbecker WARN_ON_ONCE(!irqs_disabled()); 2971b2852b1SFrederic Weisbecker 298cf9efce0SPaul Mackerras now = mftb(); 2994603ac18SMichael Neuling nowscaled = read_spurr(now); 300a19ff1a2SFrederic Weisbecker stime = now - acct->starttime; 301c223c903SChristophe Leroy acct->starttime = now; 302c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 303c223c903SChristophe Leroy acct->startspurr = nowscaled; 304cf9efce0SPaul Mackerras 305a19ff1a2SFrederic Weisbecker *steal_time = calculate_stolen_time(now); 306cf9efce0SPaul Mackerras 307a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 3088c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 309cf9efce0SPaul Mackerras 310cf9efce0SPaul Mackerras /* 311cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 312cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 313cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 314cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 315cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 316cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 317cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 318cf9efce0SPaul Mackerras * used by account_process_tick. 319cf9efce0SPaul Mackerras */ 320a19ff1a2SFrederic Weisbecker *stime_scaled = stime; 321a19ff1a2SFrederic Weisbecker utime_scaled = utime; 322a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 323a19ff1a2SFrederic Weisbecker if (utime) { 324a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled * stime / (stime + utime); 325a19ff1a2SFrederic Weisbecker utime_scaled = deltascaled - *stime_scaled; 326cf9efce0SPaul Mackerras } else { 327a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled; 328c6622f63SPaul Mackerras } 329cf9efce0SPaul Mackerras } 330a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 331cf9efce0SPaul Mackerras 332a19ff1a2SFrederic Weisbecker return stime; 333a7e1a9e3SFrederic Weisbecker } 334a7e1a9e3SFrederic Weisbecker 335fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk) 336a7e1a9e3SFrederic Weisbecker { 337a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 338a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 339a7e1a9e3SFrederic Weisbecker 340a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 341a19ff1a2SFrederic Weisbecker 342a19ff1a2SFrederic Weisbecker stime -= min(stime, steal_time); 343a19ff1a2SFrederic Weisbecker acct->steal_time += steal_time; 344a19ff1a2SFrederic Weisbecker 345a19ff1a2SFrederic Weisbecker if ((tsk->flags & PF_VCPU) && !irq_count()) { 346a19ff1a2SFrederic Weisbecker acct->gtime += stime; 347a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 348a19ff1a2SFrederic Weisbecker } else { 349a19ff1a2SFrederic Weisbecker if (hardirq_count()) 350a19ff1a2SFrederic Weisbecker acct->hardirq_time += stime; 351a19ff1a2SFrederic Weisbecker else if (in_serving_softirq()) 352a19ff1a2SFrederic Weisbecker acct->softirq_time += stime; 353a19ff1a2SFrederic Weisbecker else 354a19ff1a2SFrederic Weisbecker acct->stime += stime; 355a19ff1a2SFrederic Weisbecker 356a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 357a19ff1a2SFrederic Weisbecker } 358a7e1a9e3SFrederic Weisbecker } 359c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system); 360a7e1a9e3SFrederic Weisbecker 361fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 362a7e1a9e3SFrederic Weisbecker { 363a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 364a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 365a7e1a9e3SFrederic Weisbecker 366a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 367a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 368cf9efce0SPaul Mackerras } 369c6622f63SPaul Mackerras 370c6622f63SPaul Mackerras /* 371c8d7dabfSFrederic Weisbecker * Account the whole cputime accumulated in the paca 372c6622f63SPaul Mackerras * Must be called with interrupts disabled. 373bcebdf84SFrederic Weisbecker * Assumes that vtime_account_system/idle() has been called 374bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 375cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 376c6622f63SPaul Mackerras */ 377c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk) 378c6622f63SPaul Mackerras { 379c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 380c6622f63SPaul Mackerras 381a19ff1a2SFrederic Weisbecker if (acct->utime) 38223244a5cSFrederic Weisbecker account_user_time(tsk, cputime_to_nsecs(acct->utime)); 383a19ff1a2SFrederic Weisbecker 384a19ff1a2SFrederic Weisbecker if (acct->utime_scaled) 3855613fda9SFrederic Weisbecker tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 386a19ff1a2SFrederic Weisbecker 387a19ff1a2SFrederic Weisbecker if (acct->gtime) 388fb8b049cSFrederic Weisbecker account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 389a19ff1a2SFrederic Weisbecker 390a19ff1a2SFrederic Weisbecker if (acct->steal_time) 391be9095edSFrederic Weisbecker account_steal_time(cputime_to_nsecs(acct->steal_time)); 392a19ff1a2SFrederic Weisbecker 393a19ff1a2SFrederic Weisbecker if (acct->idle_time) 39418b43a9bSFrederic Weisbecker account_idle_time(cputime_to_nsecs(acct->idle_time)); 395a19ff1a2SFrederic Weisbecker 396a19ff1a2SFrederic Weisbecker if (acct->stime) 397fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 398fb8b049cSFrederic Weisbecker CPUTIME_SYSTEM); 399a19ff1a2SFrederic Weisbecker if (acct->stime_scaled) 4005613fda9SFrederic Weisbecker tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 401a19ff1a2SFrederic Weisbecker 402a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 403fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 404fb8b049cSFrederic Weisbecker CPUTIME_IRQ); 405a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 406fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 407fb8b049cSFrederic Weisbecker CPUTIME_SOFTIRQ); 408a19ff1a2SFrederic Weisbecker 4098c8b73c4SFrederic Weisbecker acct->utime = 0; 4108c8b73c4SFrederic Weisbecker acct->utime_scaled = 0; 411c223c903SChristophe Leroy acct->utime_sspurr = 0; 412a19ff1a2SFrederic Weisbecker acct->gtime = 0; 413a19ff1a2SFrederic Weisbecker acct->steal_time = 0; 414a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 415a19ff1a2SFrederic Weisbecker acct->stime = 0; 416a19ff1a2SFrederic Weisbecker acct->stime_scaled = 0; 417a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 418a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 419c6622f63SPaul Mackerras } 420c6622f63SPaul Mackerras 421c223c903SChristophe Leroy #ifdef CONFIG_PPC32 422c223c903SChristophe Leroy /* 423c223c903SChristophe Leroy * Called from the context switch with interrupts disabled, to charge all 424c223c903SChristophe Leroy * accumulated times to the current process, and to prepare accounting on 425c223c903SChristophe Leroy * the next process. 426c223c903SChristophe Leroy */ 427c223c903SChristophe Leroy void arch_vtime_task_switch(struct task_struct *prev) 428c223c903SChristophe Leroy { 429c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(current); 430c223c903SChristophe Leroy 431c223c903SChristophe Leroy acct->starttime = get_accounting(prev)->starttime; 43290d08ba2SFrederic Weisbecker acct->startspurr = get_accounting(prev)->startspurr; 433c223c903SChristophe Leroy } 434c223c903SChristophe Leroy #endif /* CONFIG_PPC32 */ 435c223c903SChristophe Leroy 436abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 437c6622f63SPaul Mackerras #define calc_cputime_factors() 438c6622f63SPaul Mackerras #endif 439c6622f63SPaul Mackerras 4406defa38bSPaul Mackerras void __delay(unsigned long loops) 4416defa38bSPaul Mackerras { 4426defa38bSPaul Mackerras unsigned long start; 4436defa38bSPaul Mackerras int diff; 4446defa38bSPaul Mackerras 4456defa38bSPaul Mackerras if (__USE_RTC()) { 4466defa38bSPaul Mackerras start = get_rtcl(); 4476defa38bSPaul Mackerras do { 4486defa38bSPaul Mackerras /* the RTCL register wraps at 1000000000 */ 4496defa38bSPaul Mackerras diff = get_rtcl() - start; 4506defa38bSPaul Mackerras if (diff < 0) 4516defa38bSPaul Mackerras diff += 1000000000; 4526defa38bSPaul Mackerras } while (diff < loops); 4536defa38bSPaul Mackerras } else { 4546defa38bSPaul Mackerras start = get_tbl(); 4556defa38bSPaul Mackerras while (get_tbl() - start < loops) 4566defa38bSPaul Mackerras HMT_low(); 4576defa38bSPaul Mackerras HMT_medium(); 4586defa38bSPaul Mackerras } 4596defa38bSPaul Mackerras } 4606defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4616defa38bSPaul Mackerras 4626defa38bSPaul Mackerras void udelay(unsigned long usecs) 4636defa38bSPaul Mackerras { 4646defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4656defa38bSPaul Mackerras } 4666defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4676defa38bSPaul Mackerras 468f2783c15SPaul Mackerras #ifdef CONFIG_SMP 469f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 470f2783c15SPaul Mackerras { 471f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 472f2783c15SPaul Mackerras 473f2783c15SPaul Mackerras if (in_lock_functions(pc)) 474f2783c15SPaul Mackerras return regs->link; 475f2783c15SPaul Mackerras 476f2783c15SPaul Mackerras return pc; 477f2783c15SPaul Mackerras } 478f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 479f2783c15SPaul Mackerras #endif 480f2783c15SPaul Mackerras 481e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 482105988c0SPaul Mackerras 4830fe1ac48SPaul Mackerras /* 4840fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 4850fe1ac48SPaul Mackerras */ 4860fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 487e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void) 488105988c0SPaul Mackerras { 4890fe1ac48SPaul Mackerras unsigned long x; 4900fe1ac48SPaul Mackerras 4910fe1ac48SPaul Mackerras asm volatile("lbz %0,%1(13)" 4920fe1ac48SPaul Mackerras : "=r" (x) 493e360adbeSPeter Zijlstra : "i" (offsetof(struct paca_struct, irq_work_pending))); 4940fe1ac48SPaul Mackerras return x; 495105988c0SPaul Mackerras } 496105988c0SPaul Mackerras 497e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 4980fe1ac48SPaul Mackerras { 4990fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5000fe1ac48SPaul Mackerras "r" (1), 501e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5020fe1ac48SPaul Mackerras } 5030fe1ac48SPaul Mackerras 504e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 5050fe1ac48SPaul Mackerras { 5060fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5070fe1ac48SPaul Mackerras "r" (0), 508e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5090fe1ac48SPaul Mackerras } 5100fe1ac48SPaul Mackerras 5110fe1ac48SPaul Mackerras #else /* 32-bit */ 5120fe1ac48SPaul Mackerras 513e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5140fe1ac48SPaul Mackerras 51569111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 51669111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 51769111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 518105988c0SPaul Mackerras 5190fe1ac48SPaul Mackerras #endif /* 32 vs 64 bit */ 5200fe1ac48SPaul Mackerras 5214f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5220fe1ac48SPaul Mackerras { 5230fe1ac48SPaul Mackerras preempt_disable(); 524e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5250fe1ac48SPaul Mackerras set_dec(1); 5260fe1ac48SPaul Mackerras preempt_enable(); 5270fe1ac48SPaul Mackerras } 5280fe1ac48SPaul Mackerras 529e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 530105988c0SPaul Mackerras 531e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 532e360adbeSPeter Zijlstra #define clear_irq_work_pending() 533105988c0SPaul Mackerras 534e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 535105988c0SPaul Mackerras 536e51df2c1SAnton Blanchard static void __timer_interrupt(void) 537f2783c15SPaul Mackerras { 5381b783955SPreeti U Murthy struct pt_regs *regs = get_irq_regs(); 53969111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 54069111bacSChristoph Lameter struct clock_event_device *evt = this_cpu_ptr(&decrementers); 541860aed25SPaul Mackerras u64 now; 542d831d0b8STony Breeds 543e72bbbabSLi Zhong trace_timer_interrupt_entry(regs); 544e72bbbabSLi Zhong 545e360adbeSPeter Zijlstra if (test_irq_work_pending()) { 546e360adbeSPeter Zijlstra clear_irq_work_pending(); 547e360adbeSPeter Zijlstra irq_work_run(); 5480fe1ac48SPaul Mackerras } 5490fe1ac48SPaul Mackerras 550860aed25SPaul Mackerras now = get_tb_or_rtc(); 551860aed25SPaul Mackerras if (now >= *next_tb) { 5527df10275SAnton Blanchard *next_tb = ~(u64)0; 553d831d0b8STony Breeds if (evt->event_handler) 554d831d0b8STony Breeds evt->event_handler(evt); 55569111bacSChristoph Lameter __this_cpu_inc(irq_stat.timer_irqs_event); 556860aed25SPaul Mackerras } else { 557860aed25SPaul Mackerras now = *next_tb - now; 55879901024SOliver O'Halloran if (now <= decrementer_max) 55979901024SOliver O'Halloran set_dec(now); 5600215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 5610215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 5620215f7d8SBenjamin Herrenschmidt set_dec(1); 56369111bacSChristoph Lameter __this_cpu_inc(irq_stat.timer_irqs_others); 564860aed25SPaul Mackerras } 565f2783c15SPaul Mackerras 566f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 567f2783c15SPaul Mackerras /* collect purr register values often, for accurate calculations */ 568f2783c15SPaul Mackerras if (firmware_has_feature(FW_FEATURE_SPLPAR)) { 56969111bacSChristoph Lameter struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array); 570f2783c15SPaul Mackerras cu->current_tb = mfspr(SPRN_PURR); 571f2783c15SPaul Mackerras } 572f2783c15SPaul Mackerras #endif 573f2783c15SPaul Mackerras 574e72bbbabSLi Zhong trace_timer_interrupt_exit(regs); 5751b783955SPreeti U Murthy } 576e72bbbabSLi Zhong 5771b783955SPreeti U Murthy /* 5781b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 5791b783955SPreeti U Murthy * with interrupts disabled. 5801b783955SPreeti U Murthy */ 5811b783955SPreeti U Murthy void timer_interrupt(struct pt_regs * regs) 5821b783955SPreeti U Murthy { 5831b783955SPreeti U Murthy struct pt_regs *old_regs; 58469111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 5851b783955SPreeti U Murthy 5861b783955SPreeti U Murthy /* Ensure a positive value is written to the decrementer, or else 5871b783955SPreeti U Murthy * some CPUs will continue to take decrementer exceptions. 5881b783955SPreeti U Murthy */ 58979901024SOliver O'Halloran set_dec(decrementer_max); 5901b783955SPreeti U Murthy 5911b783955SPreeti U Murthy /* Some implementations of hotplug will get timer interrupts while 5921b783955SPreeti U Murthy * offline, just ignore these and we also need to set 5931b783955SPreeti U Murthy * decrementers_next_tb as MAX to make sure __check_irq_replay 5941b783955SPreeti U Murthy * don't replay timer interrupt when return, otherwise we'll trap 5951b783955SPreeti U Murthy * here infinitely :( 5961b783955SPreeti U Murthy */ 5971b783955SPreeti U Murthy if (!cpu_online(smp_processor_id())) { 5981b783955SPreeti U Murthy *next_tb = ~(u64)0; 5991b783955SPreeti U Murthy return; 6001b783955SPreeti U Murthy } 6011b783955SPreeti U Murthy 6021b783955SPreeti U Murthy /* Conditionally hard-enable interrupts now that the DEC has been 6031b783955SPreeti U Murthy * bumped to its maximum value 6041b783955SPreeti U Murthy */ 6051b783955SPreeti U Murthy may_hard_irq_enable(); 6061b783955SPreeti U Murthy 6071b783955SPreeti U Murthy 6086e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 6091b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 6101b783955SPreeti U Murthy do_IRQ(regs); 6111b783955SPreeti U Murthy #endif 6121b783955SPreeti U Murthy 6131b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 6141b783955SPreeti U Murthy irq_enter(); 6151b783955SPreeti U Murthy 6161b783955SPreeti U Murthy __timer_interrupt(); 617f2783c15SPaul Mackerras irq_exit(); 6187d12e780SDavid Howells set_irq_regs(old_regs); 619f2783c15SPaul Mackerras } 6209445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 621f2783c15SPaul Mackerras 622dabe859eSPaul Mackerras /* 623dabe859eSPaul Mackerras * Hypervisor decrementer interrupts shouldn't occur but are sometimes 624dabe859eSPaul Mackerras * left pending on exit from a KVM guest. We don't need to do anything 625dabe859eSPaul Mackerras * to clear them, as they are edge-triggered. 626dabe859eSPaul Mackerras */ 627dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs) 628dabe859eSPaul Mackerras { 629dabe859eSPaul Mackerras } 630dabe859eSPaul Mackerras 6317ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 632d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void) 6337ac5dde9SScott Wood { 6347ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6357ac5dde9SScott Wood * with suspending. 6367ac5dde9SScott Wood */ 6377ac5dde9SScott Wood 63879901024SOliver O'Halloran set_dec(decrementer_max); 6397ac5dde9SScott Wood local_irq_disable(); 64079901024SOliver O'Halloran set_dec(decrementer_max); 6417ac5dde9SScott Wood } 6427ac5dde9SScott Wood 643d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void) 6447ac5dde9SScott Wood { 6457ac5dde9SScott Wood local_irq_enable(); 6467ac5dde9SScott Wood } 6477ac5dde9SScott Wood 6487ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6497ac5dde9SScott Wood void arch_suspend_disable_irqs(void) 6507ac5dde9SScott Wood { 6517ac5dde9SScott Wood if (ppc_md.suspend_disable_irqs) 6527ac5dde9SScott Wood ppc_md.suspend_disable_irqs(); 6537ac5dde9SScott Wood generic_suspend_disable_irqs(); 6547ac5dde9SScott Wood } 6557ac5dde9SScott Wood 6567ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6577ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 6587ac5dde9SScott Wood { 6597ac5dde9SScott Wood generic_suspend_enable_irqs(); 6607ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 6617ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 6627ac5dde9SScott Wood } 6637ac5dde9SScott Wood #endif 6647ac5dde9SScott Wood 665b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 666b6c295dfSPaul Mackerras { 667b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 668b6c295dfSPaul Mackerras } 669b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 670b6c295dfSPaul Mackerras 671f2783c15SPaul Mackerras /* 672f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 673f2783c15SPaul Mackerras * 674f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 675f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 676f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 677f2783c15SPaul Mackerras */ 6786b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void) 679f2783c15SPaul Mackerras { 68096c44507SPaul Mackerras if (__USE_RTC()) 68196c44507SPaul Mackerras return get_rtc(); 682fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 683f2783c15SPaul Mackerras } 684f2783c15SPaul Mackerras 6854be1b297SCyril Bur 6864be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 6874be1b297SCyril Bur 6884be1b297SCyril Bur /* 6894be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 6904be1b297SCyril Bur * kernels. 6914be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 6924be1b297SCyril Bur */ 6934be1b297SCyril Bur unsigned long long running_clock(void) 6944be1b297SCyril Bur { 6954be1b297SCyril Bur /* 6964be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 6974be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 6984be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 6994be1b297SCyril Bur * 7004be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7014be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7024be1b297SCyril Bur */ 7034be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7044be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7054be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7064be1b297SCyril Bur 7074be1b297SCyril Bur /* 7084be1b297SCyril Bur * This is a next best approximation without a VTB. 7094be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7104be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7114be1b297SCyril Bur * VTB so it makes no difference anyway. 7124be1b297SCyril Bur */ 7139f3768e0SFrédéric Weisbecker return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 7144be1b297SCyril Bur } 7154be1b297SCyril Bur #endif 7164be1b297SCyril Bur 7170bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 718f2783c15SPaul Mackerras { 719f2783c15SPaul Mackerras struct device_node *cpu; 7206f7aba7bSAnton Blanchard const __be32 *fp; 7210bb474a4SAnton Blanchard int found = 0; 722f2783c15SPaul Mackerras 7230bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 724f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 725f2783c15SPaul Mackerras 726d8a8188dSOlaf Hering if (cpu) { 727e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 728d8a8188dSOlaf Hering if (fp) { 7290bb474a4SAnton Blanchard found = 1; 730a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 731f2783c15SPaul Mackerras } 7320bb474a4SAnton Blanchard 7330bb474a4SAnton Blanchard of_node_put(cpu); 734f2783c15SPaul Mackerras } 7350bb474a4SAnton Blanchard 7360bb474a4SAnton Blanchard return found; 7370bb474a4SAnton Blanchard } 7380bb474a4SAnton Blanchard 739e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 74077c0a700SBenjamin Herrenschmidt { 74177c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 7426e2f03e2SIvan Mikhaylov unsigned int tcr; 7436e2f03e2SIvan Mikhaylov 74477c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 74577c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 74677c0a700SBenjamin Herrenschmidt 7476e2f03e2SIvan Mikhaylov tcr = mfspr(SPRN_TCR); 7486e2f03e2SIvan Mikhaylov /* 7496e2f03e2SIvan Mikhaylov * The watchdog may have already been enabled by u-boot. So leave 7506e2f03e2SIvan Mikhaylov * TRC[WP] (Watchdog Period) alone. 7516e2f03e2SIvan Mikhaylov */ 7526e2f03e2SIvan Mikhaylov tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 7536e2f03e2SIvan Mikhaylov tcr |= TCR_DIE; /* Enable decrementer */ 7546e2f03e2SIvan Mikhaylov mtspr(SPRN_TCR, tcr); 7556e2f03e2SIvan Mikhaylov #endif 75677c0a700SBenjamin Herrenschmidt } 75777c0a700SBenjamin Herrenschmidt 7580bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 7590bb474a4SAnton Blanchard { 7600bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 7610bb474a4SAnton Blanchard 7620bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 7630bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 7640bb474a4SAnton Blanchard 765f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 766f2783c15SPaul Mackerras "(not found)\n"); 7670bb474a4SAnton Blanchard } 768f2783c15SPaul Mackerras 7690bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 7700bb474a4SAnton Blanchard 7710bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 7720bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 7730bb474a4SAnton Blanchard 7740bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 7750bb474a4SAnton Blanchard "(not found)\n"); 776f2783c15SPaul Mackerras } 777f2783c15SPaul Mackerras } 778f2783c15SPaul Mackerras 779aa3be5f3STony Breeds int update_persistent_clock(struct timespec now) 780f2783c15SPaul Mackerras { 781f2783c15SPaul Mackerras struct rtc_time tm; 782f2783c15SPaul Mackerras 783aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 784023f333aSJason Gunthorpe return -ENODEV; 785aa3be5f3STony Breeds 786aa3be5f3STony Breeds to_tm(now.tv_sec + 1 + timezone_offset, &tm); 787aa3be5f3STony Breeds tm.tm_year -= 1900; 788aa3be5f3STony Breeds tm.tm_mon -= 1; 789aa3be5f3STony Breeds 790aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 791aa3be5f3STony Breeds } 792aa3be5f3STony Breeds 793978d7eb3SBenjamin Herrenschmidt static void __read_persistent_clock(struct timespec *ts) 794aa3be5f3STony Breeds { 795aa3be5f3STony Breeds struct rtc_time tm; 796aa3be5f3STony Breeds static int first = 1; 797aa3be5f3STony Breeds 798d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 799aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 800aa3be5f3STony Breeds if (first) { 801aa3be5f3STony Breeds first = 0; 802aa3be5f3STony Breeds if (ppc_md.time_init) 803aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 804aa3be5f3STony Breeds 805aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 806d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 807d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 808d90246cdSMartin Schwidefsky return; 809aa3be5f3STony Breeds } 810d90246cdSMartin Schwidefsky } 811d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 812d90246cdSMartin Schwidefsky ts->tv_sec = 0; 813d90246cdSMartin Schwidefsky return; 814d90246cdSMartin Schwidefsky } 815f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 816978d7eb3SBenjamin Herrenschmidt 817d4f587c6SMartin Schwidefsky ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, 818f2783c15SPaul Mackerras tm.tm_hour, tm.tm_min, tm.tm_sec); 819f2783c15SPaul Mackerras } 820f2783c15SPaul Mackerras 821978d7eb3SBenjamin Herrenschmidt void read_persistent_clock(struct timespec *ts) 822978d7eb3SBenjamin Herrenschmidt { 823978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 824978d7eb3SBenjamin Herrenschmidt 825978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 826978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 827978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 828978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 829978d7eb3SBenjamin Herrenschmidt } 830978d7eb3SBenjamin Herrenschmidt 831978d7eb3SBenjamin Herrenschmidt } 832978d7eb3SBenjamin Herrenschmidt 8334a4cfe38STony Breeds /* clocksource code */ 8346b847d79SSantosh Sivaraj static notrace u64 rtc_read(struct clocksource *cs) 8354a4cfe38STony Breeds { 836a5a1d1c2SThomas Gleixner return (u64)get_rtc(); 8374a4cfe38STony Breeds } 8384a4cfe38STony Breeds 8396b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs) 8404a4cfe38STony Breeds { 841a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8424a4cfe38STony Breeds } 8434a4cfe38STony Breeds 844*d4cfb113SPaul Mackerras 845*d4cfb113SPaul Mackerras void update_vsyscall(struct timekeeper *tk) 8464a4cfe38STony Breeds { 847*d4cfb113SPaul Mackerras struct timespec xt; 848*d4cfb113SPaul Mackerras struct clocksource *clock = tk->tkr_mono.clock; 849*d4cfb113SPaul Mackerras u32 mult = tk->tkr_mono.mult; 850*d4cfb113SPaul Mackerras u32 shift = tk->tkr_mono.shift; 851*d4cfb113SPaul Mackerras u64 cycle_last = tk->tkr_mono.cycle_last; 852b0797b60SJohn Stultz u64 new_tb_to_xs, new_stamp_xsec; 853*d4cfb113SPaul Mackerras u64 frac_sec; 8544a4cfe38STony Breeds 8554a4cfe38STony Breeds if (clock != &clocksource_timebase) 8564a4cfe38STony Breeds return; 8574a4cfe38STony Breeds 858*d4cfb113SPaul Mackerras xt.tv_sec = tk->xtime_sec; 859*d4cfb113SPaul Mackerras xt.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 860*d4cfb113SPaul Mackerras 8614a4cfe38STony Breeds /* Make userspace gettimeofday spin until we're done. */ 8624a4cfe38STony Breeds ++vdso_data->tb_update_count; 8634a4cfe38STony Breeds smp_mb(); 8644a4cfe38STony Breeds 865*d4cfb113SPaul Mackerras /* 866*d4cfb113SPaul Mackerras * This computes ((2^20 / 1e9) * mult) >> shift as a 867*d4cfb113SPaul Mackerras * 0.64 fixed-point fraction. 868*d4cfb113SPaul Mackerras * The computation in the else clause below won't overflow 869*d4cfb113SPaul Mackerras * (as long as the timebase frequency is >= 1.049 MHz) 870*d4cfb113SPaul Mackerras * but loses precision because we lose the low bits of the constant 871*d4cfb113SPaul Mackerras * in the shift. Note that 19342813113834067 ~= 2^(20+64) / 1e9. 872*d4cfb113SPaul Mackerras * For a shift of 24 the error is about 0.5e-9, or about 0.5ns 873*d4cfb113SPaul Mackerras * over a second. (Shift values are usually 22, 23 or 24.) 874*d4cfb113SPaul Mackerras * For high frequency clocks such as the 512MHz timebase clock 875*d4cfb113SPaul Mackerras * on POWER[6789], the mult value is small (e.g. 32768000) 876*d4cfb113SPaul Mackerras * and so we can shift the constant by 16 initially 877*d4cfb113SPaul Mackerras * (295147905179 ~= 2^(20+64-16) / 1e9) and then do the 878*d4cfb113SPaul Mackerras * remaining shifts after the multiplication, which gives a 879*d4cfb113SPaul Mackerras * more accurate result (e.g. with mult = 32768000, shift = 24, 880*d4cfb113SPaul Mackerras * the error is only about 1.2e-12, or 0.7ns over 10 minutes). 881*d4cfb113SPaul Mackerras */ 882*d4cfb113SPaul Mackerras if (mult <= 62500000 && clock->shift >= 16) 883*d4cfb113SPaul Mackerras new_tb_to_xs = ((u64) mult * 295147905179ULL) >> (clock->shift - 16); 884*d4cfb113SPaul Mackerras else 88511b8633aSAnton Blanchard new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift); 886b0797b60SJohn Stultz 887*d4cfb113SPaul Mackerras /* 888*d4cfb113SPaul Mackerras * Compute the fractional second in units of 2^-32 seconds. 889*d4cfb113SPaul Mackerras * The fractional second is tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift 890*d4cfb113SPaul Mackerras * in nanoseconds, so multiplying that by 2^32 / 1e9 gives 891*d4cfb113SPaul Mackerras * it in units of 2^-32 seconds. 892*d4cfb113SPaul Mackerras * We assume shift <= 32 because clocks_calc_mult_shift() 893*d4cfb113SPaul Mackerras * generates shift values in the range 0 - 32. 894*d4cfb113SPaul Mackerras */ 895*d4cfb113SPaul Mackerras frac_sec = tk->tkr_mono.xtime_nsec << (32 - shift); 896*d4cfb113SPaul Mackerras do_div(frac_sec, NSEC_PER_SEC); 897*d4cfb113SPaul Mackerras 898*d4cfb113SPaul Mackerras /* 899*d4cfb113SPaul Mackerras * Work out new stamp_xsec value for any legacy users of systemcfg. 900*d4cfb113SPaul Mackerras * stamp_xsec is in units of 2^-20 seconds. 901*d4cfb113SPaul Mackerras */ 902*d4cfb113SPaul Mackerras new_stamp_xsec = frac_sec >> 12; 903*d4cfb113SPaul Mackerras new_stamp_xsec += tk->xtime_sec * XSEC_PER_SEC; 90447916be4SThomas Gleixner 905b0797b60SJohn Stultz /* 906b0797b60SJohn Stultz * tb_update_count is used to allow the userspace gettimeofday code 907b0797b60SJohn Stultz * to assure itself that it sees a consistent view of the tb_to_xs and 908b0797b60SJohn Stultz * stamp_xsec variables. It reads the tb_update_count, then reads 909b0797b60SJohn Stultz * tb_to_xs and stamp_xsec and then reads tb_update_count again. If 910b0797b60SJohn Stultz * the two values of tb_update_count match and are even then the 911b0797b60SJohn Stultz * tb_to_xs and stamp_xsec values are consistent. If not, then it 912b0797b60SJohn Stultz * loops back and reads them again until this criteria is met. 913b0797b60SJohn Stultz */ 9144a0e6377SThomas Gleixner vdso_data->tb_orig_stamp = cycle_last; 915b0797b60SJohn Stultz vdso_data->stamp_xsec = new_stamp_xsec; 916b0797b60SJohn Stultz vdso_data->tb_to_xs = new_tb_to_xs; 917*d4cfb113SPaul Mackerras vdso_data->wtom_clock_sec = tk->wall_to_monotonic.tv_sec; 918*d4cfb113SPaul Mackerras vdso_data->wtom_clock_nsec = tk->wall_to_monotonic.tv_nsec; 919*d4cfb113SPaul Mackerras vdso_data->stamp_xtime = xt; 92047916be4SThomas Gleixner vdso_data->stamp_sec_fraction = frac_sec; 921b0797b60SJohn Stultz smp_wmb(); 922b0797b60SJohn Stultz ++(vdso_data->tb_update_count); 9234a4cfe38STony Breeds } 9244a4cfe38STony Breeds 9254a4cfe38STony Breeds void update_vsyscall_tz(void) 9264a4cfe38STony Breeds { 9274a4cfe38STony Breeds vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 9284a4cfe38STony Breeds vdso_data->tz_dsttime = sys_tz.tz_dsttime; 9294a4cfe38STony Breeds } 9304a4cfe38STony Breeds 9311c21a293SMichael Ellerman static void __init clocksource_init(void) 9324a4cfe38STony Breeds { 9334a4cfe38STony Breeds struct clocksource *clock; 9344a4cfe38STony Breeds 9354a4cfe38STony Breeds if (__USE_RTC()) 9364a4cfe38STony Breeds clock = &clocksource_rtc; 9374a4cfe38STony Breeds else 9384a4cfe38STony Breeds clock = &clocksource_timebase; 9394a4cfe38STony Breeds 94011b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 9414a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 9424a4cfe38STony Breeds clock->name); 9434a4cfe38STony Breeds return; 9444a4cfe38STony Breeds } 9454a4cfe38STony Breeds 9464a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 9474a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 9484a4cfe38STony Breeds } 9494a4cfe38STony Breeds 950d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 951d831d0b8STony Breeds struct clock_event_device *dev) 952d831d0b8STony Breeds { 95369111bacSChristoph Lameter __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt); 954d831d0b8STony Breeds set_dec(evt); 9550215f7d8SBenjamin Herrenschmidt 9560215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 9570215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 9580215f7d8SBenjamin Herrenschmidt set_dec(1); 9590215f7d8SBenjamin Herrenschmidt 960d831d0b8STony Breeds return 0; 961d831d0b8STony Breeds } 962d831d0b8STony Breeds 96337a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 964d831d0b8STony Breeds { 96579901024SOliver O'Halloran decrementer_set_next_event(decrementer_max, dev); 96637a13e78SViresh Kumar return 0; 967d831d0b8STony Breeds } 968d831d0b8STony Breeds 9691b67bee1SSrivatsa S. Bhat /* Interrupt handler for the timer broadcast IPI */ 9701b67bee1SSrivatsa S. Bhat void tick_broadcast_ipi_handler(void) 9711b67bee1SSrivatsa S. Bhat { 97269111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 9731b783955SPreeti U Murthy 9741b783955SPreeti U Murthy *next_tb = get_tb_or_rtc(); 9751b783955SPreeti U Murthy __timer_interrupt(); 9761b67bee1SSrivatsa S. Bhat } 9771b67bee1SSrivatsa S. Bhat 978d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 979d831d0b8STony Breeds { 9807df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 981d831d0b8STony Breeds 982d831d0b8STony Breeds *dec = decrementer_clockevent; 983320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 984d831d0b8STony Breeds 985b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 986d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 987d831d0b8STony Breeds 988d831d0b8STony Breeds clockevents_register_device(dec); 989d831d0b8STony Breeds } 990d831d0b8STony Breeds 99179901024SOliver O'Halloran static void enable_large_decrementer(void) 99279901024SOliver O'Halloran { 99379901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 99479901024SOliver O'Halloran return; 99579901024SOliver O'Halloran 99679901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 99779901024SOliver O'Halloran return; 99879901024SOliver O'Halloran 99979901024SOliver O'Halloran /* 100079901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 100179901024SOliver O'Halloran * otherwise firmware should have done it for us. 100279901024SOliver O'Halloran */ 100379901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 100479901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 100579901024SOliver O'Halloran } 100679901024SOliver O'Halloran 100779901024SOliver O'Halloran static void __init set_decrementer_max(void) 100879901024SOliver O'Halloran { 100979901024SOliver O'Halloran struct device_node *cpu; 101079901024SOliver O'Halloran u32 bits = 32; 101179901024SOliver O'Halloran 101279901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 101379901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 101479901024SOliver O'Halloran return; 101579901024SOliver O'Halloran 101679901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 101779901024SOliver O'Halloran 101879901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 101979901024SOliver O'Halloran if (bits > 64 || bits < 32) { 102079901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 102179901024SOliver O'Halloran bits = 32; 102279901024SOliver O'Halloran } 102379901024SOliver O'Halloran 102479901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 102579901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 102679901024SOliver O'Halloran } 102779901024SOliver O'Halloran 102879901024SOliver O'Halloran of_node_put(cpu); 102979901024SOliver O'Halloran 103079901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 103179901024SOliver O'Halloran bits, decrementer_max); 103279901024SOliver O'Halloran } 103379901024SOliver O'Halloran 1034c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 1035d831d0b8STony Breeds { 1036d831d0b8STony Breeds int cpu = smp_processor_id(); 1037d831d0b8STony Breeds 1038d8afc6fdSAnton Blanchard clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4); 1039d8afc6fdSAnton Blanchard 1040d831d0b8STony Breeds decrementer_clockevent.max_delta_ns = 104179901024SOliver O'Halloran clockevent_delta2ns(decrementer_max, &decrementer_clockevent); 1042115631c3SNicolai Stange decrementer_clockevent.max_delta_ticks = decrementer_max; 104343875cc0SPaul Mackerras decrementer_clockevent.min_delta_ns = 104443875cc0SPaul Mackerras clockevent_delta2ns(2, &decrementer_clockevent); 1045115631c3SNicolai Stange decrementer_clockevent.min_delta_ticks = 2; 1046d831d0b8STony Breeds 1047d831d0b8STony Breeds register_decrementer_clockevent(cpu); 1048d831d0b8STony Breeds } 1049d831d0b8STony Breeds 1050d831d0b8STony Breeds void secondary_cpu_time_init(void) 1051d831d0b8STony Breeds { 105279901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 105379901024SOliver O'Halloran enable_large_decrementer(); 105479901024SOliver O'Halloran 105577c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 105677c0a700SBenjamin Herrenschmidt * such as BookE 105777c0a700SBenjamin Herrenschmidt */ 105877c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 105977c0a700SBenjamin Herrenschmidt 1060d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 1061d831d0b8STony Breeds * call here ! */ 1062d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 1063d831d0b8STony Breeds } 1064d831d0b8STony Breeds 1065f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 1066f2783c15SPaul Mackerras void __init time_init(void) 1067f2783c15SPaul Mackerras { 1068f2783c15SPaul Mackerras struct div_result res; 1069d75d68cfSPaul Mackerras u64 scale; 1070f2783c15SPaul Mackerras unsigned shift; 1071f2783c15SPaul Mackerras 107296c44507SPaul Mackerras if (__USE_RTC()) { 107396c44507SPaul Mackerras /* 601 processor: dec counts down by 128 every 128ns */ 107496c44507SPaul Mackerras ppc_tb_freq = 1000000000; 107596c44507SPaul Mackerras } else { 107696c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 1077f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 1078224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 1079374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 1080224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 1081374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 108296c44507SPaul Mackerras } 1083374e99d4SPaul Mackerras 1084374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 1085092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 1086374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 1087c6622f63SPaul Mackerras calc_cputime_factors(); 1088092b8f34SPaul Mackerras 1089092b8f34SPaul Mackerras /* 1090f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 1091f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 1092f2783c15SPaul Mackerras * which is the timebase frequency. 1093f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 1094f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 1095f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 1096f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 1097f2783c15SPaul Mackerras * sched_clock(). 1098f2783c15SPaul Mackerras */ 1099f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1100f2783c15SPaul Mackerras scale = res.result_low; 1101f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1102f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1103f2783c15SPaul Mackerras res.result_high >>= 1; 1104f2783c15SPaul Mackerras } 1105f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1106f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1107fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 1108c27da339SBenjamin Herrenschmidt boot_tb = get_tb_or_rtc(); 1109f2783c15SPaul Mackerras 1110092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1111092b8f34SPaul Mackerras if (timezone_offset) { 1112092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1113092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1114092b8f34SPaul Mackerras } 1115092b8f34SPaul Mackerras 1116a7f290daSBenjamin Herrenschmidt vdso_data->tb_update_count = 0; 1117a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1118f2783c15SPaul Mackerras 111979901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 112079901024SOliver O'Halloran set_decrementer_max(); 112179901024SOliver O'Halloran enable_large_decrementer(); 112279901024SOliver O'Halloran 112377c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 112477c0a700SBenjamin Herrenschmidt * such as BookE 112577c0a700SBenjamin Herrenschmidt */ 112677c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 112777c0a700SBenjamin Herrenschmidt 1128f5339277SStephen Rothwell /* Register the clocksource */ 11294a4cfe38STony Breeds clocksource_init(); 11304a4cfe38STony Breeds 1131d831d0b8STony Breeds init_decrementer_clockevent(); 11320d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1133f0d37300SKevin Hao 1134f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK 1135f0d37300SKevin Hao of_clk_init(NULL); 1136f0d37300SKevin Hao #endif 1137f2783c15SPaul Mackerras } 1138f2783c15SPaul Mackerras 1139f2783c15SPaul Mackerras 1140f2783c15SPaul Mackerras #define FEBRUARY 2 1141f2783c15SPaul Mackerras #define STARTOFTIME 1970 1142f2783c15SPaul Mackerras #define SECDAY 86400L 1143f2783c15SPaul Mackerras #define SECYR (SECDAY * 365) 1144f2783c15SPaul Mackerras #define leapyear(year) ((year) % 4 == 0 && \ 1145f2783c15SPaul Mackerras ((year) % 100 != 0 || (year) % 400 == 0)) 1146f2783c15SPaul Mackerras #define days_in_year(a) (leapyear(a) ? 366 : 365) 1147f2783c15SPaul Mackerras #define days_in_month(a) (month_days[(a) - 1]) 1148f2783c15SPaul Mackerras 1149f2783c15SPaul Mackerras static int month_days[12] = { 1150f2783c15SPaul Mackerras 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 1151f2783c15SPaul Mackerras }; 1152f2783c15SPaul Mackerras 1153f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm) 1154f2783c15SPaul Mackerras { 1155f2783c15SPaul Mackerras register int i; 1156f2783c15SPaul Mackerras register long hms, day; 1157f2783c15SPaul Mackerras 1158f2783c15SPaul Mackerras day = tim / SECDAY; 1159f2783c15SPaul Mackerras hms = tim % SECDAY; 1160f2783c15SPaul Mackerras 1161f2783c15SPaul Mackerras /* Hours, minutes, seconds are easy */ 1162f2783c15SPaul Mackerras tm->tm_hour = hms / 3600; 1163f2783c15SPaul Mackerras tm->tm_min = (hms % 3600) / 60; 1164f2783c15SPaul Mackerras tm->tm_sec = (hms % 3600) % 60; 1165f2783c15SPaul Mackerras 1166f2783c15SPaul Mackerras /* Number of years in days */ 1167f2783c15SPaul Mackerras for (i = STARTOFTIME; day >= days_in_year(i); i++) 1168f2783c15SPaul Mackerras day -= days_in_year(i); 1169f2783c15SPaul Mackerras tm->tm_year = i; 1170f2783c15SPaul Mackerras 1171f2783c15SPaul Mackerras /* Number of months in days left */ 1172f2783c15SPaul Mackerras if (leapyear(tm->tm_year)) 1173f2783c15SPaul Mackerras days_in_month(FEBRUARY) = 29; 1174f2783c15SPaul Mackerras for (i = 1; day >= days_in_month(i); i++) 1175f2783c15SPaul Mackerras day -= days_in_month(i); 1176f2783c15SPaul Mackerras days_in_month(FEBRUARY) = 28; 1177f2783c15SPaul Mackerras tm->tm_mon = i; 1178f2783c15SPaul Mackerras 1179f2783c15SPaul Mackerras /* Days are what is left over (+1) from all that. */ 1180f2783c15SPaul Mackerras tm->tm_mday = day + 1; 1181f2783c15SPaul Mackerras 1182f2783c15SPaul Mackerras /* 118300b912b0SDaniel Axtens * No-one uses the day of the week. 1184f2783c15SPaul Mackerras */ 118500b912b0SDaniel Axtens tm->tm_wday = -1; 1186f2783c15SPaul Mackerras } 1187e1802b06SAnton Blanchard EXPORT_SYMBOL(to_tm); 1188f2783c15SPaul Mackerras 1189f2783c15SPaul Mackerras /* 1190f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1191f2783c15SPaul Mackerras * result. 1192f2783c15SPaul Mackerras */ 1193f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1194f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1195f2783c15SPaul Mackerras { 1196f2783c15SPaul Mackerras unsigned long a, b, c, d; 1197f2783c15SPaul Mackerras unsigned long w, x, y, z; 1198f2783c15SPaul Mackerras u64 ra, rb, rc; 1199f2783c15SPaul Mackerras 1200f2783c15SPaul Mackerras a = dividend_high >> 32; 1201f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1202f2783c15SPaul Mackerras c = dividend_low >> 32; 1203f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1204f2783c15SPaul Mackerras 1205f2783c15SPaul Mackerras w = a / divisor; 1206f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1207f2783c15SPaul Mackerras 1208f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1209f2783c15SPaul Mackerras x = ra; 1210f2783c15SPaul Mackerras 1211f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1212f2783c15SPaul Mackerras y = rb; 1213f2783c15SPaul Mackerras 1214f2783c15SPaul Mackerras do_div(rc, divisor); 1215f2783c15SPaul Mackerras z = rc; 1216f2783c15SPaul Mackerras 1217f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1218f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1219f2783c15SPaul Mackerras 1220f2783c15SPaul Mackerras } 1221bcd68a70SGeert Uytterhoeven 1222177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1223177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1224177996e6SBenjamin Herrenschmidt { 1225177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1226177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1227177996e6SBenjamin Herrenschmidt */ 1228177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1229177996e6SBenjamin Herrenschmidt } 1230177996e6SBenjamin Herrenschmidt 1231169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1232169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1233169047f4SArnd Bergmann { 1234169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1235169047f4SArnd Bergmann return rtc_valid_tm(tm); 1236169047f4SArnd Bergmann } 1237169047f4SArnd Bergmann 1238169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1239169047f4SArnd Bergmann { 1240169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1241169047f4SArnd Bergmann return -EOPNOTSUPP; 1242169047f4SArnd Bergmann 1243169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1244169047f4SArnd Bergmann return -EOPNOTSUPP; 1245169047f4SArnd Bergmann 1246169047f4SArnd Bergmann return 0; 1247169047f4SArnd Bergmann } 1248169047f4SArnd Bergmann 1249169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1250169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1251169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1252169047f4SArnd Bergmann }; 1253169047f4SArnd Bergmann 1254bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1255bcd68a70SGeert Uytterhoeven { 1256bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1257bcd68a70SGeert Uytterhoeven 1258bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1259bcd68a70SGeert Uytterhoeven return -ENODEV; 1260bcd68a70SGeert Uytterhoeven 1261169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1262169047f4SArnd Bergmann &rtc_generic_ops, 1263169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1264bcd68a70SGeert Uytterhoeven 12658c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1266bcd68a70SGeert Uytterhoeven } 1267bcd68a70SGeert Uytterhoeven 12688f6b9512SPaul Gortmaker device_initcall(rtc_init); 1269169047f4SArnd Bergmann #endif 1270