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> 624e287e65SNicholas Piggin #include <linux/processor.h> 636795b85cSAnton Blanchard #include <asm/trace.h> 64f2783c15SPaul Mackerras 65f2783c15SPaul Mackerras #include <asm/io.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, 114*81759360SAnton Blanchard .set_state_oneshot_stopped = decrementer_shutdown, 11537a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 11637a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 11737a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 11837a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 119d831d0b8STony Breeds }; 1206e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 121d831d0b8STony Breeds 1227df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb); 1237df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers); 124d831d0b8STony Breeds 125f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024) 126f2783c15SPaul Mackerras 127f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 128f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 129f2783c15SPaul Mackerras #else 130f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */ 131f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 132f2783c15SPaul Mackerras #endif 133f2783c15SPaul Mackerras 134f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy; 135f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */ 136f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec); 137f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec; 1382cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 139092b8f34SPaul Mackerras 140f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock); 141f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock); 142f2783c15SPaul Mackerras 143fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly; 144fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly; 145364a1246SHeiko Schocher static u64 boot_tb __read_mostly; 146f2783c15SPaul Mackerras 147f2783c15SPaul Mackerras extern struct timezone sys_tz; 148f2783c15SPaul Mackerras static long timezone_offset; 149f2783c15SPaul Mackerras 150f2783c15SPaul Mackerras unsigned long ppc_proc_freq; 15155ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq); 152f2783c15SPaul Mackerras unsigned long ppc_tb_freq; 15355ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq); 15496c44507SPaul Mackerras 155abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 156c6622f63SPaul Mackerras /* 157e7f340caSFrederic Weisbecker * Factor for converting from cputime_t (timebase ticks) to 158e7f340caSFrederic Weisbecker * microseconds. This is stored as 0.64 fixed-point binary fraction. 159c6622f63SPaul Mackerras */ 1609f5072d4SAndreas Schwab u64 __cputime_usec_factor; 1619f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor); 162a42548a1SStanislaw Gruszka 163c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR 164872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64); 165c223c903SChristophe Leroy #endif 166c223c903SChristophe Leroy 167c6622f63SPaul Mackerras static void calc_cputime_factors(void) 168c6622f63SPaul Mackerras { 169c6622f63SPaul Mackerras struct div_result res; 170c6622f63SPaul Mackerras 1719f5072d4SAndreas Schwab div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 1729f5072d4SAndreas Schwab __cputime_usec_factor = res.result_low; 173c6622f63SPaul Mackerras } 174c6622f63SPaul Mackerras 175c6622f63SPaul Mackerras /* 176cf9efce0SPaul Mackerras * Read the SPURR on systems that have it, otherwise the PURR, 177cf9efce0SPaul Mackerras * or if that doesn't exist return the timebase value passed in. 178c6622f63SPaul Mackerras */ 179c223c903SChristophe Leroy static unsigned long read_spurr(unsigned long tb) 180c6622f63SPaul Mackerras { 181cf9efce0SPaul Mackerras if (cpu_has_feature(CPU_FTR_SPURR)) 182cf9efce0SPaul Mackerras return mfspr(SPRN_SPURR); 183c6622f63SPaul Mackerras if (cpu_has_feature(CPU_FTR_PURR)) 184c6622f63SPaul Mackerras return mfspr(SPRN_PURR); 185cf9efce0SPaul Mackerras return tb; 186cf9efce0SPaul Mackerras } 187cf9efce0SPaul Mackerras 188cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR 189cf9efce0SPaul Mackerras 190cf9efce0SPaul Mackerras /* 191cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 192cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 193cf9efce0SPaul Mackerras */ 194cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 195cf9efce0SPaul Mackerras { 196872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 197cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 198cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 199cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 200cf9efce0SPaul Mackerras u64 tb_delta; 201cf9efce0SPaul Mackerras u64 stolen = 0; 202cf9efce0SPaul Mackerras u64 dtb; 203cf9efce0SPaul Mackerras 20484ffae55SAnton Blanchard if (!dtl) 20584ffae55SAnton Blanchard return 0; 20684ffae55SAnton Blanchard 2077ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 208cf9efce0SPaul Mackerras return 0; 2097ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2107ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2117ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2127ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 213cf9efce0SPaul Mackerras barrier(); 2147ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 215cf9efce0SPaul Mackerras /* buffer has overflowed */ 2167ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 217cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 218cf9efce0SPaul Mackerras continue; 219cf9efce0SPaul Mackerras } 220cf9efce0SPaul Mackerras if (dtb > stop_tb) 221cf9efce0SPaul Mackerras break; 22284b07386SAnton Blanchard if (dtl_consumer) 22384b07386SAnton Blanchard dtl_consumer(dtl, i); 224cf9efce0SPaul Mackerras stolen += tb_delta; 225cf9efce0SPaul Mackerras ++i; 226cf9efce0SPaul Mackerras ++dtl; 227cf9efce0SPaul Mackerras if (dtl == dtl_end) 228cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 229cf9efce0SPaul Mackerras } 230cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 231cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 232cf9efce0SPaul Mackerras return stolen; 233c6622f63SPaul Mackerras } 234c6622f63SPaul Mackerras 235c6622f63SPaul Mackerras /* 236cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 237cf9efce0SPaul Mackerras * Called on entry from user mode. 2384603ac18SMichael Neuling */ 239cf9efce0SPaul Mackerras void accumulate_stolen_time(void) 2404603ac18SMichael Neuling { 241cf9efce0SPaul Mackerras u64 sst, ust; 2424e26bc4aSMadhavan Srinivasan unsigned long save_irq_soft_mask = irq_soft_mask_return(); 243c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 244b18ae08dSTejun Heo 245b18ae08dSTejun Heo /* We are called early in the exception entry, before 246b18ae08dSTejun Heo * soft/hard_enabled are sync'ed to the expected state 247b18ae08dSTejun Heo * for the exception. We are hard disabled but the PACA 248b18ae08dSTejun Heo * needs to reflect that so various debug stuff doesn't 249b18ae08dSTejun Heo * complain 250b18ae08dSTejun Heo */ 2514e26bc4aSMadhavan Srinivasan irq_soft_mask_set(IRQS_DISABLED); 252b18ae08dSTejun Heo 253c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 254c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2558c8b73c4SFrederic Weisbecker acct->stime -= sst; 2568c8b73c4SFrederic Weisbecker acct->utime -= ust; 257f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 258b18ae08dSTejun Heo 2594e26bc4aSMadhavan Srinivasan irq_soft_mask_set(save_irq_soft_mask); 2604603ac18SMichael Neuling } 2614603ac18SMichael Neuling 262cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 263cf9efce0SPaul Mackerras { 264a6201da3SAneesh Kumar K.V if (!firmware_has_feature(FW_FEATURE_SPLPAR)) 265a6201da3SAneesh Kumar K.V return 0; 266a6201da3SAneesh Kumar K.V 267a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 268a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 269cf9efce0SPaul Mackerras 270a19ff1a2SFrederic Weisbecker return 0; 271cf9efce0SPaul Mackerras } 272cf9efce0SPaul Mackerras 273cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 274cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 275cf9efce0SPaul Mackerras { 276cf9efce0SPaul Mackerras return 0; 277cf9efce0SPaul Mackerras } 278cf9efce0SPaul Mackerras 279cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 280cf9efce0SPaul Mackerras 2814603ac18SMichael Neuling /* 282c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 283c6622f63SPaul Mackerras * or soft irq state. 284c6622f63SPaul Mackerras */ 285c223c903SChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk, 286a19ff1a2SFrederic Weisbecker unsigned long *stime_scaled, 287a19ff1a2SFrederic Weisbecker unsigned long *steal_time) 288c6622f63SPaul Mackerras { 289c223c903SChristophe Leroy unsigned long now, nowscaled, deltascaled; 290a19ff1a2SFrederic Weisbecker unsigned long stime; 291a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 292c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 293c6622f63SPaul Mackerras 2941b2852b1SFrederic Weisbecker WARN_ON_ONCE(!irqs_disabled()); 2951b2852b1SFrederic Weisbecker 296cf9efce0SPaul Mackerras now = mftb(); 2974603ac18SMichael Neuling nowscaled = read_spurr(now); 298a19ff1a2SFrederic Weisbecker stime = now - acct->starttime; 299c223c903SChristophe Leroy acct->starttime = now; 300c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 301c223c903SChristophe Leroy acct->startspurr = nowscaled; 302cf9efce0SPaul Mackerras 303a19ff1a2SFrederic Weisbecker *steal_time = calculate_stolen_time(now); 304cf9efce0SPaul Mackerras 305a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 3068c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 307cf9efce0SPaul Mackerras 308cf9efce0SPaul Mackerras /* 309cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 310cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 311cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 312cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 313cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 314cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 315cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 316cf9efce0SPaul Mackerras * used by account_process_tick. 317cf9efce0SPaul Mackerras */ 318a19ff1a2SFrederic Weisbecker *stime_scaled = stime; 319a19ff1a2SFrederic Weisbecker utime_scaled = utime; 320a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 321a19ff1a2SFrederic Weisbecker if (utime) { 322a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled * stime / (stime + utime); 323a19ff1a2SFrederic Weisbecker utime_scaled = deltascaled - *stime_scaled; 324cf9efce0SPaul Mackerras } else { 325a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled; 326c6622f63SPaul Mackerras } 327cf9efce0SPaul Mackerras } 328a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 329cf9efce0SPaul Mackerras 330a19ff1a2SFrederic Weisbecker return stime; 331a7e1a9e3SFrederic Weisbecker } 332a7e1a9e3SFrederic Weisbecker 333fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk) 334a7e1a9e3SFrederic Weisbecker { 335a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 336a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 337a7e1a9e3SFrederic Weisbecker 338a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 339a19ff1a2SFrederic Weisbecker 340a19ff1a2SFrederic Weisbecker stime -= min(stime, steal_time); 341a19ff1a2SFrederic Weisbecker acct->steal_time += steal_time; 342a19ff1a2SFrederic Weisbecker 343a19ff1a2SFrederic Weisbecker if ((tsk->flags & PF_VCPU) && !irq_count()) { 344a19ff1a2SFrederic Weisbecker acct->gtime += stime; 345a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 346a19ff1a2SFrederic Weisbecker } else { 347a19ff1a2SFrederic Weisbecker if (hardirq_count()) 348a19ff1a2SFrederic Weisbecker acct->hardirq_time += stime; 349a19ff1a2SFrederic Weisbecker else if (in_serving_softirq()) 350a19ff1a2SFrederic Weisbecker acct->softirq_time += stime; 351a19ff1a2SFrederic Weisbecker else 352a19ff1a2SFrederic Weisbecker acct->stime += stime; 353a19ff1a2SFrederic Weisbecker 354a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 355a19ff1a2SFrederic Weisbecker } 356a7e1a9e3SFrederic Weisbecker } 357c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system); 358a7e1a9e3SFrederic Weisbecker 359fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 360a7e1a9e3SFrederic Weisbecker { 361a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 362a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 363a7e1a9e3SFrederic Weisbecker 364a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 365a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 366cf9efce0SPaul Mackerras } 367c6622f63SPaul Mackerras 368c6622f63SPaul Mackerras /* 369c8d7dabfSFrederic Weisbecker * Account the whole cputime accumulated in the paca 370c6622f63SPaul Mackerras * Must be called with interrupts disabled. 371bcebdf84SFrederic Weisbecker * Assumes that vtime_account_system/idle() has been called 372bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 373cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 374c6622f63SPaul Mackerras */ 375c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk) 376c6622f63SPaul Mackerras { 377c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 378c6622f63SPaul Mackerras 379a19ff1a2SFrederic Weisbecker if (acct->utime) 38023244a5cSFrederic Weisbecker account_user_time(tsk, cputime_to_nsecs(acct->utime)); 381a19ff1a2SFrederic Weisbecker 382a19ff1a2SFrederic Weisbecker if (acct->utime_scaled) 3835613fda9SFrederic Weisbecker tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 384a19ff1a2SFrederic Weisbecker 385a19ff1a2SFrederic Weisbecker if (acct->gtime) 386fb8b049cSFrederic Weisbecker account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 387a19ff1a2SFrederic Weisbecker 388a19ff1a2SFrederic Weisbecker if (acct->steal_time) 389be9095edSFrederic Weisbecker account_steal_time(cputime_to_nsecs(acct->steal_time)); 390a19ff1a2SFrederic Weisbecker 391a19ff1a2SFrederic Weisbecker if (acct->idle_time) 39218b43a9bSFrederic Weisbecker account_idle_time(cputime_to_nsecs(acct->idle_time)); 393a19ff1a2SFrederic Weisbecker 394a19ff1a2SFrederic Weisbecker if (acct->stime) 395fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 396fb8b049cSFrederic Weisbecker CPUTIME_SYSTEM); 397a19ff1a2SFrederic Weisbecker if (acct->stime_scaled) 3985613fda9SFrederic Weisbecker tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 399a19ff1a2SFrederic Weisbecker 400a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 401fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 402fb8b049cSFrederic Weisbecker CPUTIME_IRQ); 403a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 404fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 405fb8b049cSFrederic Weisbecker CPUTIME_SOFTIRQ); 406a19ff1a2SFrederic Weisbecker 4078c8b73c4SFrederic Weisbecker acct->utime = 0; 4088c8b73c4SFrederic Weisbecker acct->utime_scaled = 0; 409c223c903SChristophe Leroy acct->utime_sspurr = 0; 410a19ff1a2SFrederic Weisbecker acct->gtime = 0; 411a19ff1a2SFrederic Weisbecker acct->steal_time = 0; 412a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 413a19ff1a2SFrederic Weisbecker acct->stime = 0; 414a19ff1a2SFrederic Weisbecker acct->stime_scaled = 0; 415a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 416a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 417c6622f63SPaul Mackerras } 418c6622f63SPaul Mackerras 419abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 420c6622f63SPaul Mackerras #define calc_cputime_factors() 421c6622f63SPaul Mackerras #endif 422c6622f63SPaul Mackerras 4236defa38bSPaul Mackerras void __delay(unsigned long loops) 4246defa38bSPaul Mackerras { 4256defa38bSPaul Mackerras unsigned long start; 4266defa38bSPaul Mackerras int diff; 4276defa38bSPaul Mackerras 4284e287e65SNicholas Piggin spin_begin(); 4296defa38bSPaul Mackerras if (__USE_RTC()) { 4306defa38bSPaul Mackerras start = get_rtcl(); 4316defa38bSPaul Mackerras do { 4326defa38bSPaul Mackerras /* the RTCL register wraps at 1000000000 */ 4336defa38bSPaul Mackerras diff = get_rtcl() - start; 4346defa38bSPaul Mackerras if (diff < 0) 4356defa38bSPaul Mackerras diff += 1000000000; 4364e287e65SNicholas Piggin spin_cpu_relax(); 4376defa38bSPaul Mackerras } while (diff < loops); 4386defa38bSPaul Mackerras } else { 4396defa38bSPaul Mackerras start = get_tbl(); 4406defa38bSPaul Mackerras while (get_tbl() - start < loops) 4414e287e65SNicholas Piggin spin_cpu_relax(); 4426defa38bSPaul Mackerras } 4434e287e65SNicholas Piggin spin_end(); 4446defa38bSPaul Mackerras } 4456defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4466defa38bSPaul Mackerras 4476defa38bSPaul Mackerras void udelay(unsigned long usecs) 4486defa38bSPaul Mackerras { 4496defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4506defa38bSPaul Mackerras } 4516defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4526defa38bSPaul Mackerras 453f2783c15SPaul Mackerras #ifdef CONFIG_SMP 454f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 455f2783c15SPaul Mackerras { 456f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 457f2783c15SPaul Mackerras 458f2783c15SPaul Mackerras if (in_lock_functions(pc)) 459f2783c15SPaul Mackerras return regs->link; 460f2783c15SPaul Mackerras 461f2783c15SPaul Mackerras return pc; 462f2783c15SPaul Mackerras } 463f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 464f2783c15SPaul Mackerras #endif 465f2783c15SPaul Mackerras 466e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 467105988c0SPaul Mackerras 4680fe1ac48SPaul Mackerras /* 4690fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 4700fe1ac48SPaul Mackerras */ 4710fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 472e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void) 473105988c0SPaul Mackerras { 4740fe1ac48SPaul Mackerras unsigned long x; 4750fe1ac48SPaul Mackerras 4760fe1ac48SPaul Mackerras asm volatile("lbz %0,%1(13)" 4770fe1ac48SPaul Mackerras : "=r" (x) 478e360adbeSPeter Zijlstra : "i" (offsetof(struct paca_struct, irq_work_pending))); 4790fe1ac48SPaul Mackerras return x; 480105988c0SPaul Mackerras } 481105988c0SPaul Mackerras 482e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 4830fe1ac48SPaul Mackerras { 4840fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 4850fe1ac48SPaul Mackerras "r" (1), 486e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 4870fe1ac48SPaul Mackerras } 4880fe1ac48SPaul Mackerras 489e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 4900fe1ac48SPaul Mackerras { 4910fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 4920fe1ac48SPaul Mackerras "r" (0), 493e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 4940fe1ac48SPaul Mackerras } 4950fe1ac48SPaul Mackerras 496ebb37cf3SNicholas Piggin void arch_irq_work_raise(void) 497ebb37cf3SNicholas Piggin { 498ebb37cf3SNicholas Piggin preempt_disable(); 499ebb37cf3SNicholas Piggin set_irq_work_pending_flag(); 500ebb37cf3SNicholas Piggin /* 501ebb37cf3SNicholas Piggin * Non-nmi code running with interrupts disabled will replay 502ebb37cf3SNicholas Piggin * irq_happened before it re-enables interrupts, so setthe 503ebb37cf3SNicholas Piggin * decrementer there instead of causing a hardware exception 504ebb37cf3SNicholas Piggin * which would immediately hit the masked interrupt handler 505ebb37cf3SNicholas Piggin * and have the net effect of setting the decrementer in 506ebb37cf3SNicholas Piggin * irq_happened. 507ebb37cf3SNicholas Piggin * 508ebb37cf3SNicholas Piggin * NMI interrupts can not check this when they return, so the 509ebb37cf3SNicholas Piggin * decrementer hardware exception is raised, which will fire 510ebb37cf3SNicholas Piggin * when interrupts are next enabled. 511ebb37cf3SNicholas Piggin * 512ebb37cf3SNicholas Piggin * BookE does not support this yet, it must audit all NMI 513ebb37cf3SNicholas Piggin * interrupt handlers to ensure they call nmi_enter() so this 514ebb37cf3SNicholas Piggin * check would be correct. 515ebb37cf3SNicholas Piggin */ 516ebb37cf3SNicholas Piggin if (IS_ENABLED(CONFIG_BOOKE) || !irqs_disabled() || in_nmi()) { 517ebb37cf3SNicholas Piggin set_dec(1); 518ebb37cf3SNicholas Piggin } else { 519ebb37cf3SNicholas Piggin hard_irq_disable(); 520ebb37cf3SNicholas Piggin local_paca->irq_happened |= PACA_IRQ_DEC; 521ebb37cf3SNicholas Piggin } 522ebb37cf3SNicholas Piggin preempt_enable(); 523ebb37cf3SNicholas Piggin } 524ebb37cf3SNicholas Piggin 5250fe1ac48SPaul Mackerras #else /* 32-bit */ 5260fe1ac48SPaul Mackerras 527e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5280fe1ac48SPaul Mackerras 52969111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 53069111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 53169111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 532105988c0SPaul Mackerras 5334f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5340fe1ac48SPaul Mackerras { 5350fe1ac48SPaul Mackerras preempt_disable(); 536e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5370fe1ac48SPaul Mackerras set_dec(1); 5380fe1ac48SPaul Mackerras preempt_enable(); 5390fe1ac48SPaul Mackerras } 5400fe1ac48SPaul Mackerras 541ebb37cf3SNicholas Piggin #endif /* 32 vs 64 bit */ 542ebb37cf3SNicholas Piggin 543e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 544105988c0SPaul Mackerras 545e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 546e360adbeSPeter Zijlstra #define clear_irq_work_pending() 547105988c0SPaul Mackerras 548e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 549105988c0SPaul Mackerras 5501b783955SPreeti U Murthy /* 5511b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 5521b783955SPreeti U Murthy * with interrupts disabled. 5531b783955SPreeti U Murthy */ 5541b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs) 5551b783955SPreeti U Murthy { 5563f984620SNicholas Piggin struct clock_event_device *evt = this_cpu_ptr(&decrementers); 55769111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 5583f984620SNicholas Piggin struct pt_regs *old_regs; 5593f984620SNicholas Piggin u64 now; 5601b783955SPreeti U Murthy 5611b783955SPreeti U Murthy /* Some implementations of hotplug will get timer interrupts while 5621b783955SPreeti U Murthy * offline, just ignore these and we also need to set 5631b783955SPreeti U Murthy * decrementers_next_tb as MAX to make sure __check_irq_replay 5641b783955SPreeti U Murthy * don't replay timer interrupt when return, otherwise we'll trap 5651b783955SPreeti U Murthy * here infinitely :( 5661b783955SPreeti U Murthy */ 567a7cba02dSNicholas Piggin if (unlikely(!cpu_online(smp_processor_id()))) { 5681b783955SPreeti U Murthy *next_tb = ~(u64)0; 569a7cba02dSNicholas Piggin set_dec(decrementer_max); 5701b783955SPreeti U Murthy return; 5711b783955SPreeti U Murthy } 5721b783955SPreeti U Murthy 573a7cba02dSNicholas Piggin /* Ensure a positive value is written to the decrementer, or else 574a7cba02dSNicholas Piggin * some CPUs will continue to take decrementer exceptions. When the 575a7cba02dSNicholas Piggin * PPC_WATCHDOG (decrementer based) is configured, keep this at most 576a7cba02dSNicholas Piggin * 31 bits, which is about 4 seconds on most systems, which gives 577a7cba02dSNicholas Piggin * the watchdog a chance of catching timer interrupt hard lockups. 578a7cba02dSNicholas Piggin */ 579a7cba02dSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_WATCHDOG)) 580a7cba02dSNicholas Piggin set_dec(0x7fffffff); 581a7cba02dSNicholas Piggin else 582a7cba02dSNicholas Piggin set_dec(decrementer_max); 583a7cba02dSNicholas Piggin 5841b783955SPreeti U Murthy /* Conditionally hard-enable interrupts now that the DEC has been 5851b783955SPreeti U Murthy * bumped to its maximum value 5861b783955SPreeti U Murthy */ 5871b783955SPreeti U Murthy may_hard_irq_enable(); 5881b783955SPreeti U Murthy 5891b783955SPreeti U Murthy 5906e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 5911b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 5921b783955SPreeti U Murthy do_IRQ(regs); 5931b783955SPreeti U Murthy #endif 5941b783955SPreeti U Murthy 5951b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 5961b783955SPreeti U Murthy irq_enter(); 5973f984620SNicholas Piggin trace_timer_interrupt_entry(regs); 5981b783955SPreeti U Murthy 5993f984620SNicholas Piggin if (test_irq_work_pending()) { 6003f984620SNicholas Piggin clear_irq_work_pending(); 6013f984620SNicholas Piggin irq_work_run(); 6023f984620SNicholas Piggin } 6033f984620SNicholas Piggin 6043f984620SNicholas Piggin now = get_tb_or_rtc(); 6053f984620SNicholas Piggin if (now >= *next_tb) { 6063f984620SNicholas Piggin *next_tb = ~(u64)0; 6073f984620SNicholas Piggin if (evt->event_handler) 6083f984620SNicholas Piggin evt->event_handler(evt); 6093f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_event); 6103f984620SNicholas Piggin } else { 6113f984620SNicholas Piggin now = *next_tb - now; 6123f984620SNicholas Piggin if (now <= decrementer_max) 6133f984620SNicholas Piggin set_dec(now); 6143f984620SNicholas Piggin /* We may have raced with new irq work */ 6153f984620SNicholas Piggin if (test_irq_work_pending()) 6163f984620SNicholas Piggin set_dec(1); 6173f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_others); 6183f984620SNicholas Piggin } 6193f984620SNicholas Piggin 6203f984620SNicholas Piggin trace_timer_interrupt_exit(regs); 621f2783c15SPaul Mackerras irq_exit(); 6227d12e780SDavid Howells set_irq_regs(old_regs); 623f2783c15SPaul Mackerras } 6249445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 625f2783c15SPaul Mackerras 626bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 6273f984620SNicholas Piggin void timer_broadcast_interrupt(void) 6283f984620SNicholas Piggin { 6293f984620SNicholas Piggin u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 6303f984620SNicholas Piggin 6313f984620SNicholas Piggin *next_tb = ~(u64)0; 6323f984620SNicholas Piggin tick_receive_broadcast(); 633e360cd37SNicholas Piggin __this_cpu_inc(irq_stat.broadcast_irqs_event); 6343f984620SNicholas Piggin } 635bc907113SNicholas Piggin #endif 6363f984620SNicholas Piggin 637dabe859eSPaul Mackerras /* 638dabe859eSPaul Mackerras * Hypervisor decrementer interrupts shouldn't occur but are sometimes 639dabe859eSPaul Mackerras * left pending on exit from a KVM guest. We don't need to do anything 640dabe859eSPaul Mackerras * to clear them, as they are edge-triggered. 641dabe859eSPaul Mackerras */ 642dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs) 643dabe859eSPaul Mackerras { 644dabe859eSPaul Mackerras } 645dabe859eSPaul Mackerras 6467ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 647d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void) 6487ac5dde9SScott Wood { 6497ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6507ac5dde9SScott Wood * with suspending. 6517ac5dde9SScott Wood */ 6527ac5dde9SScott Wood 65379901024SOliver O'Halloran set_dec(decrementer_max); 6547ac5dde9SScott Wood local_irq_disable(); 65579901024SOliver O'Halloran set_dec(decrementer_max); 6567ac5dde9SScott Wood } 6577ac5dde9SScott Wood 658d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void) 6597ac5dde9SScott Wood { 6607ac5dde9SScott Wood local_irq_enable(); 6617ac5dde9SScott Wood } 6627ac5dde9SScott Wood 6637ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6647ac5dde9SScott Wood void arch_suspend_disable_irqs(void) 6657ac5dde9SScott Wood { 6667ac5dde9SScott Wood if (ppc_md.suspend_disable_irqs) 6677ac5dde9SScott Wood ppc_md.suspend_disable_irqs(); 6687ac5dde9SScott Wood generic_suspend_disable_irqs(); 6697ac5dde9SScott Wood } 6707ac5dde9SScott Wood 6717ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6727ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 6737ac5dde9SScott Wood { 6747ac5dde9SScott Wood generic_suspend_enable_irqs(); 6757ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 6767ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 6777ac5dde9SScott Wood } 6787ac5dde9SScott Wood #endif 6797ac5dde9SScott Wood 680b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 681b6c295dfSPaul Mackerras { 682b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 683b6c295dfSPaul Mackerras } 684b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 685b6c295dfSPaul Mackerras 686f2783c15SPaul Mackerras /* 687f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 688f2783c15SPaul Mackerras * 689f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 690f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 691f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 692f2783c15SPaul Mackerras */ 6936b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void) 694f2783c15SPaul Mackerras { 69596c44507SPaul Mackerras if (__USE_RTC()) 69696c44507SPaul Mackerras return get_rtc(); 697fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 698f2783c15SPaul Mackerras } 699f2783c15SPaul Mackerras 7004be1b297SCyril Bur 7014be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 7024be1b297SCyril Bur 7034be1b297SCyril Bur /* 7044be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 7054be1b297SCyril Bur * kernels. 7064be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 7074be1b297SCyril Bur */ 7084be1b297SCyril Bur unsigned long long running_clock(void) 7094be1b297SCyril Bur { 7104be1b297SCyril Bur /* 7114be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 7124be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 7134be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 7144be1b297SCyril Bur * 7154be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7164be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7174be1b297SCyril Bur */ 7184be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7194be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7204be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7214be1b297SCyril Bur 7224be1b297SCyril Bur /* 7234be1b297SCyril Bur * This is a next best approximation without a VTB. 7244be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7254be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7264be1b297SCyril Bur * VTB so it makes no difference anyway. 7274be1b297SCyril Bur */ 7289f3768e0SFrédéric Weisbecker return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 7294be1b297SCyril Bur } 7304be1b297SCyril Bur #endif 7314be1b297SCyril Bur 7320bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 733f2783c15SPaul Mackerras { 734f2783c15SPaul Mackerras struct device_node *cpu; 7356f7aba7bSAnton Blanchard const __be32 *fp; 7360bb474a4SAnton Blanchard int found = 0; 737f2783c15SPaul Mackerras 7380bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 739f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 740f2783c15SPaul Mackerras 741d8a8188dSOlaf Hering if (cpu) { 742e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 743d8a8188dSOlaf Hering if (fp) { 7440bb474a4SAnton Blanchard found = 1; 745a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 746f2783c15SPaul Mackerras } 7470bb474a4SAnton Blanchard 7480bb474a4SAnton Blanchard of_node_put(cpu); 749f2783c15SPaul Mackerras } 7500bb474a4SAnton Blanchard 7510bb474a4SAnton Blanchard return found; 7520bb474a4SAnton Blanchard } 7530bb474a4SAnton Blanchard 754e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 75577c0a700SBenjamin Herrenschmidt { 75677c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 7576e2f03e2SIvan Mikhaylov unsigned int tcr; 7586e2f03e2SIvan Mikhaylov 75977c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 76077c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 76177c0a700SBenjamin Herrenschmidt 7626e2f03e2SIvan Mikhaylov tcr = mfspr(SPRN_TCR); 7636e2f03e2SIvan Mikhaylov /* 7646e2f03e2SIvan Mikhaylov * The watchdog may have already been enabled by u-boot. So leave 7656e2f03e2SIvan Mikhaylov * TRC[WP] (Watchdog Period) alone. 7666e2f03e2SIvan Mikhaylov */ 7676e2f03e2SIvan Mikhaylov tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 7686e2f03e2SIvan Mikhaylov tcr |= TCR_DIE; /* Enable decrementer */ 7696e2f03e2SIvan Mikhaylov mtspr(SPRN_TCR, tcr); 7706e2f03e2SIvan Mikhaylov #endif 77177c0a700SBenjamin Herrenschmidt } 77277c0a700SBenjamin Herrenschmidt 7730bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 7740bb474a4SAnton Blanchard { 7750bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 7760bb474a4SAnton Blanchard 7770bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 7780bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 7790bb474a4SAnton Blanchard 780f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 781f2783c15SPaul Mackerras "(not found)\n"); 7820bb474a4SAnton Blanchard } 783f2783c15SPaul Mackerras 7840bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 7850bb474a4SAnton Blanchard 7860bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 7870bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 7880bb474a4SAnton Blanchard 7890bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 7900bb474a4SAnton Blanchard "(not found)\n"); 791f2783c15SPaul Mackerras } 792f2783c15SPaul Mackerras } 793f2783c15SPaul Mackerras 7945235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now) 795f2783c15SPaul Mackerras { 796f2783c15SPaul Mackerras struct rtc_time tm; 797f2783c15SPaul Mackerras 798aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 799023f333aSJason Gunthorpe return -ENODEV; 800aa3be5f3STony Breeds 8015235afa8SArnd Bergmann rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm); 802aa3be5f3STony Breeds 803aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 804aa3be5f3STony Breeds } 805aa3be5f3STony Breeds 8065bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts) 807aa3be5f3STony Breeds { 808aa3be5f3STony Breeds struct rtc_time tm; 809aa3be5f3STony Breeds static int first = 1; 810aa3be5f3STony Breeds 811d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 812aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 813aa3be5f3STony Breeds if (first) { 814aa3be5f3STony Breeds first = 0; 815aa3be5f3STony Breeds if (ppc_md.time_init) 816aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 817aa3be5f3STony Breeds 818aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 819d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 820d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 821d90246cdSMartin Schwidefsky return; 822aa3be5f3STony Breeds } 823d90246cdSMartin Schwidefsky } 824d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 825d90246cdSMartin Schwidefsky ts->tv_sec = 0; 826d90246cdSMartin Schwidefsky return; 827d90246cdSMartin Schwidefsky } 828f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 829978d7eb3SBenjamin Herrenschmidt 8305bfd6435SArnd Bergmann ts->tv_sec = rtc_tm_to_time64(&tm); 831f2783c15SPaul Mackerras } 832f2783c15SPaul Mackerras 8335bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts) 834978d7eb3SBenjamin Herrenschmidt { 835978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 836978d7eb3SBenjamin Herrenschmidt 837978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 838978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 839978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 840978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 841978d7eb3SBenjamin Herrenschmidt } 842978d7eb3SBenjamin Herrenschmidt 843978d7eb3SBenjamin Herrenschmidt } 844978d7eb3SBenjamin Herrenschmidt 8454a4cfe38STony Breeds /* clocksource code */ 8466b847d79SSantosh Sivaraj static notrace u64 rtc_read(struct clocksource *cs) 8474a4cfe38STony Breeds { 848a5a1d1c2SThomas Gleixner return (u64)get_rtc(); 8494a4cfe38STony Breeds } 8504a4cfe38STony Breeds 8516b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs) 8524a4cfe38STony Breeds { 853a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8544a4cfe38STony Breeds } 8554a4cfe38STony Breeds 856d4cfb113SPaul Mackerras 857d4cfb113SPaul Mackerras void update_vsyscall(struct timekeeper *tk) 8584a4cfe38STony Breeds { 859d4cfb113SPaul Mackerras struct timespec xt; 860d4cfb113SPaul Mackerras struct clocksource *clock = tk->tkr_mono.clock; 861d4cfb113SPaul Mackerras u32 mult = tk->tkr_mono.mult; 862d4cfb113SPaul Mackerras u32 shift = tk->tkr_mono.shift; 863d4cfb113SPaul Mackerras u64 cycle_last = tk->tkr_mono.cycle_last; 864b0797b60SJohn Stultz u64 new_tb_to_xs, new_stamp_xsec; 865d4cfb113SPaul Mackerras u64 frac_sec; 8664a4cfe38STony Breeds 8674a4cfe38STony Breeds if (clock != &clocksource_timebase) 8684a4cfe38STony Breeds return; 8694a4cfe38STony Breeds 870d4cfb113SPaul Mackerras xt.tv_sec = tk->xtime_sec; 871d4cfb113SPaul Mackerras xt.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 872d4cfb113SPaul Mackerras 8734a4cfe38STony Breeds /* Make userspace gettimeofday spin until we're done. */ 8744a4cfe38STony Breeds ++vdso_data->tb_update_count; 8754a4cfe38STony Breeds smp_mb(); 8764a4cfe38STony Breeds 877d4cfb113SPaul Mackerras /* 878d4cfb113SPaul Mackerras * This computes ((2^20 / 1e9) * mult) >> shift as a 879d4cfb113SPaul Mackerras * 0.64 fixed-point fraction. 880d4cfb113SPaul Mackerras * The computation in the else clause below won't overflow 881d4cfb113SPaul Mackerras * (as long as the timebase frequency is >= 1.049 MHz) 882d4cfb113SPaul Mackerras * but loses precision because we lose the low bits of the constant 883d4cfb113SPaul Mackerras * in the shift. Note that 19342813113834067 ~= 2^(20+64) / 1e9. 884d4cfb113SPaul Mackerras * For a shift of 24 the error is about 0.5e-9, or about 0.5ns 885d4cfb113SPaul Mackerras * over a second. (Shift values are usually 22, 23 or 24.) 886d4cfb113SPaul Mackerras * For high frequency clocks such as the 512MHz timebase clock 887d4cfb113SPaul Mackerras * on POWER[6789], the mult value is small (e.g. 32768000) 888d4cfb113SPaul Mackerras * and so we can shift the constant by 16 initially 889d4cfb113SPaul Mackerras * (295147905179 ~= 2^(20+64-16) / 1e9) and then do the 890d4cfb113SPaul Mackerras * remaining shifts after the multiplication, which gives a 891d4cfb113SPaul Mackerras * more accurate result (e.g. with mult = 32768000, shift = 24, 892d4cfb113SPaul Mackerras * the error is only about 1.2e-12, or 0.7ns over 10 minutes). 893d4cfb113SPaul Mackerras */ 894d4cfb113SPaul Mackerras if (mult <= 62500000 && clock->shift >= 16) 895d4cfb113SPaul Mackerras new_tb_to_xs = ((u64) mult * 295147905179ULL) >> (clock->shift - 16); 896d4cfb113SPaul Mackerras else 89711b8633aSAnton Blanchard new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift); 898b0797b60SJohn Stultz 899d4cfb113SPaul Mackerras /* 900d4cfb113SPaul Mackerras * Compute the fractional second in units of 2^-32 seconds. 901d4cfb113SPaul Mackerras * The fractional second is tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift 902d4cfb113SPaul Mackerras * in nanoseconds, so multiplying that by 2^32 / 1e9 gives 903d4cfb113SPaul Mackerras * it in units of 2^-32 seconds. 904d4cfb113SPaul Mackerras * We assume shift <= 32 because clocks_calc_mult_shift() 905d4cfb113SPaul Mackerras * generates shift values in the range 0 - 32. 906d4cfb113SPaul Mackerras */ 907d4cfb113SPaul Mackerras frac_sec = tk->tkr_mono.xtime_nsec << (32 - shift); 908d4cfb113SPaul Mackerras do_div(frac_sec, NSEC_PER_SEC); 909d4cfb113SPaul Mackerras 910d4cfb113SPaul Mackerras /* 911d4cfb113SPaul Mackerras * Work out new stamp_xsec value for any legacy users of systemcfg. 912d4cfb113SPaul Mackerras * stamp_xsec is in units of 2^-20 seconds. 913d4cfb113SPaul Mackerras */ 914d4cfb113SPaul Mackerras new_stamp_xsec = frac_sec >> 12; 915d4cfb113SPaul Mackerras new_stamp_xsec += tk->xtime_sec * XSEC_PER_SEC; 91647916be4SThomas Gleixner 917b0797b60SJohn Stultz /* 918b0797b60SJohn Stultz * tb_update_count is used to allow the userspace gettimeofday code 919b0797b60SJohn Stultz * to assure itself that it sees a consistent view of the tb_to_xs and 920b0797b60SJohn Stultz * stamp_xsec variables. It reads the tb_update_count, then reads 921b0797b60SJohn Stultz * tb_to_xs and stamp_xsec and then reads tb_update_count again. If 922b0797b60SJohn Stultz * the two values of tb_update_count match and are even then the 923b0797b60SJohn Stultz * tb_to_xs and stamp_xsec values are consistent. If not, then it 924b0797b60SJohn Stultz * loops back and reads them again until this criteria is met. 925b0797b60SJohn Stultz */ 9264a0e6377SThomas Gleixner vdso_data->tb_orig_stamp = cycle_last; 927b0797b60SJohn Stultz vdso_data->stamp_xsec = new_stamp_xsec; 928b0797b60SJohn Stultz vdso_data->tb_to_xs = new_tb_to_xs; 929d4cfb113SPaul Mackerras vdso_data->wtom_clock_sec = tk->wall_to_monotonic.tv_sec; 930d4cfb113SPaul Mackerras vdso_data->wtom_clock_nsec = tk->wall_to_monotonic.tv_nsec; 931d4cfb113SPaul Mackerras vdso_data->stamp_xtime = xt; 93247916be4SThomas Gleixner vdso_data->stamp_sec_fraction = frac_sec; 933b0797b60SJohn Stultz smp_wmb(); 934b0797b60SJohn Stultz ++(vdso_data->tb_update_count); 9354a4cfe38STony Breeds } 9364a4cfe38STony Breeds 9374a4cfe38STony Breeds void update_vsyscall_tz(void) 9384a4cfe38STony Breeds { 9394a4cfe38STony Breeds vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 9404a4cfe38STony Breeds vdso_data->tz_dsttime = sys_tz.tz_dsttime; 9414a4cfe38STony Breeds } 9424a4cfe38STony Breeds 9431c21a293SMichael Ellerman static void __init clocksource_init(void) 9444a4cfe38STony Breeds { 9454a4cfe38STony Breeds struct clocksource *clock; 9464a4cfe38STony Breeds 9474a4cfe38STony Breeds if (__USE_RTC()) 9484a4cfe38STony Breeds clock = &clocksource_rtc; 9494a4cfe38STony Breeds else 9504a4cfe38STony Breeds clock = &clocksource_timebase; 9514a4cfe38STony Breeds 95211b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 9534a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 9544a4cfe38STony Breeds clock->name); 9554a4cfe38STony Breeds return; 9564a4cfe38STony Breeds } 9574a4cfe38STony Breeds 9584a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 9594a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 9604a4cfe38STony Breeds } 9614a4cfe38STony Breeds 962d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 963d831d0b8STony Breeds struct clock_event_device *dev) 964d831d0b8STony Breeds { 96569111bacSChristoph Lameter __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt); 966d831d0b8STony Breeds set_dec(evt); 9670215f7d8SBenjamin Herrenschmidt 9680215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 9690215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 9700215f7d8SBenjamin Herrenschmidt set_dec(1); 9710215f7d8SBenjamin Herrenschmidt 972d831d0b8STony Breeds return 0; 973d831d0b8STony Breeds } 974d831d0b8STony Breeds 97537a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 976d831d0b8STony Breeds { 97779901024SOliver O'Halloran decrementer_set_next_event(decrementer_max, dev); 97837a13e78SViresh Kumar return 0; 979d831d0b8STony Breeds } 980d831d0b8STony Breeds 981d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 982d831d0b8STony Breeds { 9837df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 984d831d0b8STony Breeds 985d831d0b8STony Breeds *dec = decrementer_clockevent; 986320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 987d831d0b8STony Breeds 9888b78fdb0SAnton Blanchard clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max); 9898b78fdb0SAnton Blanchard 990b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 991d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 992d831d0b8STony Breeds } 993d831d0b8STony Breeds 99479901024SOliver O'Halloran static void enable_large_decrementer(void) 99579901024SOliver O'Halloran { 99679901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 99779901024SOliver O'Halloran return; 99879901024SOliver O'Halloran 99979901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 100079901024SOliver O'Halloran return; 100179901024SOliver O'Halloran 100279901024SOliver O'Halloran /* 100379901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 100479901024SOliver O'Halloran * otherwise firmware should have done it for us. 100579901024SOliver O'Halloran */ 100679901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 100779901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 100879901024SOliver O'Halloran } 100979901024SOliver O'Halloran 101079901024SOliver O'Halloran static void __init set_decrementer_max(void) 101179901024SOliver O'Halloran { 101279901024SOliver O'Halloran struct device_node *cpu; 101379901024SOliver O'Halloran u32 bits = 32; 101479901024SOliver O'Halloran 101579901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 101679901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 101779901024SOliver O'Halloran return; 101879901024SOliver O'Halloran 101979901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 102079901024SOliver O'Halloran 102179901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 102279901024SOliver O'Halloran if (bits > 64 || bits < 32) { 102379901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 102479901024SOliver O'Halloran bits = 32; 102579901024SOliver O'Halloran } 102679901024SOliver O'Halloran 102779901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 102879901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 102979901024SOliver O'Halloran } 103079901024SOliver O'Halloran 103179901024SOliver O'Halloran of_node_put(cpu); 103279901024SOliver O'Halloran 103379901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 103479901024SOliver O'Halloran bits, decrementer_max); 103579901024SOliver O'Halloran } 103679901024SOliver O'Halloran 1037c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 1038d831d0b8STony Breeds { 10398b78fdb0SAnton Blanchard register_decrementer_clockevent(smp_processor_id()); 1040d831d0b8STony Breeds } 1041d831d0b8STony Breeds 1042d831d0b8STony Breeds void secondary_cpu_time_init(void) 1043d831d0b8STony Breeds { 104479901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 104579901024SOliver O'Halloran enable_large_decrementer(); 104679901024SOliver O'Halloran 104777c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 104877c0a700SBenjamin Herrenschmidt * such as BookE 104977c0a700SBenjamin Herrenschmidt */ 105077c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 105177c0a700SBenjamin Herrenschmidt 1052d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 1053d831d0b8STony Breeds * call here ! */ 1054d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 1055d831d0b8STony Breeds } 1056d831d0b8STony Breeds 1057f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 1058f2783c15SPaul Mackerras void __init time_init(void) 1059f2783c15SPaul Mackerras { 1060f2783c15SPaul Mackerras struct div_result res; 1061d75d68cfSPaul Mackerras u64 scale; 1062f2783c15SPaul Mackerras unsigned shift; 1063f2783c15SPaul Mackerras 106496c44507SPaul Mackerras if (__USE_RTC()) { 106596c44507SPaul Mackerras /* 601 processor: dec counts down by 128 every 128ns */ 106696c44507SPaul Mackerras ppc_tb_freq = 1000000000; 106796c44507SPaul Mackerras } else { 106896c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 1069f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 1070224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 1071374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 1072224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 1073374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 107496c44507SPaul Mackerras } 1075374e99d4SPaul Mackerras 1076374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 1077092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 1078374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 1079c6622f63SPaul Mackerras calc_cputime_factors(); 1080092b8f34SPaul Mackerras 1081092b8f34SPaul Mackerras /* 1082f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 1083f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 1084f2783c15SPaul Mackerras * which is the timebase frequency. 1085f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 1086f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 1087f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 1088f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 1089f2783c15SPaul Mackerras * sched_clock(). 1090f2783c15SPaul Mackerras */ 1091f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1092f2783c15SPaul Mackerras scale = res.result_low; 1093f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1094f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1095f2783c15SPaul Mackerras res.result_high >>= 1; 1096f2783c15SPaul Mackerras } 1097f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1098f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1099fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 1100c27da339SBenjamin Herrenschmidt boot_tb = get_tb_or_rtc(); 1101f2783c15SPaul Mackerras 1102092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1103092b8f34SPaul Mackerras if (timezone_offset) { 1104092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1105092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1106092b8f34SPaul Mackerras } 1107092b8f34SPaul Mackerras 1108a7f290daSBenjamin Herrenschmidt vdso_data->tb_update_count = 0; 1109a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1110f2783c15SPaul Mackerras 111179901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 111279901024SOliver O'Halloran set_decrementer_max(); 111379901024SOliver O'Halloran enable_large_decrementer(); 111479901024SOliver O'Halloran 111577c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 111677c0a700SBenjamin Herrenschmidt * such as BookE 111777c0a700SBenjamin Herrenschmidt */ 111877c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 111977c0a700SBenjamin Herrenschmidt 1120f5339277SStephen Rothwell /* Register the clocksource */ 11214a4cfe38STony Breeds clocksource_init(); 11224a4cfe38STony Breeds 1123d831d0b8STony Breeds init_decrementer_clockevent(); 11240d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1125f0d37300SKevin Hao 1126f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK 1127f0d37300SKevin Hao of_clk_init(NULL); 1128f0d37300SKevin Hao #endif 1129f2783c15SPaul Mackerras } 1130f2783c15SPaul Mackerras 1131f2783c15SPaul Mackerras /* 1132f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1133f2783c15SPaul Mackerras * result. 1134f2783c15SPaul Mackerras */ 1135f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1136f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1137f2783c15SPaul Mackerras { 1138f2783c15SPaul Mackerras unsigned long a, b, c, d; 1139f2783c15SPaul Mackerras unsigned long w, x, y, z; 1140f2783c15SPaul Mackerras u64 ra, rb, rc; 1141f2783c15SPaul Mackerras 1142f2783c15SPaul Mackerras a = dividend_high >> 32; 1143f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1144f2783c15SPaul Mackerras c = dividend_low >> 32; 1145f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1146f2783c15SPaul Mackerras 1147f2783c15SPaul Mackerras w = a / divisor; 1148f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1149f2783c15SPaul Mackerras 1150f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1151f2783c15SPaul Mackerras x = ra; 1152f2783c15SPaul Mackerras 1153f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1154f2783c15SPaul Mackerras y = rb; 1155f2783c15SPaul Mackerras 1156f2783c15SPaul Mackerras do_div(rc, divisor); 1157f2783c15SPaul Mackerras z = rc; 1158f2783c15SPaul Mackerras 1159f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1160f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1161f2783c15SPaul Mackerras 1162f2783c15SPaul Mackerras } 1163bcd68a70SGeert Uytterhoeven 1164177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1165177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1166177996e6SBenjamin Herrenschmidt { 1167177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1168177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1169177996e6SBenjamin Herrenschmidt */ 1170177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1171177996e6SBenjamin Herrenschmidt } 1172177996e6SBenjamin Herrenschmidt 1173169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1174169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1175169047f4SArnd Bergmann { 1176169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1177890ae797SAlexandre Belloni return 0; 1178169047f4SArnd Bergmann } 1179169047f4SArnd Bergmann 1180169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1181169047f4SArnd Bergmann { 1182169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1183169047f4SArnd Bergmann return -EOPNOTSUPP; 1184169047f4SArnd Bergmann 1185169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1186169047f4SArnd Bergmann return -EOPNOTSUPP; 1187169047f4SArnd Bergmann 1188169047f4SArnd Bergmann return 0; 1189169047f4SArnd Bergmann } 1190169047f4SArnd Bergmann 1191169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1192169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1193169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1194169047f4SArnd Bergmann }; 1195169047f4SArnd Bergmann 1196bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1197bcd68a70SGeert Uytterhoeven { 1198bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1199bcd68a70SGeert Uytterhoeven 1200bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1201bcd68a70SGeert Uytterhoeven return -ENODEV; 1202bcd68a70SGeert Uytterhoeven 1203169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1204169047f4SArnd Bergmann &rtc_generic_ops, 1205169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1206bcd68a70SGeert Uytterhoeven 12078c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1208bcd68a70SGeert Uytterhoeven } 1209bcd68a70SGeert Uytterhoeven 12108f6b9512SPaul Gortmaker device_initcall(rtc_init); 1211169047f4SArnd Bergmann #endif 1212