12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2f2783c15SPaul Mackerras /* 3f2783c15SPaul Mackerras * Common time routines among all ppc machines. 4f2783c15SPaul Mackerras * 5f2783c15SPaul Mackerras * Written by Cort Dougan (cort@cs.nmt.edu) to merge 6f2783c15SPaul Mackerras * Paul Mackerras' version and mine for PReP and Pmac. 7f2783c15SPaul Mackerras * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). 8f2783c15SPaul Mackerras * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) 9f2783c15SPaul Mackerras * 10f2783c15SPaul Mackerras * First round of bugfixes by Gabriel Paubert (paubert@iram.es) 11f2783c15SPaul Mackerras * to make clock more stable (2.4.0-test5). The only thing 12f2783c15SPaul Mackerras * that this code assumes is that the timebases have been synchronized 13f2783c15SPaul Mackerras * by firmware on SMP and are never stopped (never do sleep 14f2783c15SPaul Mackerras * on SMP then, nap and doze are OK). 15f2783c15SPaul Mackerras * 16f2783c15SPaul Mackerras * Speeded up do_gettimeofday by getting rid of references to 17f2783c15SPaul Mackerras * xtime (which required locks for consistency). (mikejc@us.ibm.com) 18f2783c15SPaul Mackerras * 19f2783c15SPaul Mackerras * TODO (not necessarily in this file): 20f2783c15SPaul Mackerras * - improve precision and reproducibility of timebase frequency 21f5339277SStephen Rothwell * measurement at boot time. 22f2783c15SPaul Mackerras * - for astronomical applications: add a new function to get 23f2783c15SPaul Mackerras * non ambiguous timestamps even around leap seconds. This needs 24f2783c15SPaul Mackerras * a new timestamp format and a good name. 25f2783c15SPaul Mackerras * 26f2783c15SPaul Mackerras * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 27f2783c15SPaul Mackerras * "A Kernel Model for Precision Timekeeping" by Dave Mills 28f2783c15SPaul Mackerras */ 29f2783c15SPaul Mackerras 30f2783c15SPaul Mackerras #include <linux/errno.h> 314b16f8e2SPaul Gortmaker #include <linux/export.h> 32f2783c15SPaul Mackerras #include <linux/sched.h> 33e6017571SIngo Molnar #include <linux/sched/clock.h> 34e225c4d6SWan Jiabing #include <linux/sched/cputime.h> 35f2783c15SPaul Mackerras #include <linux/kernel.h> 36f2783c15SPaul Mackerras #include <linux/param.h> 37f2783c15SPaul Mackerras #include <linux/string.h> 38f2783c15SPaul Mackerras #include <linux/mm.h> 39f2783c15SPaul Mackerras #include <linux/interrupt.h> 40f2783c15SPaul Mackerras #include <linux/timex.h> 41f2783c15SPaul Mackerras #include <linux/kernel_stat.h> 42f2783c15SPaul Mackerras #include <linux/time.h> 43f2783c15SPaul Mackerras #include <linux/init.h> 44f2783c15SPaul Mackerras #include <linux/profile.h> 45f2783c15SPaul Mackerras #include <linux/cpu.h> 46f2783c15SPaul Mackerras #include <linux/security.h> 47f2783c15SPaul Mackerras #include <linux/percpu.h> 48f2783c15SPaul Mackerras #include <linux/rtc.h> 49092b8f34SPaul Mackerras #include <linux/jiffies.h> 50c6622f63SPaul Mackerras #include <linux/posix-timers.h> 517d12e780SDavid Howells #include <linux/irq.h> 52177996e6SBenjamin Herrenschmidt #include <linux/delay.h> 53e360adbeSPeter Zijlstra #include <linux/irq_work.h> 5460083063SGeert Uytterhoeven #include <linux/of_clk.h> 557f92bc56SDaniel Axtens #include <linux/suspend.h> 564e287e65SNicholas Piggin #include <linux/processor.h> 576795b85cSAnton Blanchard #include <asm/trace.h> 58f2783c15SPaul Mackerras 593a96570fSNicholas Piggin #include <asm/interrupt.h> 60f2783c15SPaul Mackerras #include <asm/io.h> 61f2783c15SPaul Mackerras #include <asm/nvram.h> 62f2783c15SPaul Mackerras #include <asm/cache.h> 63f2783c15SPaul Mackerras #include <asm/machdep.h> 647c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 65f2783c15SPaul Mackerras #include <asm/time.h> 66f2783c15SPaul Mackerras #include <asm/prom.h> 67f2783c15SPaul Mackerras #include <asm/irq.h> 68f2783c15SPaul Mackerras #include <asm/div64.h> 692249ca9dSPaul Mackerras #include <asm/smp.h> 70a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h> 71f2783c15SPaul Mackerras #include <asm/firmware.h> 72cc15ff32SGanesh Goudar #include <asm/mce.h> 73f2783c15SPaul Mackerras 744a4cfe38STony Breeds /* powerpc clocksource/clockevent code */ 754a4cfe38STony Breeds 76d831d0b8STony Breeds #include <linux/clockchips.h> 77189374aeSJohn Stultz #include <linux/timekeeper_internal.h> 784a4cfe38STony Breeds 79a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *); 804a4cfe38STony Breeds static struct clocksource clocksource_timebase = { 814a4cfe38STony Breeds .name = "timebase", 824a4cfe38STony Breeds .rating = 400, 834a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 844a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 854a4cfe38STony Breeds .read = timebase_read, 86ab037dd8SChristophe Leroy .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER, 874a4cfe38STony Breeds }; 884a4cfe38STony Breeds 8979901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 9079901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 919581991aSNicholas Piggin EXPORT_SYMBOL_GPL(decrementer_max); /* for KVM HDEC */ 92d831d0b8STony Breeds 93d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 94d831d0b8STony Breeds struct clock_event_device *dev); 9537a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt); 96d831d0b8STony Breeds 976e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = { 98d831d0b8STony Breeds .name = "decrementer", 99d831d0b8STony Breeds .rating = 200, 100d831d0b8STony Breeds .irq = 0, 101d831d0b8STony Breeds .set_next_event = decrementer_set_next_event, 10281759360SAnton Blanchard .set_state_oneshot_stopped = decrementer_shutdown, 10337a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 10437a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 10537a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 10637a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 107d831d0b8STony Breeds }; 1086e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 109d831d0b8STony Breeds 110*35de589cSNicholas Piggin /* 111*35de589cSNicholas Piggin * This always puts next_tb beyond now, so the clock event will never fire 112*35de589cSNicholas Piggin * with the usual comparison, no need for a separate test for stopped. 113*35de589cSNicholas Piggin */ 114*35de589cSNicholas Piggin #define DEC_CLOCKEVENT_STOPPED ~0ULL 115*35de589cSNicholas Piggin DEFINE_PER_CPU(u64, decrementers_next_tb) = DEC_CLOCKEVENT_STOPPED; 1164ebbd075SNicholas Piggin EXPORT_SYMBOL_GPL(decrementers_next_tb); 1177df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers); 118d831d0b8STony Breeds 119f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024) 120f2783c15SPaul Mackerras 121f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 122f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 123f2783c15SPaul Mackerras #else 124f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */ 125f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 126f2783c15SPaul Mackerras #endif 127f2783c15SPaul Mackerras 128f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy; 129f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */ 130f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec); 131f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec; 1322cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 133092b8f34SPaul Mackerras 134f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock); 135f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock); 136f2783c15SPaul Mackerras 137fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly; 138fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly; 139364a1246SHeiko Schocher static u64 boot_tb __read_mostly; 140f2783c15SPaul Mackerras 141f2783c15SPaul Mackerras extern struct timezone sys_tz; 142f2783c15SPaul Mackerras static long timezone_offset; 143f2783c15SPaul Mackerras 144f2783c15SPaul Mackerras unsigned long ppc_proc_freq; 14555ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq); 146f2783c15SPaul Mackerras unsigned long ppc_tb_freq; 14755ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq); 14896c44507SPaul Mackerras 149de269129SMahesh Salgaonkar bool tb_invalid; 150de269129SMahesh Salgaonkar 151abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 152c6622f63SPaul Mackerras /* 153e7f340caSFrederic Weisbecker * Factor for converting from cputime_t (timebase ticks) to 154e7f340caSFrederic Weisbecker * microseconds. This is stored as 0.64 fixed-point binary fraction. 155c6622f63SPaul Mackerras */ 1569f5072d4SAndreas Schwab u64 __cputime_usec_factor; 1579f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor); 158a42548a1SStanislaw Gruszka 159c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR 160872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64); 161c223c903SChristophe Leroy #endif 162c223c903SChristophe Leroy 163c6622f63SPaul Mackerras static void calc_cputime_factors(void) 164c6622f63SPaul Mackerras { 165c6622f63SPaul Mackerras struct div_result res; 166c6622f63SPaul Mackerras 1679f5072d4SAndreas Schwab div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 1689f5072d4SAndreas Schwab __cputime_usec_factor = res.result_low; 169c6622f63SPaul Mackerras } 170c6622f63SPaul Mackerras 171c6622f63SPaul Mackerras /* 172cf9efce0SPaul Mackerras * Read the SPURR on systems that have it, otherwise the PURR, 173cf9efce0SPaul Mackerras * or if that doesn't exist return the timebase value passed in. 174c6622f63SPaul Mackerras */ 175abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb) 176c6622f63SPaul Mackerras { 177cf9efce0SPaul Mackerras if (cpu_has_feature(CPU_FTR_SPURR)) 178cf9efce0SPaul Mackerras return mfspr(SPRN_SPURR); 179c6622f63SPaul Mackerras if (cpu_has_feature(CPU_FTR_PURR)) 180c6622f63SPaul Mackerras return mfspr(SPRN_PURR); 181cf9efce0SPaul Mackerras return tb; 182cf9efce0SPaul Mackerras } 183cf9efce0SPaul Mackerras 184cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR 185cf9efce0SPaul Mackerras 186d6bdceb6SPeter Zijlstra #include <asm/dtl.h> 187d6bdceb6SPeter Zijlstra 188cf9efce0SPaul Mackerras /* 189cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 190cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 191cf9efce0SPaul Mackerras */ 192cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 193cf9efce0SPaul Mackerras { 194872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 195cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 196cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 197cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 198cf9efce0SPaul Mackerras u64 tb_delta; 199cf9efce0SPaul Mackerras u64 stolen = 0; 200cf9efce0SPaul Mackerras u64 dtb; 201cf9efce0SPaul Mackerras 20284ffae55SAnton Blanchard if (!dtl) 20384ffae55SAnton Blanchard return 0; 20484ffae55SAnton Blanchard 2057ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 206cf9efce0SPaul Mackerras return 0; 2077ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2087ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2097ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2107ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 211cf9efce0SPaul Mackerras barrier(); 2127ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 213cf9efce0SPaul Mackerras /* buffer has overflowed */ 2147ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 215cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 216cf9efce0SPaul Mackerras continue; 217cf9efce0SPaul Mackerras } 218cf9efce0SPaul Mackerras if (dtb > stop_tb) 219cf9efce0SPaul Mackerras break; 22084b07386SAnton Blanchard if (dtl_consumer) 22184b07386SAnton Blanchard dtl_consumer(dtl, i); 222cf9efce0SPaul Mackerras stolen += tb_delta; 223cf9efce0SPaul Mackerras ++i; 224cf9efce0SPaul Mackerras ++dtl; 225cf9efce0SPaul Mackerras if (dtl == dtl_end) 226cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 227cf9efce0SPaul Mackerras } 228cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 229cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 230cf9efce0SPaul Mackerras return stolen; 231c6622f63SPaul Mackerras } 232c6622f63SPaul Mackerras 233c6622f63SPaul Mackerras /* 234cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 235cf9efce0SPaul Mackerras * Called on entry from user mode. 2364603ac18SMichael Neuling */ 237eb8e20f8SMichael Ellerman void notrace accumulate_stolen_time(void) 2384603ac18SMichael Neuling { 239cf9efce0SPaul Mackerras u64 sst, ust; 240c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 241b18ae08dSTejun Heo 242c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 243c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2448c8b73c4SFrederic Weisbecker acct->stime -= sst; 2458c8b73c4SFrederic Weisbecker acct->utime -= ust; 246f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 2474603ac18SMichael Neuling } 2484603ac18SMichael Neuling 249cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 250cf9efce0SPaul Mackerras { 251a6201da3SAneesh Kumar K.V if (!firmware_has_feature(FW_FEATURE_SPLPAR)) 252a6201da3SAneesh Kumar K.V return 0; 253a6201da3SAneesh Kumar K.V 254a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 255a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 256cf9efce0SPaul Mackerras 257a19ff1a2SFrederic Weisbecker return 0; 258cf9efce0SPaul Mackerras } 259cf9efce0SPaul Mackerras 260cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 261cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 262cf9efce0SPaul Mackerras { 263cf9efce0SPaul Mackerras return 0; 264cf9efce0SPaul Mackerras } 265cf9efce0SPaul Mackerras 266cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 267cf9efce0SPaul Mackerras 2684603ac18SMichael Neuling /* 269c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 270c6622f63SPaul Mackerras * or soft irq state. 271c6622f63SPaul Mackerras */ 272b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct, 273b38a181cSChristophe Leroy unsigned long now, unsigned long stime) 274c6622f63SPaul Mackerras { 275abcff86dSChristophe Leroy unsigned long stime_scaled = 0; 276abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 277b38a181cSChristophe Leroy unsigned long nowscaled, deltascaled; 278a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 279c6622f63SPaul Mackerras 2804603ac18SMichael Neuling nowscaled = read_spurr(now); 281c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 282c223c903SChristophe Leroy acct->startspurr = nowscaled; 283a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 2848c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 285cf9efce0SPaul Mackerras 286cf9efce0SPaul Mackerras /* 287cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 288cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 289cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 290cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 291cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 292cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 293cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 294cf9efce0SPaul Mackerras * used by account_process_tick. 295cf9efce0SPaul Mackerras */ 296b38a181cSChristophe Leroy stime_scaled = stime; 297a19ff1a2SFrederic Weisbecker utime_scaled = utime; 298a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 299a19ff1a2SFrederic Weisbecker if (utime) { 300b38a181cSChristophe Leroy stime_scaled = deltascaled * stime / (stime + utime); 301b38a181cSChristophe Leroy utime_scaled = deltascaled - stime_scaled; 302cf9efce0SPaul Mackerras } else { 303b38a181cSChristophe Leroy stime_scaled = deltascaled; 304c6622f63SPaul Mackerras } 305cf9efce0SPaul Mackerras } 306a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 307abcff86dSChristophe Leroy #endif 308cf9efce0SPaul Mackerras 309b38a181cSChristophe Leroy return stime_scaled; 310b38a181cSChristophe Leroy } 311b38a181cSChristophe Leroy 3128a6a5920SFrederic Weisbecker static unsigned long vtime_delta(struct cpu_accounting_data *acct, 313b38a181cSChristophe Leroy unsigned long *stime_scaled, 314b38a181cSChristophe Leroy unsigned long *steal_time) 315b38a181cSChristophe Leroy { 316b38a181cSChristophe Leroy unsigned long now, stime; 317b38a181cSChristophe Leroy 318b38a181cSChristophe Leroy WARN_ON_ONCE(!irqs_disabled()); 319b38a181cSChristophe Leroy 320b38a181cSChristophe Leroy now = mftb(); 321b38a181cSChristophe Leroy stime = now - acct->starttime; 322b38a181cSChristophe Leroy acct->starttime = now; 323b38a181cSChristophe Leroy 324b38a181cSChristophe Leroy *stime_scaled = vtime_delta_scaled(acct, now, stime); 325b38a181cSChristophe Leroy 326b38a181cSChristophe Leroy *steal_time = calculate_stolen_time(now); 327b38a181cSChristophe Leroy 328a19ff1a2SFrederic Weisbecker return stime; 329a7e1a9e3SFrederic Weisbecker } 330a7e1a9e3SFrederic Weisbecker 3318a6a5920SFrederic Weisbecker static void vtime_delta_kernel(struct cpu_accounting_data *acct, 3328a6a5920SFrederic Weisbecker unsigned long *stime, unsigned long *stime_scaled) 3338a6a5920SFrederic Weisbecker { 3348a6a5920SFrederic Weisbecker unsigned long steal_time; 3358a6a5920SFrederic Weisbecker 3368a6a5920SFrederic Weisbecker *stime = vtime_delta(acct, stime_scaled, &steal_time); 3378a6a5920SFrederic Weisbecker *stime -= min(*stime, steal_time); 3388a6a5920SFrederic Weisbecker acct->steal_time += steal_time; 3398a6a5920SFrederic Weisbecker } 3408a6a5920SFrederic Weisbecker 341f83eeb1aSFrederic Weisbecker void vtime_account_kernel(struct task_struct *tsk) 342a7e1a9e3SFrederic Weisbecker { 343a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3448a6a5920SFrederic Weisbecker unsigned long stime, stime_scaled; 345a7e1a9e3SFrederic Weisbecker 3468a6a5920SFrederic Weisbecker vtime_delta_kernel(acct, &stime, &stime_scaled); 347a19ff1a2SFrederic Weisbecker 3488a6a5920SFrederic Weisbecker if (tsk->flags & PF_VCPU) { 349a19ff1a2SFrederic Weisbecker acct->gtime += stime; 350abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 351a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 352abcff86dSChristophe Leroy #endif 353a19ff1a2SFrederic Weisbecker } else { 354a19ff1a2SFrederic Weisbecker acct->stime += stime; 355abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 356a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 357abcff86dSChristophe Leroy #endif 358a19ff1a2SFrederic Weisbecker } 359a7e1a9e3SFrederic Weisbecker } 360f83eeb1aSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_kernel); 361a7e1a9e3SFrederic Weisbecker 362fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 363a7e1a9e3SFrederic Weisbecker { 364a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 365a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 366a7e1a9e3SFrederic Weisbecker 3678a6a5920SFrederic Weisbecker stime = vtime_delta(acct, &stime_scaled, &steal_time); 368a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 369cf9efce0SPaul Mackerras } 370c6622f63SPaul Mackerras 3718a6a5920SFrederic Weisbecker static void vtime_account_irq_field(struct cpu_accounting_data *acct, 3728a6a5920SFrederic Weisbecker unsigned long *field) 3738a6a5920SFrederic Weisbecker { 3748a6a5920SFrederic Weisbecker unsigned long stime, stime_scaled; 3758a6a5920SFrederic Weisbecker 3768a6a5920SFrederic Weisbecker vtime_delta_kernel(acct, &stime, &stime_scaled); 3778a6a5920SFrederic Weisbecker *field += stime; 3788a6a5920SFrederic Weisbecker #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 3798a6a5920SFrederic Weisbecker acct->stime_scaled += stime_scaled; 3808a6a5920SFrederic Weisbecker #endif 3818a6a5920SFrederic Weisbecker } 3828a6a5920SFrederic Weisbecker 3838a6a5920SFrederic Weisbecker void vtime_account_softirq(struct task_struct *tsk) 3848a6a5920SFrederic Weisbecker { 3858a6a5920SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3868a6a5920SFrederic Weisbecker vtime_account_irq_field(acct, &acct->softirq_time); 3878a6a5920SFrederic Weisbecker } 3888a6a5920SFrederic Weisbecker 3898a6a5920SFrederic Weisbecker void vtime_account_hardirq(struct task_struct *tsk) 3908a6a5920SFrederic Weisbecker { 3918a6a5920SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3928a6a5920SFrederic Weisbecker vtime_account_irq_field(acct, &acct->hardirq_time); 3938a6a5920SFrederic Weisbecker } 3948a6a5920SFrederic Weisbecker 395b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk, 396b38a181cSChristophe Leroy struct cpu_accounting_data *acct) 397b38a181cSChristophe Leroy { 398abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 399b38a181cSChristophe Leroy if (acct->utime_scaled) 400b38a181cSChristophe Leroy tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 401b38a181cSChristophe Leroy if (acct->stime_scaled) 402b38a181cSChristophe Leroy tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 403b38a181cSChristophe Leroy 404b38a181cSChristophe Leroy acct->utime_scaled = 0; 405b38a181cSChristophe Leroy acct->utime_sspurr = 0; 406b38a181cSChristophe Leroy acct->stime_scaled = 0; 407abcff86dSChristophe Leroy #endif 408b38a181cSChristophe Leroy } 409b38a181cSChristophe Leroy 410c6622f63SPaul Mackerras /* 411c8d7dabfSFrederic Weisbecker * Account the whole cputime accumulated in the paca 412c6622f63SPaul Mackerras * Must be called with interrupts disabled. 413f83eeb1aSFrederic Weisbecker * Assumes that vtime_account_kernel/idle() has been called 414bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 415cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 416c6622f63SPaul Mackerras */ 417c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk) 418c6622f63SPaul Mackerras { 419c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 420c6622f63SPaul Mackerras 421a19ff1a2SFrederic Weisbecker if (acct->utime) 42223244a5cSFrederic Weisbecker account_user_time(tsk, cputime_to_nsecs(acct->utime)); 423a19ff1a2SFrederic Weisbecker 424a19ff1a2SFrederic Weisbecker if (acct->gtime) 425fb8b049cSFrederic Weisbecker account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 426a19ff1a2SFrederic Weisbecker 42751eeef9eSChristophe Leroy if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) { 428be9095edSFrederic Weisbecker account_steal_time(cputime_to_nsecs(acct->steal_time)); 42951eeef9eSChristophe Leroy acct->steal_time = 0; 43051eeef9eSChristophe Leroy } 431a19ff1a2SFrederic Weisbecker 432a19ff1a2SFrederic Weisbecker if (acct->idle_time) 43318b43a9bSFrederic Weisbecker account_idle_time(cputime_to_nsecs(acct->idle_time)); 434a19ff1a2SFrederic Weisbecker 435a19ff1a2SFrederic Weisbecker if (acct->stime) 436fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 437fb8b049cSFrederic Weisbecker CPUTIME_SYSTEM); 438a19ff1a2SFrederic Weisbecker 439a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 440fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 441fb8b049cSFrederic Weisbecker CPUTIME_IRQ); 442a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 443fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 444fb8b049cSFrederic Weisbecker CPUTIME_SOFTIRQ); 445a19ff1a2SFrederic Weisbecker 446b38a181cSChristophe Leroy vtime_flush_scaled(tsk, acct); 447b38a181cSChristophe Leroy 4488c8b73c4SFrederic Weisbecker acct->utime = 0; 449a19ff1a2SFrederic Weisbecker acct->gtime = 0; 450a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 451a19ff1a2SFrederic Weisbecker acct->stime = 0; 452a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 453a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 454c6622f63SPaul Mackerras } 455c6622f63SPaul Mackerras 456abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 457c6622f63SPaul Mackerras #define calc_cputime_factors() 458c6622f63SPaul Mackerras #endif 459c6622f63SPaul Mackerras 4606defa38bSPaul Mackerras void __delay(unsigned long loops) 4616defa38bSPaul Mackerras { 4626defa38bSPaul Mackerras unsigned long start; 4636defa38bSPaul Mackerras 4644e287e65SNicholas Piggin spin_begin(); 465a4c5a355SChristophe Leroy if (tb_invalid) { 466de269129SMahesh Salgaonkar /* 467de269129SMahesh Salgaonkar * TB is in error state and isn't ticking anymore. 468de269129SMahesh Salgaonkar * HMI handler was unable to recover from TB error. 469de269129SMahesh Salgaonkar * Return immediately, so that kernel won't get stuck here. 470de269129SMahesh Salgaonkar */ 471de269129SMahesh Salgaonkar spin_cpu_relax(); 4726defa38bSPaul Mackerras } else { 473942e8911SChristophe Leroy start = mftb(); 474942e8911SChristophe Leroy while (mftb() - start < loops) 4754e287e65SNicholas Piggin spin_cpu_relax(); 4766defa38bSPaul Mackerras } 4774e287e65SNicholas Piggin spin_end(); 4786defa38bSPaul Mackerras } 4796defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4806defa38bSPaul Mackerras 4816defa38bSPaul Mackerras void udelay(unsigned long usecs) 4826defa38bSPaul Mackerras { 4836defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4846defa38bSPaul Mackerras } 4856defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4866defa38bSPaul Mackerras 487f2783c15SPaul Mackerras #ifdef CONFIG_SMP 488f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 489f2783c15SPaul Mackerras { 490f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 491f2783c15SPaul Mackerras 492f2783c15SPaul Mackerras if (in_lock_functions(pc)) 493f2783c15SPaul Mackerras return regs->link; 494f2783c15SPaul Mackerras 495f2783c15SPaul Mackerras return pc; 496f2783c15SPaul Mackerras } 497f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 498f2783c15SPaul Mackerras #endif 499f2783c15SPaul Mackerras 500e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 501105988c0SPaul Mackerras 5020fe1ac48SPaul Mackerras /* 5030fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 5040fe1ac48SPaul Mackerras */ 5050fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 50625aa1458SNicholas Piggin static inline unsigned long test_irq_work_pending(void) 50725aa1458SNicholas Piggin { 50825aa1458SNicholas Piggin unsigned long x; 50925aa1458SNicholas Piggin 51025aa1458SNicholas Piggin asm volatile("lbz %0,%1(13)" 51125aa1458SNicholas Piggin : "=r" (x) 51225aa1458SNicholas Piggin : "i" (offsetof(struct paca_struct, irq_work_pending))); 51325aa1458SNicholas Piggin return x; 51425aa1458SNicholas Piggin } 51525aa1458SNicholas Piggin 516e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 5170fe1ac48SPaul Mackerras { 5180fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5190fe1ac48SPaul Mackerras "r" (1), 520e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5210fe1ac48SPaul Mackerras } 5220fe1ac48SPaul Mackerras 523e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 5240fe1ac48SPaul Mackerras { 5250fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5260fe1ac48SPaul Mackerras "r" (0), 527e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5280fe1ac48SPaul Mackerras } 5290fe1ac48SPaul Mackerras 5300fe1ac48SPaul Mackerras #else /* 32-bit */ 5310fe1ac48SPaul Mackerras 532e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5330fe1ac48SPaul Mackerras 53469111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 53569111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 53669111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 537105988c0SPaul Mackerras 538abc3fce7SNicholas Piggin #endif /* 32 vs 64 bit */ 539abc3fce7SNicholas Piggin 5404f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5410fe1ac48SPaul Mackerras { 542abc3fce7SNicholas Piggin /* 543abc3fce7SNicholas Piggin * 64-bit code that uses irq soft-mask can just cause an immediate 544abc3fce7SNicholas Piggin * interrupt here that gets soft masked, if this is called under 545abc3fce7SNicholas Piggin * local_irq_disable(). It might be possible to prevent that happening 546abc3fce7SNicholas Piggin * by noticing interrupts are disabled and setting decrementer pending 547abc3fce7SNicholas Piggin * to be replayed when irqs are enabled. The problem there is that 548abc3fce7SNicholas Piggin * tracing can call irq_work_raise, including in code that does low 549abc3fce7SNicholas Piggin * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on) 550abc3fce7SNicholas Piggin * which could get tangled up if we're messing with the same state 551abc3fce7SNicholas Piggin * here. 552abc3fce7SNicholas Piggin */ 5530fe1ac48SPaul Mackerras preempt_disable(); 554e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5550fe1ac48SPaul Mackerras set_dec(1); 5560fe1ac48SPaul Mackerras preempt_enable(); 5570fe1ac48SPaul Mackerras } 5580fe1ac48SPaul Mackerras 55925aa1458SNicholas Piggin static void set_dec_or_work(u64 val) 56025aa1458SNicholas Piggin { 56125aa1458SNicholas Piggin set_dec(val); 56225aa1458SNicholas Piggin /* We may have raced with new irq work */ 56325aa1458SNicholas Piggin if (unlikely(test_irq_work_pending())) 56425aa1458SNicholas Piggin set_dec(1); 56525aa1458SNicholas Piggin } 56625aa1458SNicholas Piggin 567e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 568105988c0SPaul Mackerras 569e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 570e360adbeSPeter Zijlstra #define clear_irq_work_pending() 571105988c0SPaul Mackerras 57225aa1458SNicholas Piggin static void set_dec_or_work(u64 val) 57325aa1458SNicholas Piggin { 57425aa1458SNicholas Piggin set_dec(val); 57525aa1458SNicholas Piggin } 576e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 577105988c0SPaul Mackerras 57825aa1458SNicholas Piggin #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 57925aa1458SNicholas Piggin void timer_rearm_host_dec(u64 now) 58025aa1458SNicholas Piggin { 58125aa1458SNicholas Piggin u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 58225aa1458SNicholas Piggin 58325aa1458SNicholas Piggin WARN_ON_ONCE(!arch_irqs_disabled()); 58425aa1458SNicholas Piggin WARN_ON_ONCE(mfmsr() & MSR_EE); 58525aa1458SNicholas Piggin 58625aa1458SNicholas Piggin if (now >= *next_tb) { 58725aa1458SNicholas Piggin local_paca->irq_happened |= PACA_IRQ_DEC; 58825aa1458SNicholas Piggin } else { 58925aa1458SNicholas Piggin now = *next_tb - now; 590cf74ff52SNicholas Piggin if (now > decrementer_max) 591cf74ff52SNicholas Piggin now = decrementer_max; 59225aa1458SNicholas Piggin set_dec_or_work(now); 59325aa1458SNicholas Piggin } 59425aa1458SNicholas Piggin } 59525aa1458SNicholas Piggin EXPORT_SYMBOL_GPL(timer_rearm_host_dec); 59625aa1458SNicholas Piggin #endif 59725aa1458SNicholas Piggin 5981b783955SPreeti U Murthy /* 5991b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 6001b783955SPreeti U Murthy * with interrupts disabled. 6011b783955SPreeti U Murthy */ 6023a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt) 6031b783955SPreeti U Murthy { 6043f984620SNicholas Piggin struct clock_event_device *evt = this_cpu_ptr(&decrementers); 60569111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 6063f984620SNicholas Piggin struct pt_regs *old_regs; 6073f984620SNicholas Piggin u64 now; 6081b783955SPreeti U Murthy 60959d512e4SNicholas Piggin /* 61059d512e4SNicholas Piggin * Some implementations of hotplug will get timer interrupts while 61159d512e4SNicholas Piggin * offline, just ignore these. 6121b783955SPreeti U Murthy */ 613a7cba02dSNicholas Piggin if (unlikely(!cpu_online(smp_processor_id()))) { 614a7cba02dSNicholas Piggin set_dec(decrementer_max); 6151b783955SPreeti U Murthy return; 6161b783955SPreeti U Murthy } 6171b783955SPreeti U Murthy 6180faf20a1SNicholas Piggin /* Conditionally hard-enable interrupts. */ 6190faf20a1SNicholas Piggin if (should_hard_irq_enable()) { 6200faf20a1SNicholas Piggin /* 6210faf20a1SNicholas Piggin * Ensure a positive value is written to the decrementer, or 6220faf20a1SNicholas Piggin * else some CPUs will continue to take decrementer exceptions. 6230faf20a1SNicholas Piggin * When the PPC_WATCHDOG (decrementer based) is configured, 6240faf20a1SNicholas Piggin * keep this at most 31 bits, which is about 4 seconds on most 6250faf20a1SNicholas Piggin * systems, which gives the watchdog a chance of catching timer 6260faf20a1SNicholas Piggin * interrupt hard lockups. 627a7cba02dSNicholas Piggin */ 628a7cba02dSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_WATCHDOG)) 629a7cba02dSNicholas Piggin set_dec(0x7fffffff); 630a7cba02dSNicholas Piggin else 631a7cba02dSNicholas Piggin set_dec(decrementer_max); 632a7cba02dSNicholas Piggin 6330faf20a1SNicholas Piggin do_hard_irq_enable(); 6340faf20a1SNicholas Piggin } 6351b783955SPreeti U Murthy 6366e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 6371b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 63898694166SChristophe Leroy __do_IRQ(regs); 6391b783955SPreeti U Murthy #endif 6401b783955SPreeti U Murthy 6411b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 6421b1b6a6fSNicholas Piggin 6433f984620SNicholas Piggin trace_timer_interrupt_entry(regs); 6441b783955SPreeti U Murthy 6453f984620SNicholas Piggin if (test_irq_work_pending()) { 6463f984620SNicholas Piggin clear_irq_work_pending(); 647cc15ff32SGanesh Goudar mce_run_irq_context_handlers(); 6483f984620SNicholas Piggin irq_work_run(); 6493f984620SNicholas Piggin } 6503f984620SNicholas Piggin 6516601ec1cSChristophe Leroy now = get_tb(); 6523f984620SNicholas Piggin if (now >= *next_tb) { 6533f984620SNicholas Piggin evt->event_handler(evt); 6543f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_event); 6553f984620SNicholas Piggin } else { 6563f984620SNicholas Piggin now = *next_tb - now; 6578defc2a5SNicholas Piggin if (now > decrementer_max) 6588defc2a5SNicholas Piggin now = decrementer_max; 65925aa1458SNicholas Piggin set_dec_or_work(now); 6603f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_others); 6613f984620SNicholas Piggin } 6623f984620SNicholas Piggin 6633f984620SNicholas Piggin trace_timer_interrupt_exit(regs); 6641b1b6a6fSNicholas Piggin 6657d12e780SDavid Howells set_irq_regs(old_regs); 666f2783c15SPaul Mackerras } 6679445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 668f2783c15SPaul Mackerras 669bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 6703f984620SNicholas Piggin void timer_broadcast_interrupt(void) 6713f984620SNicholas Piggin { 6723f984620SNicholas Piggin tick_receive_broadcast(); 673e360cd37SNicholas Piggin __this_cpu_inc(irq_stat.broadcast_irqs_event); 6743f984620SNicholas Piggin } 675bc907113SNicholas Piggin #endif 6763f984620SNicholas Piggin 6777ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 678e606a2f4SChristophe Leroy /* Overrides the weak version in kernel/power/main.c */ 679e606a2f4SChristophe Leroy void arch_suspend_disable_irqs(void) 6807ac5dde9SScott Wood { 681e606a2f4SChristophe Leroy if (ppc_md.suspend_disable_irqs) 682e606a2f4SChristophe Leroy ppc_md.suspend_disable_irqs(); 683e606a2f4SChristophe Leroy 6847ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6857ac5dde9SScott Wood * with suspending. 6867ac5dde9SScott Wood */ 6877ac5dde9SScott Wood 68879901024SOliver O'Halloran set_dec(decrementer_max); 6897ac5dde9SScott Wood local_irq_disable(); 69079901024SOliver O'Halloran set_dec(decrementer_max); 6917ac5dde9SScott Wood } 6927ac5dde9SScott Wood 6937ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6947ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 6957ac5dde9SScott Wood { 696e606a2f4SChristophe Leroy local_irq_enable(); 697e606a2f4SChristophe Leroy 6987ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 6997ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 7007ac5dde9SScott Wood } 7017ac5dde9SScott Wood #endif 7027ac5dde9SScott Wood 703b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 704b6c295dfSPaul Mackerras { 705b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 706b6c295dfSPaul Mackerras } 707b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 708b6c295dfSPaul Mackerras 709f2783c15SPaul Mackerras /* 710f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 711f2783c15SPaul Mackerras * 712f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 713f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 714f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 715f2783c15SPaul Mackerras */ 7166b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void) 717f2783c15SPaul Mackerras { 718fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 719f2783c15SPaul Mackerras } 720f2783c15SPaul Mackerras 7214be1b297SCyril Bur 7224be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 7234be1b297SCyril Bur 7244be1b297SCyril Bur /* 7254be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 7264be1b297SCyril Bur * kernels. 7274be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 7284be1b297SCyril Bur */ 7294be1b297SCyril Bur unsigned long long running_clock(void) 7304be1b297SCyril Bur { 7314be1b297SCyril Bur /* 7324be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 7334be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 7344be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 7354be1b297SCyril Bur * 7364be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7374be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7384be1b297SCyril Bur */ 7394be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7404be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7414be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7424be1b297SCyril Bur 7434be1b297SCyril Bur /* 7444be1b297SCyril Bur * This is a next best approximation without a VTB. 7454be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7464be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7474be1b297SCyril Bur * VTB so it makes no difference anyway. 7484be1b297SCyril Bur */ 7499f3768e0SFrédéric Weisbecker return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 7504be1b297SCyril Bur } 7514be1b297SCyril Bur #endif 7524be1b297SCyril Bur 7530bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 754f2783c15SPaul Mackerras { 755f2783c15SPaul Mackerras struct device_node *cpu; 7566f7aba7bSAnton Blanchard const __be32 *fp; 7570bb474a4SAnton Blanchard int found = 0; 758f2783c15SPaul Mackerras 7590bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 760f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 761f2783c15SPaul Mackerras 762d8a8188dSOlaf Hering if (cpu) { 763e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 764d8a8188dSOlaf Hering if (fp) { 7650bb474a4SAnton Blanchard found = 1; 766a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 767f2783c15SPaul Mackerras } 7680bb474a4SAnton Blanchard 7690bb474a4SAnton Blanchard of_node_put(cpu); 770f2783c15SPaul Mackerras } 7710bb474a4SAnton Blanchard 7720bb474a4SAnton Blanchard return found; 7730bb474a4SAnton Blanchard } 7740bb474a4SAnton Blanchard 775e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 77677c0a700SBenjamin Herrenschmidt { 777047a6fd4SChristophe Leroy #ifdef CONFIG_BOOKE_OR_40x 7786e2f03e2SIvan Mikhaylov unsigned int tcr; 7796e2f03e2SIvan Mikhaylov 78077c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 78177c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 78277c0a700SBenjamin Herrenschmidt 7836e2f03e2SIvan Mikhaylov tcr = mfspr(SPRN_TCR); 7846e2f03e2SIvan Mikhaylov /* 7856e2f03e2SIvan Mikhaylov * The watchdog may have already been enabled by u-boot. So leave 7866e2f03e2SIvan Mikhaylov * TRC[WP] (Watchdog Period) alone. 7876e2f03e2SIvan Mikhaylov */ 7886e2f03e2SIvan Mikhaylov tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 7896e2f03e2SIvan Mikhaylov tcr |= TCR_DIE; /* Enable decrementer */ 7906e2f03e2SIvan Mikhaylov mtspr(SPRN_TCR, tcr); 7916e2f03e2SIvan Mikhaylov #endif 79277c0a700SBenjamin Herrenschmidt } 79377c0a700SBenjamin Herrenschmidt 7940bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 7950bb474a4SAnton Blanchard { 7960bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 7970bb474a4SAnton Blanchard 7980bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 7990bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 8000bb474a4SAnton Blanchard 801f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 802f2783c15SPaul Mackerras "(not found)\n"); 8030bb474a4SAnton Blanchard } 804f2783c15SPaul Mackerras 8050bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 8060bb474a4SAnton Blanchard 8070bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 8080bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 8090bb474a4SAnton Blanchard 8100bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 8110bb474a4SAnton Blanchard "(not found)\n"); 812f2783c15SPaul Mackerras } 813f2783c15SPaul Mackerras } 814f2783c15SPaul Mackerras 8155235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now) 816f2783c15SPaul Mackerras { 817f2783c15SPaul Mackerras struct rtc_time tm; 818f2783c15SPaul Mackerras 819aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 820023f333aSJason Gunthorpe return -ENODEV; 821aa3be5f3STony Breeds 8225235afa8SArnd Bergmann rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm); 823aa3be5f3STony Breeds 824aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 825aa3be5f3STony Breeds } 826aa3be5f3STony Breeds 8275bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts) 828aa3be5f3STony Breeds { 829aa3be5f3STony Breeds struct rtc_time tm; 830aa3be5f3STony Breeds static int first = 1; 831aa3be5f3STony Breeds 832d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 833aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 834aa3be5f3STony Breeds if (first) { 835aa3be5f3STony Breeds first = 0; 836aa3be5f3STony Breeds if (ppc_md.time_init) 837aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 838aa3be5f3STony Breeds 839aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 840d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 841d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 842d90246cdSMartin Schwidefsky return; 843aa3be5f3STony Breeds } 844d90246cdSMartin Schwidefsky } 845d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 846d90246cdSMartin Schwidefsky ts->tv_sec = 0; 847d90246cdSMartin Schwidefsky return; 848d90246cdSMartin Schwidefsky } 849f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 850978d7eb3SBenjamin Herrenschmidt 8515bfd6435SArnd Bergmann ts->tv_sec = rtc_tm_to_time64(&tm); 852f2783c15SPaul Mackerras } 853f2783c15SPaul Mackerras 8545bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts) 855978d7eb3SBenjamin Herrenschmidt { 856978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 857978d7eb3SBenjamin Herrenschmidt 858978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 859978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 860978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 861978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 862978d7eb3SBenjamin Herrenschmidt } 863978d7eb3SBenjamin Herrenschmidt 864978d7eb3SBenjamin Herrenschmidt } 865978d7eb3SBenjamin Herrenschmidt 8664a4cfe38STony Breeds /* clocksource code */ 8676b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs) 8684a4cfe38STony Breeds { 869a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8704a4cfe38STony Breeds } 8714a4cfe38STony Breeds 8721c21a293SMichael Ellerman static void __init clocksource_init(void) 8734a4cfe38STony Breeds { 874a4c5a355SChristophe Leroy struct clocksource *clock = &clocksource_timebase; 8754a4cfe38STony Breeds 87611b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 8774a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 8784a4cfe38STony Breeds clock->name); 8794a4cfe38STony Breeds return; 8804a4cfe38STony Breeds } 8814a4cfe38STony Breeds 8824a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 8834a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 8844a4cfe38STony Breeds } 8854a4cfe38STony Breeds 886d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 887d831d0b8STony Breeds struct clock_event_device *dev) 888d831d0b8STony Breeds { 8896601ec1cSChristophe Leroy __this_cpu_write(decrementers_next_tb, get_tb() + evt); 89025aa1458SNicholas Piggin set_dec_or_work(evt); 8910215f7d8SBenjamin Herrenschmidt 892d831d0b8STony Breeds return 0; 893d831d0b8STony Breeds } 894d831d0b8STony Breeds 89537a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 896d831d0b8STony Breeds { 897*35de589cSNicholas Piggin __this_cpu_write(decrementers_next_tb, DEC_CLOCKEVENT_STOPPED); 898*35de589cSNicholas Piggin set_dec_or_work(decrementer_max); 899*35de589cSNicholas Piggin 90037a13e78SViresh Kumar return 0; 901d831d0b8STony Breeds } 902d831d0b8STony Breeds 903d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 904d831d0b8STony Breeds { 9057df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 906d831d0b8STony Breeds 907d831d0b8STony Breeds *dec = decrementer_clockevent; 908320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 909d831d0b8STony Breeds 9108b78fdb0SAnton Blanchard clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max); 9118b78fdb0SAnton Blanchard 912b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 913d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 914b4d16ab5SMichael Ellerman 915b4d16ab5SMichael Ellerman /* Set values for KVM, see kvm_emulate_dec() */ 916b4d16ab5SMichael Ellerman decrementer_clockevent.mult = dec->mult; 917b4d16ab5SMichael Ellerman decrementer_clockevent.shift = dec->shift; 918d831d0b8STony Breeds } 919d831d0b8STony Breeds 92079901024SOliver O'Halloran static void enable_large_decrementer(void) 92179901024SOliver O'Halloran { 92279901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 92379901024SOliver O'Halloran return; 92479901024SOliver O'Halloran 92579901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 92679901024SOliver O'Halloran return; 92779901024SOliver O'Halloran 92879901024SOliver O'Halloran /* 92979901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 93079901024SOliver O'Halloran * otherwise firmware should have done it for us. 93179901024SOliver O'Halloran */ 93279901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 93379901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 93479901024SOliver O'Halloran } 93579901024SOliver O'Halloran 93679901024SOliver O'Halloran static void __init set_decrementer_max(void) 93779901024SOliver O'Halloran { 93879901024SOliver O'Halloran struct device_node *cpu; 93979901024SOliver O'Halloran u32 bits = 32; 94079901024SOliver O'Halloran 94179901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 94279901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 94379901024SOliver O'Halloran return; 94479901024SOliver O'Halloran 94579901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 94679901024SOliver O'Halloran 94779901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 94879901024SOliver O'Halloran if (bits > 64 || bits < 32) { 94979901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 95079901024SOliver O'Halloran bits = 32; 95179901024SOliver O'Halloran } 95279901024SOliver O'Halloran 95379901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 95479901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 95579901024SOliver O'Halloran } 95679901024SOliver O'Halloran 95779901024SOliver O'Halloran of_node_put(cpu); 95879901024SOliver O'Halloran 95979901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 96079901024SOliver O'Halloran bits, decrementer_max); 96179901024SOliver O'Halloran } 96279901024SOliver O'Halloran 963c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 964d831d0b8STony Breeds { 9658b78fdb0SAnton Blanchard register_decrementer_clockevent(smp_processor_id()); 966d831d0b8STony Breeds } 967d831d0b8STony Breeds 968d831d0b8STony Breeds void secondary_cpu_time_init(void) 969d831d0b8STony Breeds { 97079901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 97179901024SOliver O'Halloran enable_large_decrementer(); 97279901024SOliver O'Halloran 97377c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 97477c0a700SBenjamin Herrenschmidt * such as BookE 97577c0a700SBenjamin Herrenschmidt */ 97677c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 97777c0a700SBenjamin Herrenschmidt 978d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 979d831d0b8STony Breeds * call here ! */ 980d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 981d831d0b8STony Breeds } 982d831d0b8STony Breeds 983f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 984f2783c15SPaul Mackerras void __init time_init(void) 985f2783c15SPaul Mackerras { 986f2783c15SPaul Mackerras struct div_result res; 987d75d68cfSPaul Mackerras u64 scale; 988f2783c15SPaul Mackerras unsigned shift; 989f2783c15SPaul Mackerras 99096c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 991f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 992224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 993374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 994224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 995374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 996374e99d4SPaul Mackerras 997374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 998092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 999374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 1000c6622f63SPaul Mackerras calc_cputime_factors(); 1001092b8f34SPaul Mackerras 1002092b8f34SPaul Mackerras /* 1003f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 1004f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 1005f2783c15SPaul Mackerras * which is the timebase frequency. 1006f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 1007f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 1008f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 1009f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 1010f2783c15SPaul Mackerras * sched_clock(). 1011f2783c15SPaul Mackerras */ 1012f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1013f2783c15SPaul Mackerras scale = res.result_low; 1014f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1015f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1016f2783c15SPaul Mackerras res.result_high >>= 1; 1017f2783c15SPaul Mackerras } 1018f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1019f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1020fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 10216601ec1cSChristophe Leroy boot_tb = get_tb(); 1022f2783c15SPaul Mackerras 1023092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1024092b8f34SPaul Mackerras if (timezone_offset) { 1025092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1026092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1027092b8f34SPaul Mackerras } 1028092b8f34SPaul Mackerras 1029a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1030f2783c15SPaul Mackerras 103179901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 103279901024SOliver O'Halloran set_decrementer_max(); 103379901024SOliver O'Halloran enable_large_decrementer(); 103479901024SOliver O'Halloran 103577c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 103677c0a700SBenjamin Herrenschmidt * such as BookE 103777c0a700SBenjamin Herrenschmidt */ 103877c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 103977c0a700SBenjamin Herrenschmidt 1040f5339277SStephen Rothwell /* Register the clocksource */ 10414a4cfe38STony Breeds clocksource_init(); 10424a4cfe38STony Breeds 1043d831d0b8STony Breeds init_decrementer_clockevent(); 10440d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1045f0d37300SKevin Hao 1046f0d37300SKevin Hao of_clk_init(NULL); 1047b709e32eSPingfan Liu enable_sched_clock_irqtime(); 1048f2783c15SPaul Mackerras } 1049f2783c15SPaul Mackerras 1050f2783c15SPaul Mackerras /* 1051f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1052f2783c15SPaul Mackerras * result. 1053f2783c15SPaul Mackerras */ 1054f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1055f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1056f2783c15SPaul Mackerras { 1057f2783c15SPaul Mackerras unsigned long a, b, c, d; 1058f2783c15SPaul Mackerras unsigned long w, x, y, z; 1059f2783c15SPaul Mackerras u64 ra, rb, rc; 1060f2783c15SPaul Mackerras 1061f2783c15SPaul Mackerras a = dividend_high >> 32; 1062f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1063f2783c15SPaul Mackerras c = dividend_low >> 32; 1064f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1065f2783c15SPaul Mackerras 1066f2783c15SPaul Mackerras w = a / divisor; 1067f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1068f2783c15SPaul Mackerras 1069f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1070f2783c15SPaul Mackerras x = ra; 1071f2783c15SPaul Mackerras 1072f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1073f2783c15SPaul Mackerras y = rb; 1074f2783c15SPaul Mackerras 1075f2783c15SPaul Mackerras do_div(rc, divisor); 1076f2783c15SPaul Mackerras z = rc; 1077f2783c15SPaul Mackerras 1078f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1079f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1080f2783c15SPaul Mackerras 1081f2783c15SPaul Mackerras } 1082bcd68a70SGeert Uytterhoeven 1083177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1084177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1085177996e6SBenjamin Herrenschmidt { 1086177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1087177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1088177996e6SBenjamin Herrenschmidt */ 1089177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1090177996e6SBenjamin Herrenschmidt } 1091177996e6SBenjamin Herrenschmidt 1092169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1093169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1094169047f4SArnd Bergmann { 1095169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1096890ae797SAlexandre Belloni return 0; 1097169047f4SArnd Bergmann } 1098169047f4SArnd Bergmann 1099169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1100169047f4SArnd Bergmann { 1101169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1102169047f4SArnd Bergmann return -EOPNOTSUPP; 1103169047f4SArnd Bergmann 1104169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1105169047f4SArnd Bergmann return -EOPNOTSUPP; 1106169047f4SArnd Bergmann 1107169047f4SArnd Bergmann return 0; 1108169047f4SArnd Bergmann } 1109169047f4SArnd Bergmann 1110169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1111169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1112169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1113169047f4SArnd Bergmann }; 1114169047f4SArnd Bergmann 1115bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1116bcd68a70SGeert Uytterhoeven { 1117bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1118bcd68a70SGeert Uytterhoeven 1119bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1120bcd68a70SGeert Uytterhoeven return -ENODEV; 1121bcd68a70SGeert Uytterhoeven 1122169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1123169047f4SArnd Bergmann &rtc_generic_ops, 1124169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1125bcd68a70SGeert Uytterhoeven 11268c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1127bcd68a70SGeert Uytterhoeven } 1128bcd68a70SGeert Uytterhoeven 11298f6b9512SPaul Gortmaker device_initcall(rtc_init); 1130169047f4SArnd Bergmann #endif 1131