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> 34f2783c15SPaul Mackerras #include <linux/kernel.h> 35f2783c15SPaul Mackerras #include <linux/param.h> 36f2783c15SPaul Mackerras #include <linux/string.h> 37f2783c15SPaul Mackerras #include <linux/mm.h> 38f2783c15SPaul Mackerras #include <linux/interrupt.h> 39f2783c15SPaul Mackerras #include <linux/timex.h> 40f2783c15SPaul Mackerras #include <linux/kernel_stat.h> 41f2783c15SPaul Mackerras #include <linux/time.h> 42f2783c15SPaul Mackerras #include <linux/init.h> 43f2783c15SPaul Mackerras #include <linux/profile.h> 44f2783c15SPaul Mackerras #include <linux/cpu.h> 45f2783c15SPaul Mackerras #include <linux/security.h> 46f2783c15SPaul Mackerras #include <linux/percpu.h> 47f2783c15SPaul Mackerras #include <linux/rtc.h> 48092b8f34SPaul Mackerras #include <linux/jiffies.h> 49c6622f63SPaul Mackerras #include <linux/posix-timers.h> 507d12e780SDavid Howells #include <linux/irq.h> 51177996e6SBenjamin Herrenschmidt #include <linux/delay.h> 52e360adbeSPeter Zijlstra #include <linux/irq_work.h> 53f0d37300SKevin Hao #include <linux/clk-provider.h> 547f92bc56SDaniel Axtens #include <linux/suspend.h> 5532ef5517SIngo Molnar #include <linux/sched/cputime.h> 564e287e65SNicholas Piggin #include <linux/processor.h> 576795b85cSAnton Blanchard #include <asm/trace.h> 58f2783c15SPaul Mackerras 59f2783c15SPaul Mackerras #include <asm/io.h> 60f2783c15SPaul Mackerras #include <asm/nvram.h> 61f2783c15SPaul Mackerras #include <asm/cache.h> 62f2783c15SPaul Mackerras #include <asm/machdep.h> 637c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 64f2783c15SPaul Mackerras #include <asm/time.h> 65f2783c15SPaul Mackerras #include <asm/prom.h> 66f2783c15SPaul Mackerras #include <asm/irq.h> 67f2783c15SPaul Mackerras #include <asm/div64.h> 682249ca9dSPaul Mackerras #include <asm/smp.h> 69a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h> 70f2783c15SPaul Mackerras #include <asm/firmware.h> 710545d543SDaniel Axtens #include <asm/asm-prototypes.h> 72f2783c15SPaul Mackerras 734a4cfe38STony Breeds /* powerpc clocksource/clockevent code */ 744a4cfe38STony Breeds 75d831d0b8STony Breeds #include <linux/clockchips.h> 76189374aeSJohn Stultz #include <linux/timekeeper_internal.h> 774a4cfe38STony Breeds 78a5a1d1c2SThomas Gleixner static u64 rtc_read(struct clocksource *); 794a4cfe38STony Breeds static struct clocksource clocksource_rtc = { 804a4cfe38STony Breeds .name = "rtc", 814a4cfe38STony Breeds .rating = 400, 824a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 834a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 844a4cfe38STony Breeds .read = rtc_read, 854a4cfe38STony Breeds }; 864a4cfe38STony Breeds 87a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *); 884a4cfe38STony Breeds static struct clocksource clocksource_timebase = { 894a4cfe38STony Breeds .name = "timebase", 904a4cfe38STony Breeds .rating = 400, 914a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 924a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 934a4cfe38STony Breeds .read = timebase_read, 944a4cfe38STony Breeds }; 954a4cfe38STony Breeds 9679901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 9779901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 98d831d0b8STony Breeds 99d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 100d831d0b8STony Breeds struct clock_event_device *dev); 10137a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt); 102d831d0b8STony Breeds 1036e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = { 104d831d0b8STony Breeds .name = "decrementer", 105d831d0b8STony Breeds .rating = 200, 106d831d0b8STony Breeds .irq = 0, 107d831d0b8STony Breeds .set_next_event = decrementer_set_next_event, 10881759360SAnton Blanchard .set_state_oneshot_stopped = decrementer_shutdown, 10937a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 11037a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 11137a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 11237a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 113d831d0b8STony Breeds }; 1146e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 115d831d0b8STony Breeds 1167df10275SAnton Blanchard DEFINE_PER_CPU(u64, 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 186cf9efce0SPaul Mackerras /* 187cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 188cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 189cf9efce0SPaul Mackerras */ 190cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 191cf9efce0SPaul Mackerras { 192872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 193cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 194cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 195cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 196cf9efce0SPaul Mackerras u64 tb_delta; 197cf9efce0SPaul Mackerras u64 stolen = 0; 198cf9efce0SPaul Mackerras u64 dtb; 199cf9efce0SPaul Mackerras 20084ffae55SAnton Blanchard if (!dtl) 20184ffae55SAnton Blanchard return 0; 20284ffae55SAnton Blanchard 2037ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 204cf9efce0SPaul Mackerras return 0; 2057ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2067ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2077ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2087ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 209cf9efce0SPaul Mackerras barrier(); 2107ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 211cf9efce0SPaul Mackerras /* buffer has overflowed */ 2127ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 213cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 214cf9efce0SPaul Mackerras continue; 215cf9efce0SPaul Mackerras } 216cf9efce0SPaul Mackerras if (dtb > stop_tb) 217cf9efce0SPaul Mackerras break; 21884b07386SAnton Blanchard if (dtl_consumer) 21984b07386SAnton Blanchard dtl_consumer(dtl, i); 220cf9efce0SPaul Mackerras stolen += tb_delta; 221cf9efce0SPaul Mackerras ++i; 222cf9efce0SPaul Mackerras ++dtl; 223cf9efce0SPaul Mackerras if (dtl == dtl_end) 224cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 225cf9efce0SPaul Mackerras } 226cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 227cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 228cf9efce0SPaul Mackerras return stolen; 229c6622f63SPaul Mackerras } 230c6622f63SPaul Mackerras 231c6622f63SPaul Mackerras /* 232cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 233cf9efce0SPaul Mackerras * Called on entry from user mode. 2344603ac18SMichael Neuling */ 235*eb8e20f8SMichael Ellerman void notrace accumulate_stolen_time(void) 2364603ac18SMichael Neuling { 237cf9efce0SPaul Mackerras u64 sst, ust; 2384e26bc4aSMadhavan Srinivasan unsigned long save_irq_soft_mask = irq_soft_mask_return(); 239c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 240b18ae08dSTejun Heo 241b18ae08dSTejun Heo /* We are called early in the exception entry, before 242b18ae08dSTejun Heo * soft/hard_enabled are sync'ed to the expected state 243b18ae08dSTejun Heo * for the exception. We are hard disabled but the PACA 244b18ae08dSTejun Heo * needs to reflect that so various debug stuff doesn't 245b18ae08dSTejun Heo * complain 246b18ae08dSTejun Heo */ 2474e26bc4aSMadhavan Srinivasan irq_soft_mask_set(IRQS_DISABLED); 248b18ae08dSTejun Heo 249c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 250c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2518c8b73c4SFrederic Weisbecker acct->stime -= sst; 2528c8b73c4SFrederic Weisbecker acct->utime -= ust; 253f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 254b18ae08dSTejun Heo 2554e26bc4aSMadhavan Srinivasan irq_soft_mask_set(save_irq_soft_mask); 2564603ac18SMichael Neuling } 2574603ac18SMichael Neuling 258cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 259cf9efce0SPaul Mackerras { 260a6201da3SAneesh Kumar K.V if (!firmware_has_feature(FW_FEATURE_SPLPAR)) 261a6201da3SAneesh Kumar K.V return 0; 262a6201da3SAneesh Kumar K.V 263a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 264a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 265cf9efce0SPaul Mackerras 266a19ff1a2SFrederic Weisbecker return 0; 267cf9efce0SPaul Mackerras } 268cf9efce0SPaul Mackerras 269cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 270cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 271cf9efce0SPaul Mackerras { 272cf9efce0SPaul Mackerras return 0; 273cf9efce0SPaul Mackerras } 274cf9efce0SPaul Mackerras 275cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 276cf9efce0SPaul Mackerras 2774603ac18SMichael Neuling /* 278c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 279c6622f63SPaul Mackerras * or soft irq state. 280c6622f63SPaul Mackerras */ 281b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct, 282b38a181cSChristophe Leroy unsigned long now, unsigned long stime) 283c6622f63SPaul Mackerras { 284abcff86dSChristophe Leroy unsigned long stime_scaled = 0; 285abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 286b38a181cSChristophe Leroy unsigned long nowscaled, deltascaled; 287a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 288c6622f63SPaul Mackerras 2894603ac18SMichael Neuling nowscaled = read_spurr(now); 290c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 291c223c903SChristophe Leroy acct->startspurr = nowscaled; 292a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 2938c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 294cf9efce0SPaul Mackerras 295cf9efce0SPaul Mackerras /* 296cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 297cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 298cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 299cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 300cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 301cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 302cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 303cf9efce0SPaul Mackerras * used by account_process_tick. 304cf9efce0SPaul Mackerras */ 305b38a181cSChristophe Leroy stime_scaled = stime; 306a19ff1a2SFrederic Weisbecker utime_scaled = utime; 307a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 308a19ff1a2SFrederic Weisbecker if (utime) { 309b38a181cSChristophe Leroy stime_scaled = deltascaled * stime / (stime + utime); 310b38a181cSChristophe Leroy utime_scaled = deltascaled - stime_scaled; 311cf9efce0SPaul Mackerras } else { 312b38a181cSChristophe Leroy stime_scaled = deltascaled; 313c6622f63SPaul Mackerras } 314cf9efce0SPaul Mackerras } 315a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 316abcff86dSChristophe Leroy #endif 317cf9efce0SPaul Mackerras 318b38a181cSChristophe Leroy return stime_scaled; 319b38a181cSChristophe Leroy } 320b38a181cSChristophe Leroy 321b38a181cSChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk, 322b38a181cSChristophe Leroy unsigned long *stime_scaled, 323b38a181cSChristophe Leroy unsigned long *steal_time) 324b38a181cSChristophe Leroy { 325b38a181cSChristophe Leroy unsigned long now, stime; 326b38a181cSChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 327b38a181cSChristophe Leroy 328b38a181cSChristophe Leroy WARN_ON_ONCE(!irqs_disabled()); 329b38a181cSChristophe Leroy 330b38a181cSChristophe Leroy now = mftb(); 331b38a181cSChristophe Leroy stime = now - acct->starttime; 332b38a181cSChristophe Leroy acct->starttime = now; 333b38a181cSChristophe Leroy 334b38a181cSChristophe Leroy *stime_scaled = vtime_delta_scaled(acct, now, stime); 335b38a181cSChristophe Leroy 336b38a181cSChristophe Leroy *steal_time = calculate_stolen_time(now); 337b38a181cSChristophe Leroy 338a19ff1a2SFrederic Weisbecker return stime; 339a7e1a9e3SFrederic Weisbecker } 340a7e1a9e3SFrederic Weisbecker 341fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk) 342a7e1a9e3SFrederic Weisbecker { 343a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 344a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 345a7e1a9e3SFrederic Weisbecker 346a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 347a19ff1a2SFrederic Weisbecker 348a19ff1a2SFrederic Weisbecker stime -= min(stime, steal_time); 349a19ff1a2SFrederic Weisbecker acct->steal_time += steal_time; 350a19ff1a2SFrederic Weisbecker 351a19ff1a2SFrederic Weisbecker if ((tsk->flags & PF_VCPU) && !irq_count()) { 352a19ff1a2SFrederic Weisbecker acct->gtime += stime; 353abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 354a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 355abcff86dSChristophe Leroy #endif 356a19ff1a2SFrederic Weisbecker } else { 357a19ff1a2SFrederic Weisbecker if (hardirq_count()) 358a19ff1a2SFrederic Weisbecker acct->hardirq_time += stime; 359a19ff1a2SFrederic Weisbecker else if (in_serving_softirq()) 360a19ff1a2SFrederic Weisbecker acct->softirq_time += stime; 361a19ff1a2SFrederic Weisbecker else 362a19ff1a2SFrederic Weisbecker acct->stime += stime; 363a19ff1a2SFrederic Weisbecker 364abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 365a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 366abcff86dSChristophe Leroy #endif 367a19ff1a2SFrederic Weisbecker } 368a7e1a9e3SFrederic Weisbecker } 369c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system); 370a7e1a9e3SFrederic Weisbecker 371fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 372a7e1a9e3SFrederic Weisbecker { 373a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 374a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 375a7e1a9e3SFrederic Weisbecker 376a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 377a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 378cf9efce0SPaul Mackerras } 379c6622f63SPaul Mackerras 380b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk, 381b38a181cSChristophe Leroy struct cpu_accounting_data *acct) 382b38a181cSChristophe Leroy { 383abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 384b38a181cSChristophe Leroy if (acct->utime_scaled) 385b38a181cSChristophe Leroy tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 386b38a181cSChristophe Leroy if (acct->stime_scaled) 387b38a181cSChristophe Leroy tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 388b38a181cSChristophe Leroy 389b38a181cSChristophe Leroy acct->utime_scaled = 0; 390b38a181cSChristophe Leroy acct->utime_sspurr = 0; 391b38a181cSChristophe Leroy acct->stime_scaled = 0; 392abcff86dSChristophe Leroy #endif 393b38a181cSChristophe Leroy } 394b38a181cSChristophe Leroy 395c6622f63SPaul Mackerras /* 396c8d7dabfSFrederic Weisbecker * Account the whole cputime accumulated in the paca 397c6622f63SPaul Mackerras * Must be called with interrupts disabled. 398bcebdf84SFrederic Weisbecker * Assumes that vtime_account_system/idle() has been called 399bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 400cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 401c6622f63SPaul Mackerras */ 402c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk) 403c6622f63SPaul Mackerras { 404c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 405c6622f63SPaul Mackerras 406a19ff1a2SFrederic Weisbecker if (acct->utime) 40723244a5cSFrederic Weisbecker account_user_time(tsk, cputime_to_nsecs(acct->utime)); 408a19ff1a2SFrederic Weisbecker 409a19ff1a2SFrederic Weisbecker if (acct->gtime) 410fb8b049cSFrederic Weisbecker account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 411a19ff1a2SFrederic Weisbecker 41251eeef9eSChristophe Leroy if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) { 413be9095edSFrederic Weisbecker account_steal_time(cputime_to_nsecs(acct->steal_time)); 41451eeef9eSChristophe Leroy acct->steal_time = 0; 41551eeef9eSChristophe Leroy } 416a19ff1a2SFrederic Weisbecker 417a19ff1a2SFrederic Weisbecker if (acct->idle_time) 41818b43a9bSFrederic Weisbecker account_idle_time(cputime_to_nsecs(acct->idle_time)); 419a19ff1a2SFrederic Weisbecker 420a19ff1a2SFrederic Weisbecker if (acct->stime) 421fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 422fb8b049cSFrederic Weisbecker CPUTIME_SYSTEM); 423a19ff1a2SFrederic Weisbecker 424a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 425fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 426fb8b049cSFrederic Weisbecker CPUTIME_IRQ); 427a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 428fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 429fb8b049cSFrederic Weisbecker CPUTIME_SOFTIRQ); 430a19ff1a2SFrederic Weisbecker 431b38a181cSChristophe Leroy vtime_flush_scaled(tsk, acct); 432b38a181cSChristophe Leroy 4338c8b73c4SFrederic Weisbecker acct->utime = 0; 434a19ff1a2SFrederic Weisbecker acct->gtime = 0; 435a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 436a19ff1a2SFrederic Weisbecker acct->stime = 0; 437a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 438a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 439c6622f63SPaul Mackerras } 440c6622f63SPaul Mackerras 441abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 442c6622f63SPaul Mackerras #define calc_cputime_factors() 443c6622f63SPaul Mackerras #endif 444c6622f63SPaul Mackerras 4456defa38bSPaul Mackerras void __delay(unsigned long loops) 4466defa38bSPaul Mackerras { 4476defa38bSPaul Mackerras unsigned long start; 4486defa38bSPaul Mackerras int diff; 4496defa38bSPaul Mackerras 4504e287e65SNicholas Piggin spin_begin(); 4516defa38bSPaul Mackerras if (__USE_RTC()) { 4526defa38bSPaul Mackerras start = get_rtcl(); 4536defa38bSPaul Mackerras do { 4546defa38bSPaul Mackerras /* the RTCL register wraps at 1000000000 */ 4556defa38bSPaul Mackerras diff = get_rtcl() - start; 4566defa38bSPaul Mackerras if (diff < 0) 4576defa38bSPaul Mackerras diff += 1000000000; 4584e287e65SNicholas Piggin spin_cpu_relax(); 4596defa38bSPaul Mackerras } while (diff < loops); 460de269129SMahesh Salgaonkar } else if (tb_invalid) { 461de269129SMahesh Salgaonkar /* 462de269129SMahesh Salgaonkar * TB is in error state and isn't ticking anymore. 463de269129SMahesh Salgaonkar * HMI handler was unable to recover from TB error. 464de269129SMahesh Salgaonkar * Return immediately, so that kernel won't get stuck here. 465de269129SMahesh Salgaonkar */ 466de269129SMahesh Salgaonkar spin_cpu_relax(); 4676defa38bSPaul Mackerras } else { 4686defa38bSPaul Mackerras start = get_tbl(); 4696defa38bSPaul Mackerras while (get_tbl() - start < loops) 4704e287e65SNicholas Piggin spin_cpu_relax(); 4716defa38bSPaul Mackerras } 4724e287e65SNicholas Piggin spin_end(); 4736defa38bSPaul Mackerras } 4746defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4756defa38bSPaul Mackerras 4766defa38bSPaul Mackerras void udelay(unsigned long usecs) 4776defa38bSPaul Mackerras { 4786defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4796defa38bSPaul Mackerras } 4806defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4816defa38bSPaul Mackerras 482f2783c15SPaul Mackerras #ifdef CONFIG_SMP 483f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 484f2783c15SPaul Mackerras { 485f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 486f2783c15SPaul Mackerras 487f2783c15SPaul Mackerras if (in_lock_functions(pc)) 488f2783c15SPaul Mackerras return regs->link; 489f2783c15SPaul Mackerras 490f2783c15SPaul Mackerras return pc; 491f2783c15SPaul Mackerras } 492f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 493f2783c15SPaul Mackerras #endif 494f2783c15SPaul Mackerras 495e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 496105988c0SPaul Mackerras 4970fe1ac48SPaul Mackerras /* 4980fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 4990fe1ac48SPaul Mackerras */ 5000fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 501e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void) 502105988c0SPaul Mackerras { 5030fe1ac48SPaul Mackerras unsigned long x; 5040fe1ac48SPaul Mackerras 5050fe1ac48SPaul Mackerras asm volatile("lbz %0,%1(13)" 5060fe1ac48SPaul Mackerras : "=r" (x) 507e360adbeSPeter Zijlstra : "i" (offsetof(struct paca_struct, irq_work_pending))); 5080fe1ac48SPaul Mackerras return x; 509105988c0SPaul Mackerras } 510105988c0SPaul Mackerras 511e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 5120fe1ac48SPaul Mackerras { 5130fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5140fe1ac48SPaul Mackerras "r" (1), 515e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5160fe1ac48SPaul Mackerras } 5170fe1ac48SPaul Mackerras 518e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 5190fe1ac48SPaul Mackerras { 5200fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5210fe1ac48SPaul Mackerras "r" (0), 522e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5230fe1ac48SPaul Mackerras } 5240fe1ac48SPaul Mackerras 525ebb37cf3SNicholas Piggin void arch_irq_work_raise(void) 526ebb37cf3SNicholas Piggin { 527ebb37cf3SNicholas Piggin preempt_disable(); 528ebb37cf3SNicholas Piggin set_irq_work_pending_flag(); 529ebb37cf3SNicholas Piggin /* 530ebb37cf3SNicholas Piggin * Non-nmi code running with interrupts disabled will replay 531ebb37cf3SNicholas Piggin * irq_happened before it re-enables interrupts, so setthe 532ebb37cf3SNicholas Piggin * decrementer there instead of causing a hardware exception 533ebb37cf3SNicholas Piggin * which would immediately hit the masked interrupt handler 534ebb37cf3SNicholas Piggin * and have the net effect of setting the decrementer in 535ebb37cf3SNicholas Piggin * irq_happened. 536ebb37cf3SNicholas Piggin * 537ebb37cf3SNicholas Piggin * NMI interrupts can not check this when they return, so the 538ebb37cf3SNicholas Piggin * decrementer hardware exception is raised, which will fire 539ebb37cf3SNicholas Piggin * when interrupts are next enabled. 540ebb37cf3SNicholas Piggin * 541ebb37cf3SNicholas Piggin * BookE does not support this yet, it must audit all NMI 542ebb37cf3SNicholas Piggin * interrupt handlers to ensure they call nmi_enter() so this 543ebb37cf3SNicholas Piggin * check would be correct. 544ebb37cf3SNicholas Piggin */ 545ebb37cf3SNicholas Piggin if (IS_ENABLED(CONFIG_BOOKE) || !irqs_disabled() || in_nmi()) { 546ebb37cf3SNicholas Piggin set_dec(1); 547ebb37cf3SNicholas Piggin } else { 548ebb37cf3SNicholas Piggin hard_irq_disable(); 549ebb37cf3SNicholas Piggin local_paca->irq_happened |= PACA_IRQ_DEC; 550ebb37cf3SNicholas Piggin } 551ebb37cf3SNicholas Piggin preempt_enable(); 552ebb37cf3SNicholas Piggin } 553ebb37cf3SNicholas Piggin 5540fe1ac48SPaul Mackerras #else /* 32-bit */ 5550fe1ac48SPaul Mackerras 556e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5570fe1ac48SPaul Mackerras 55869111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 55969111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 56069111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 561105988c0SPaul Mackerras 5624f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5630fe1ac48SPaul Mackerras { 5640fe1ac48SPaul Mackerras preempt_disable(); 565e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5660fe1ac48SPaul Mackerras set_dec(1); 5670fe1ac48SPaul Mackerras preempt_enable(); 5680fe1ac48SPaul Mackerras } 5690fe1ac48SPaul Mackerras 570ebb37cf3SNicholas Piggin #endif /* 32 vs 64 bit */ 571ebb37cf3SNicholas Piggin 572e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 573105988c0SPaul Mackerras 574e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 575e360adbeSPeter Zijlstra #define clear_irq_work_pending() 576105988c0SPaul Mackerras 577e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 578105988c0SPaul Mackerras 5791b783955SPreeti U Murthy /* 5801b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 5811b783955SPreeti U Murthy * with interrupts disabled. 5821b783955SPreeti U Murthy */ 5831b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs) 5841b783955SPreeti U Murthy { 5853f984620SNicholas Piggin struct clock_event_device *evt = this_cpu_ptr(&decrementers); 58669111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 5873f984620SNicholas Piggin struct pt_regs *old_regs; 5883f984620SNicholas Piggin u64 now; 5891b783955SPreeti U Murthy 5901b783955SPreeti U Murthy /* Some implementations of hotplug will get timer interrupts while 5911b783955SPreeti U Murthy * offline, just ignore these and we also need to set 5921b783955SPreeti U Murthy * decrementers_next_tb as MAX to make sure __check_irq_replay 5931b783955SPreeti U Murthy * don't replay timer interrupt when return, otherwise we'll trap 5941b783955SPreeti U Murthy * here infinitely :( 5951b783955SPreeti U Murthy */ 596a7cba02dSNicholas Piggin if (unlikely(!cpu_online(smp_processor_id()))) { 5971b783955SPreeti U Murthy *next_tb = ~(u64)0; 598a7cba02dSNicholas Piggin set_dec(decrementer_max); 5991b783955SPreeti U Murthy return; 6001b783955SPreeti U Murthy } 6011b783955SPreeti U Murthy 602a7cba02dSNicholas Piggin /* Ensure a positive value is written to the decrementer, or else 603a7cba02dSNicholas Piggin * some CPUs will continue to take decrementer exceptions. When the 604a7cba02dSNicholas Piggin * PPC_WATCHDOG (decrementer based) is configured, keep this at most 605a7cba02dSNicholas Piggin * 31 bits, which is about 4 seconds on most systems, which gives 606a7cba02dSNicholas Piggin * the watchdog a chance of catching timer interrupt hard lockups. 607a7cba02dSNicholas Piggin */ 608a7cba02dSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_WATCHDOG)) 609a7cba02dSNicholas Piggin set_dec(0x7fffffff); 610a7cba02dSNicholas Piggin else 611a7cba02dSNicholas Piggin set_dec(decrementer_max); 612a7cba02dSNicholas Piggin 6131b783955SPreeti U Murthy /* Conditionally hard-enable interrupts now that the DEC has been 6141b783955SPreeti U Murthy * bumped to its maximum value 6151b783955SPreeti U Murthy */ 6161b783955SPreeti U Murthy may_hard_irq_enable(); 6171b783955SPreeti U Murthy 6181b783955SPreeti U Murthy 6196e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 6201b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 6211b783955SPreeti U Murthy do_IRQ(regs); 6221b783955SPreeti U Murthy #endif 6231b783955SPreeti U Murthy 6241b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 6251b783955SPreeti U Murthy irq_enter(); 6263f984620SNicholas Piggin trace_timer_interrupt_entry(regs); 6271b783955SPreeti U Murthy 6283f984620SNicholas Piggin if (test_irq_work_pending()) { 6293f984620SNicholas Piggin clear_irq_work_pending(); 6303f984620SNicholas Piggin irq_work_run(); 6313f984620SNicholas Piggin } 6323f984620SNicholas Piggin 6333f984620SNicholas Piggin now = get_tb_or_rtc(); 6343f984620SNicholas Piggin if (now >= *next_tb) { 6353f984620SNicholas Piggin *next_tb = ~(u64)0; 6363f984620SNicholas Piggin if (evt->event_handler) 6373f984620SNicholas Piggin evt->event_handler(evt); 6383f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_event); 6393f984620SNicholas Piggin } else { 6403f984620SNicholas Piggin now = *next_tb - now; 6413f984620SNicholas Piggin if (now <= decrementer_max) 6423f984620SNicholas Piggin set_dec(now); 6433f984620SNicholas Piggin /* We may have raced with new irq work */ 6443f984620SNicholas Piggin if (test_irq_work_pending()) 6453f984620SNicholas Piggin set_dec(1); 6463f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_others); 6473f984620SNicholas Piggin } 6483f984620SNicholas Piggin 6493f984620SNicholas Piggin trace_timer_interrupt_exit(regs); 650f2783c15SPaul Mackerras irq_exit(); 6517d12e780SDavid Howells set_irq_regs(old_regs); 652f2783c15SPaul Mackerras } 6539445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 654f2783c15SPaul Mackerras 655bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 6563f984620SNicholas Piggin void timer_broadcast_interrupt(void) 6573f984620SNicholas Piggin { 6583f984620SNicholas Piggin u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 6593f984620SNicholas Piggin 6603f984620SNicholas Piggin *next_tb = ~(u64)0; 6613f984620SNicholas Piggin tick_receive_broadcast(); 662e360cd37SNicholas Piggin __this_cpu_inc(irq_stat.broadcast_irqs_event); 6633f984620SNicholas Piggin } 664bc907113SNicholas Piggin #endif 6653f984620SNicholas Piggin 666dabe859eSPaul Mackerras /* 667dabe859eSPaul Mackerras * Hypervisor decrementer interrupts shouldn't occur but are sometimes 668dabe859eSPaul Mackerras * left pending on exit from a KVM guest. We don't need to do anything 669dabe859eSPaul Mackerras * to clear them, as they are edge-triggered. 670dabe859eSPaul Mackerras */ 671dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs) 672dabe859eSPaul Mackerras { 673dabe859eSPaul Mackerras } 674dabe859eSPaul Mackerras 6757ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 676d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void) 6777ac5dde9SScott Wood { 6787ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6797ac5dde9SScott Wood * with suspending. 6807ac5dde9SScott Wood */ 6817ac5dde9SScott Wood 68279901024SOliver O'Halloran set_dec(decrementer_max); 6837ac5dde9SScott Wood local_irq_disable(); 68479901024SOliver O'Halloran set_dec(decrementer_max); 6857ac5dde9SScott Wood } 6867ac5dde9SScott Wood 687d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void) 6887ac5dde9SScott Wood { 6897ac5dde9SScott Wood local_irq_enable(); 6907ac5dde9SScott Wood } 6917ac5dde9SScott Wood 6927ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6937ac5dde9SScott Wood void arch_suspend_disable_irqs(void) 6947ac5dde9SScott Wood { 6957ac5dde9SScott Wood if (ppc_md.suspend_disable_irqs) 6967ac5dde9SScott Wood ppc_md.suspend_disable_irqs(); 6977ac5dde9SScott Wood generic_suspend_disable_irqs(); 6987ac5dde9SScott Wood } 6997ac5dde9SScott Wood 7007ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 7017ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 7027ac5dde9SScott Wood { 7037ac5dde9SScott Wood generic_suspend_enable_irqs(); 7047ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 7057ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 7067ac5dde9SScott Wood } 7077ac5dde9SScott Wood #endif 7087ac5dde9SScott Wood 709b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 710b6c295dfSPaul Mackerras { 711b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 712b6c295dfSPaul Mackerras } 713b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 714b6c295dfSPaul Mackerras 715f2783c15SPaul Mackerras /* 716f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 717f2783c15SPaul Mackerras * 718f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 719f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 720f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 721f2783c15SPaul Mackerras */ 7226b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void) 723f2783c15SPaul Mackerras { 72496c44507SPaul Mackerras if (__USE_RTC()) 72596c44507SPaul Mackerras return get_rtc(); 726fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 727f2783c15SPaul Mackerras } 728f2783c15SPaul Mackerras 7294be1b297SCyril Bur 7304be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 7314be1b297SCyril Bur 7324be1b297SCyril Bur /* 7334be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 7344be1b297SCyril Bur * kernels. 7354be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 7364be1b297SCyril Bur */ 7374be1b297SCyril Bur unsigned long long running_clock(void) 7384be1b297SCyril Bur { 7394be1b297SCyril Bur /* 7404be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 7414be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 7424be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 7434be1b297SCyril Bur * 7444be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7454be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7464be1b297SCyril Bur */ 7474be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7484be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7494be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7504be1b297SCyril Bur 7514be1b297SCyril Bur /* 7524be1b297SCyril Bur * This is a next best approximation without a VTB. 7534be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7544be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7554be1b297SCyril Bur * VTB so it makes no difference anyway. 7564be1b297SCyril Bur */ 7579f3768e0SFrédéric Weisbecker return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 7584be1b297SCyril Bur } 7594be1b297SCyril Bur #endif 7604be1b297SCyril Bur 7610bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 762f2783c15SPaul Mackerras { 763f2783c15SPaul Mackerras struct device_node *cpu; 7646f7aba7bSAnton Blanchard const __be32 *fp; 7650bb474a4SAnton Blanchard int found = 0; 766f2783c15SPaul Mackerras 7670bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 768f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 769f2783c15SPaul Mackerras 770d8a8188dSOlaf Hering if (cpu) { 771e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 772d8a8188dSOlaf Hering if (fp) { 7730bb474a4SAnton Blanchard found = 1; 774a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 775f2783c15SPaul Mackerras } 7760bb474a4SAnton Blanchard 7770bb474a4SAnton Blanchard of_node_put(cpu); 778f2783c15SPaul Mackerras } 7790bb474a4SAnton Blanchard 7800bb474a4SAnton Blanchard return found; 7810bb474a4SAnton Blanchard } 7820bb474a4SAnton Blanchard 783e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 78477c0a700SBenjamin Herrenschmidt { 78577c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 7866e2f03e2SIvan Mikhaylov unsigned int tcr; 7876e2f03e2SIvan Mikhaylov 78877c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 78977c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 79077c0a700SBenjamin Herrenschmidt 7916e2f03e2SIvan Mikhaylov tcr = mfspr(SPRN_TCR); 7926e2f03e2SIvan Mikhaylov /* 7936e2f03e2SIvan Mikhaylov * The watchdog may have already been enabled by u-boot. So leave 7946e2f03e2SIvan Mikhaylov * TRC[WP] (Watchdog Period) alone. 7956e2f03e2SIvan Mikhaylov */ 7966e2f03e2SIvan Mikhaylov tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 7976e2f03e2SIvan Mikhaylov tcr |= TCR_DIE; /* Enable decrementer */ 7986e2f03e2SIvan Mikhaylov mtspr(SPRN_TCR, tcr); 7996e2f03e2SIvan Mikhaylov #endif 80077c0a700SBenjamin Herrenschmidt } 80177c0a700SBenjamin Herrenschmidt 8020bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 8030bb474a4SAnton Blanchard { 8040bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 8050bb474a4SAnton Blanchard 8060bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 8070bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 8080bb474a4SAnton Blanchard 809f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 810f2783c15SPaul Mackerras "(not found)\n"); 8110bb474a4SAnton Blanchard } 812f2783c15SPaul Mackerras 8130bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 8140bb474a4SAnton Blanchard 8150bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 8160bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 8170bb474a4SAnton Blanchard 8180bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 8190bb474a4SAnton Blanchard "(not found)\n"); 820f2783c15SPaul Mackerras } 821f2783c15SPaul Mackerras } 822f2783c15SPaul Mackerras 8235235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now) 824f2783c15SPaul Mackerras { 825f2783c15SPaul Mackerras struct rtc_time tm; 826f2783c15SPaul Mackerras 827aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 828023f333aSJason Gunthorpe return -ENODEV; 829aa3be5f3STony Breeds 8305235afa8SArnd Bergmann rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm); 831aa3be5f3STony Breeds 832aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 833aa3be5f3STony Breeds } 834aa3be5f3STony Breeds 8355bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts) 836aa3be5f3STony Breeds { 837aa3be5f3STony Breeds struct rtc_time tm; 838aa3be5f3STony Breeds static int first = 1; 839aa3be5f3STony Breeds 840d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 841aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 842aa3be5f3STony Breeds if (first) { 843aa3be5f3STony Breeds first = 0; 844aa3be5f3STony Breeds if (ppc_md.time_init) 845aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 846aa3be5f3STony Breeds 847aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 848d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 849d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 850d90246cdSMartin Schwidefsky return; 851aa3be5f3STony Breeds } 852d90246cdSMartin Schwidefsky } 853d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 854d90246cdSMartin Schwidefsky ts->tv_sec = 0; 855d90246cdSMartin Schwidefsky return; 856d90246cdSMartin Schwidefsky } 857f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 858978d7eb3SBenjamin Herrenschmidt 8595bfd6435SArnd Bergmann ts->tv_sec = rtc_tm_to_time64(&tm); 860f2783c15SPaul Mackerras } 861f2783c15SPaul Mackerras 8625bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts) 863978d7eb3SBenjamin Herrenschmidt { 864978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 865978d7eb3SBenjamin Herrenschmidt 866978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 867978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 868978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 869978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 870978d7eb3SBenjamin Herrenschmidt } 871978d7eb3SBenjamin Herrenschmidt 872978d7eb3SBenjamin Herrenschmidt } 873978d7eb3SBenjamin Herrenschmidt 8744a4cfe38STony Breeds /* clocksource code */ 8756b847d79SSantosh Sivaraj static notrace u64 rtc_read(struct clocksource *cs) 8764a4cfe38STony Breeds { 877a5a1d1c2SThomas Gleixner return (u64)get_rtc(); 8784a4cfe38STony Breeds } 8794a4cfe38STony Breeds 8806b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs) 8814a4cfe38STony Breeds { 882a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8834a4cfe38STony Breeds } 8844a4cfe38STony Breeds 885d4cfb113SPaul Mackerras 886d4cfb113SPaul Mackerras void update_vsyscall(struct timekeeper *tk) 8874a4cfe38STony Breeds { 888d4cfb113SPaul Mackerras struct timespec xt; 889d4cfb113SPaul Mackerras struct clocksource *clock = tk->tkr_mono.clock; 890d4cfb113SPaul Mackerras u32 mult = tk->tkr_mono.mult; 891d4cfb113SPaul Mackerras u32 shift = tk->tkr_mono.shift; 892d4cfb113SPaul Mackerras u64 cycle_last = tk->tkr_mono.cycle_last; 893b0797b60SJohn Stultz u64 new_tb_to_xs, new_stamp_xsec; 894d4cfb113SPaul Mackerras u64 frac_sec; 8954a4cfe38STony Breeds 8964a4cfe38STony Breeds if (clock != &clocksource_timebase) 8974a4cfe38STony Breeds return; 8984a4cfe38STony Breeds 899d4cfb113SPaul Mackerras xt.tv_sec = tk->xtime_sec; 900d4cfb113SPaul Mackerras xt.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 901d4cfb113SPaul Mackerras 9024a4cfe38STony Breeds /* Make userspace gettimeofday spin until we're done. */ 9034a4cfe38STony Breeds ++vdso_data->tb_update_count; 9044a4cfe38STony Breeds smp_mb(); 9054a4cfe38STony Breeds 906d4cfb113SPaul Mackerras /* 907d4cfb113SPaul Mackerras * This computes ((2^20 / 1e9) * mult) >> shift as a 908d4cfb113SPaul Mackerras * 0.64 fixed-point fraction. 909d4cfb113SPaul Mackerras * The computation in the else clause below won't overflow 910d4cfb113SPaul Mackerras * (as long as the timebase frequency is >= 1.049 MHz) 911d4cfb113SPaul Mackerras * but loses precision because we lose the low bits of the constant 912d4cfb113SPaul Mackerras * in the shift. Note that 19342813113834067 ~= 2^(20+64) / 1e9. 913d4cfb113SPaul Mackerras * For a shift of 24 the error is about 0.5e-9, or about 0.5ns 914d4cfb113SPaul Mackerras * over a second. (Shift values are usually 22, 23 or 24.) 915d4cfb113SPaul Mackerras * For high frequency clocks such as the 512MHz timebase clock 916d4cfb113SPaul Mackerras * on POWER[6789], the mult value is small (e.g. 32768000) 917d4cfb113SPaul Mackerras * and so we can shift the constant by 16 initially 918d4cfb113SPaul Mackerras * (295147905179 ~= 2^(20+64-16) / 1e9) and then do the 919d4cfb113SPaul Mackerras * remaining shifts after the multiplication, which gives a 920d4cfb113SPaul Mackerras * more accurate result (e.g. with mult = 32768000, shift = 24, 921d4cfb113SPaul Mackerras * the error is only about 1.2e-12, or 0.7ns over 10 minutes). 922d4cfb113SPaul Mackerras */ 923d4cfb113SPaul Mackerras if (mult <= 62500000 && clock->shift >= 16) 924d4cfb113SPaul Mackerras new_tb_to_xs = ((u64) mult * 295147905179ULL) >> (clock->shift - 16); 925d4cfb113SPaul Mackerras else 92611b8633aSAnton Blanchard new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift); 927b0797b60SJohn Stultz 928d4cfb113SPaul Mackerras /* 929d4cfb113SPaul Mackerras * Compute the fractional second in units of 2^-32 seconds. 930d4cfb113SPaul Mackerras * The fractional second is tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift 931d4cfb113SPaul Mackerras * in nanoseconds, so multiplying that by 2^32 / 1e9 gives 932d4cfb113SPaul Mackerras * it in units of 2^-32 seconds. 933d4cfb113SPaul Mackerras * We assume shift <= 32 because clocks_calc_mult_shift() 934d4cfb113SPaul Mackerras * generates shift values in the range 0 - 32. 935d4cfb113SPaul Mackerras */ 936d4cfb113SPaul Mackerras frac_sec = tk->tkr_mono.xtime_nsec << (32 - shift); 937d4cfb113SPaul Mackerras do_div(frac_sec, NSEC_PER_SEC); 938d4cfb113SPaul Mackerras 939d4cfb113SPaul Mackerras /* 940d4cfb113SPaul Mackerras * Work out new stamp_xsec value for any legacy users of systemcfg. 941d4cfb113SPaul Mackerras * stamp_xsec is in units of 2^-20 seconds. 942d4cfb113SPaul Mackerras */ 943d4cfb113SPaul Mackerras new_stamp_xsec = frac_sec >> 12; 944d4cfb113SPaul Mackerras new_stamp_xsec += tk->xtime_sec * XSEC_PER_SEC; 94547916be4SThomas Gleixner 946b0797b60SJohn Stultz /* 947b0797b60SJohn Stultz * tb_update_count is used to allow the userspace gettimeofday code 948b0797b60SJohn Stultz * to assure itself that it sees a consistent view of the tb_to_xs and 949b0797b60SJohn Stultz * stamp_xsec variables. It reads the tb_update_count, then reads 950b0797b60SJohn Stultz * tb_to_xs and stamp_xsec and then reads tb_update_count again. If 951b0797b60SJohn Stultz * the two values of tb_update_count match and are even then the 952b0797b60SJohn Stultz * tb_to_xs and stamp_xsec values are consistent. If not, then it 953b0797b60SJohn Stultz * loops back and reads them again until this criteria is met. 954b0797b60SJohn Stultz */ 9554a0e6377SThomas Gleixner vdso_data->tb_orig_stamp = cycle_last; 956b0797b60SJohn Stultz vdso_data->stamp_xsec = new_stamp_xsec; 957b0797b60SJohn Stultz vdso_data->tb_to_xs = new_tb_to_xs; 958d4cfb113SPaul Mackerras vdso_data->wtom_clock_sec = tk->wall_to_monotonic.tv_sec; 959d4cfb113SPaul Mackerras vdso_data->wtom_clock_nsec = tk->wall_to_monotonic.tv_nsec; 960d4cfb113SPaul Mackerras vdso_data->stamp_xtime = xt; 96147916be4SThomas Gleixner vdso_data->stamp_sec_fraction = frac_sec; 962b0797b60SJohn Stultz smp_wmb(); 963b0797b60SJohn Stultz ++(vdso_data->tb_update_count); 9644a4cfe38STony Breeds } 9654a4cfe38STony Breeds 9664a4cfe38STony Breeds void update_vsyscall_tz(void) 9674a4cfe38STony Breeds { 9684a4cfe38STony Breeds vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 9694a4cfe38STony Breeds vdso_data->tz_dsttime = sys_tz.tz_dsttime; 9704a4cfe38STony Breeds } 9714a4cfe38STony Breeds 9721c21a293SMichael Ellerman static void __init clocksource_init(void) 9734a4cfe38STony Breeds { 9744a4cfe38STony Breeds struct clocksource *clock; 9754a4cfe38STony Breeds 9764a4cfe38STony Breeds if (__USE_RTC()) 9774a4cfe38STony Breeds clock = &clocksource_rtc; 9784a4cfe38STony Breeds else 9794a4cfe38STony Breeds clock = &clocksource_timebase; 9804a4cfe38STony Breeds 98111b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 9824a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 9834a4cfe38STony Breeds clock->name); 9844a4cfe38STony Breeds return; 9854a4cfe38STony Breeds } 9864a4cfe38STony Breeds 9874a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 9884a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 9894a4cfe38STony Breeds } 9904a4cfe38STony Breeds 991d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 992d831d0b8STony Breeds struct clock_event_device *dev) 993d831d0b8STony Breeds { 99469111bacSChristoph Lameter __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt); 995d831d0b8STony Breeds set_dec(evt); 9960215f7d8SBenjamin Herrenschmidt 9970215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 9980215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 9990215f7d8SBenjamin Herrenschmidt set_dec(1); 10000215f7d8SBenjamin Herrenschmidt 1001d831d0b8STony Breeds return 0; 1002d831d0b8STony Breeds } 1003d831d0b8STony Breeds 100437a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 1005d831d0b8STony Breeds { 100679901024SOliver O'Halloran decrementer_set_next_event(decrementer_max, dev); 100737a13e78SViresh Kumar return 0; 1008d831d0b8STony Breeds } 1009d831d0b8STony Breeds 1010d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 1011d831d0b8STony Breeds { 10127df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 1013d831d0b8STony Breeds 1014d831d0b8STony Breeds *dec = decrementer_clockevent; 1015320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 1016d831d0b8STony Breeds 10178b78fdb0SAnton Blanchard clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max); 10188b78fdb0SAnton Blanchard 1019b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 1020d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 1021b4d16ab5SMichael Ellerman 1022b4d16ab5SMichael Ellerman /* Set values for KVM, see kvm_emulate_dec() */ 1023b4d16ab5SMichael Ellerman decrementer_clockevent.mult = dec->mult; 1024b4d16ab5SMichael Ellerman decrementer_clockevent.shift = dec->shift; 1025d831d0b8STony Breeds } 1026d831d0b8STony Breeds 102779901024SOliver O'Halloran static void enable_large_decrementer(void) 102879901024SOliver O'Halloran { 102979901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 103079901024SOliver O'Halloran return; 103179901024SOliver O'Halloran 103279901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 103379901024SOliver O'Halloran return; 103479901024SOliver O'Halloran 103579901024SOliver O'Halloran /* 103679901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 103779901024SOliver O'Halloran * otherwise firmware should have done it for us. 103879901024SOliver O'Halloran */ 103979901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 104079901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 104179901024SOliver O'Halloran } 104279901024SOliver O'Halloran 104379901024SOliver O'Halloran static void __init set_decrementer_max(void) 104479901024SOliver O'Halloran { 104579901024SOliver O'Halloran struct device_node *cpu; 104679901024SOliver O'Halloran u32 bits = 32; 104779901024SOliver O'Halloran 104879901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 104979901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 105079901024SOliver O'Halloran return; 105179901024SOliver O'Halloran 105279901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 105379901024SOliver O'Halloran 105479901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 105579901024SOliver O'Halloran if (bits > 64 || bits < 32) { 105679901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 105779901024SOliver O'Halloran bits = 32; 105879901024SOliver O'Halloran } 105979901024SOliver O'Halloran 106079901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 106179901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 106279901024SOliver O'Halloran } 106379901024SOliver O'Halloran 106479901024SOliver O'Halloran of_node_put(cpu); 106579901024SOliver O'Halloran 106679901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 106779901024SOliver O'Halloran bits, decrementer_max); 106879901024SOliver O'Halloran } 106979901024SOliver O'Halloran 1070c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 1071d831d0b8STony Breeds { 10728b78fdb0SAnton Blanchard register_decrementer_clockevent(smp_processor_id()); 1073d831d0b8STony Breeds } 1074d831d0b8STony Breeds 1075d831d0b8STony Breeds void secondary_cpu_time_init(void) 1076d831d0b8STony Breeds { 107779901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 107879901024SOliver O'Halloran enable_large_decrementer(); 107979901024SOliver O'Halloran 108077c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 108177c0a700SBenjamin Herrenschmidt * such as BookE 108277c0a700SBenjamin Herrenschmidt */ 108377c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 108477c0a700SBenjamin Herrenschmidt 1085d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 1086d831d0b8STony Breeds * call here ! */ 1087d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 1088d831d0b8STony Breeds } 1089d831d0b8STony Breeds 1090f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 1091f2783c15SPaul Mackerras void __init time_init(void) 1092f2783c15SPaul Mackerras { 1093f2783c15SPaul Mackerras struct div_result res; 1094d75d68cfSPaul Mackerras u64 scale; 1095f2783c15SPaul Mackerras unsigned shift; 1096f2783c15SPaul Mackerras 109796c44507SPaul Mackerras if (__USE_RTC()) { 109896c44507SPaul Mackerras /* 601 processor: dec counts down by 128 every 128ns */ 109996c44507SPaul Mackerras ppc_tb_freq = 1000000000; 110096c44507SPaul Mackerras } else { 110196c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 1102f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 1103224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 1104374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 1105224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 1106374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 110796c44507SPaul Mackerras } 1108374e99d4SPaul Mackerras 1109374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 1110092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 1111374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 1112c6622f63SPaul Mackerras calc_cputime_factors(); 1113092b8f34SPaul Mackerras 1114092b8f34SPaul Mackerras /* 1115f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 1116f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 1117f2783c15SPaul Mackerras * which is the timebase frequency. 1118f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 1119f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 1120f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 1121f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 1122f2783c15SPaul Mackerras * sched_clock(). 1123f2783c15SPaul Mackerras */ 1124f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1125f2783c15SPaul Mackerras scale = res.result_low; 1126f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1127f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1128f2783c15SPaul Mackerras res.result_high >>= 1; 1129f2783c15SPaul Mackerras } 1130f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1131f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1132fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 1133c27da339SBenjamin Herrenschmidt boot_tb = get_tb_or_rtc(); 1134f2783c15SPaul Mackerras 1135092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1136092b8f34SPaul Mackerras if (timezone_offset) { 1137092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1138092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1139092b8f34SPaul Mackerras } 1140092b8f34SPaul Mackerras 1141a7f290daSBenjamin Herrenschmidt vdso_data->tb_update_count = 0; 1142a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1143f2783c15SPaul Mackerras 114479901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 114579901024SOliver O'Halloran set_decrementer_max(); 114679901024SOliver O'Halloran enable_large_decrementer(); 114779901024SOliver O'Halloran 114877c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 114977c0a700SBenjamin Herrenschmidt * such as BookE 115077c0a700SBenjamin Herrenschmidt */ 115177c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 115277c0a700SBenjamin Herrenschmidt 1153f5339277SStephen Rothwell /* Register the clocksource */ 11544a4cfe38STony Breeds clocksource_init(); 11554a4cfe38STony Breeds 1156d831d0b8STony Breeds init_decrementer_clockevent(); 11570d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1158f0d37300SKevin Hao 1159f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK 1160f0d37300SKevin Hao of_clk_init(NULL); 1161f0d37300SKevin Hao #endif 1162f2783c15SPaul Mackerras } 1163f2783c15SPaul Mackerras 1164f2783c15SPaul Mackerras /* 1165f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1166f2783c15SPaul Mackerras * result. 1167f2783c15SPaul Mackerras */ 1168f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1169f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1170f2783c15SPaul Mackerras { 1171f2783c15SPaul Mackerras unsigned long a, b, c, d; 1172f2783c15SPaul Mackerras unsigned long w, x, y, z; 1173f2783c15SPaul Mackerras u64 ra, rb, rc; 1174f2783c15SPaul Mackerras 1175f2783c15SPaul Mackerras a = dividend_high >> 32; 1176f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1177f2783c15SPaul Mackerras c = dividend_low >> 32; 1178f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1179f2783c15SPaul Mackerras 1180f2783c15SPaul Mackerras w = a / divisor; 1181f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1182f2783c15SPaul Mackerras 1183f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1184f2783c15SPaul Mackerras x = ra; 1185f2783c15SPaul Mackerras 1186f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1187f2783c15SPaul Mackerras y = rb; 1188f2783c15SPaul Mackerras 1189f2783c15SPaul Mackerras do_div(rc, divisor); 1190f2783c15SPaul Mackerras z = rc; 1191f2783c15SPaul Mackerras 1192f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1193f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1194f2783c15SPaul Mackerras 1195f2783c15SPaul Mackerras } 1196bcd68a70SGeert Uytterhoeven 1197177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1198177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1199177996e6SBenjamin Herrenschmidt { 1200177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1201177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1202177996e6SBenjamin Herrenschmidt */ 1203177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1204177996e6SBenjamin Herrenschmidt } 1205177996e6SBenjamin Herrenschmidt 1206169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1207169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1208169047f4SArnd Bergmann { 1209169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1210890ae797SAlexandre Belloni return 0; 1211169047f4SArnd Bergmann } 1212169047f4SArnd Bergmann 1213169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1214169047f4SArnd Bergmann { 1215169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1216169047f4SArnd Bergmann return -EOPNOTSUPP; 1217169047f4SArnd Bergmann 1218169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1219169047f4SArnd Bergmann return -EOPNOTSUPP; 1220169047f4SArnd Bergmann 1221169047f4SArnd Bergmann return 0; 1222169047f4SArnd Bergmann } 1223169047f4SArnd Bergmann 1224169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1225169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1226169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1227169047f4SArnd Bergmann }; 1228169047f4SArnd Bergmann 1229bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1230bcd68a70SGeert Uytterhoeven { 1231bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1232bcd68a70SGeert Uytterhoeven 1233bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1234bcd68a70SGeert Uytterhoeven return -ENODEV; 1235bcd68a70SGeert Uytterhoeven 1236169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1237169047f4SArnd Bergmann &rtc_generic_ops, 1238169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1239bcd68a70SGeert Uytterhoeven 12408c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1241bcd68a70SGeert Uytterhoeven } 1242bcd68a70SGeert Uytterhoeven 12438f6b9512SPaul Gortmaker device_initcall(rtc_init); 1244169047f4SArnd Bergmann #endif 1245