1f2783c15SPaul Mackerras /* 2f2783c15SPaul Mackerras * Common time routines among all ppc machines. 3f2783c15SPaul Mackerras * 4f2783c15SPaul Mackerras * Written by Cort Dougan (cort@cs.nmt.edu) to merge 5f2783c15SPaul Mackerras * Paul Mackerras' version and mine for PReP and Pmac. 6f2783c15SPaul Mackerras * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). 7f2783c15SPaul Mackerras * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) 8f2783c15SPaul Mackerras * 9f2783c15SPaul Mackerras * First round of bugfixes by Gabriel Paubert (paubert@iram.es) 10f2783c15SPaul Mackerras * to make clock more stable (2.4.0-test5). The only thing 11f2783c15SPaul Mackerras * that this code assumes is that the timebases have been synchronized 12f2783c15SPaul Mackerras * by firmware on SMP and are never stopped (never do sleep 13f2783c15SPaul Mackerras * on SMP then, nap and doze are OK). 14f2783c15SPaul Mackerras * 15f2783c15SPaul Mackerras * Speeded up do_gettimeofday by getting rid of references to 16f2783c15SPaul Mackerras * xtime (which required locks for consistency). (mikejc@us.ibm.com) 17f2783c15SPaul Mackerras * 18f2783c15SPaul Mackerras * TODO (not necessarily in this file): 19f2783c15SPaul Mackerras * - improve precision and reproducibility of timebase frequency 20f5339277SStephen Rothwell * measurement at boot time. 21f2783c15SPaul Mackerras * - for astronomical applications: add a new function to get 22f2783c15SPaul Mackerras * non ambiguous timestamps even around leap seconds. This needs 23f2783c15SPaul Mackerras * a new timestamp format and a good name. 24f2783c15SPaul Mackerras * 25f2783c15SPaul Mackerras * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 26f2783c15SPaul Mackerras * "A Kernel Model for Precision Timekeeping" by Dave Mills 27f2783c15SPaul Mackerras * 28f2783c15SPaul Mackerras * This program is free software; you can redistribute it and/or 29f2783c15SPaul Mackerras * modify it under the terms of the GNU General Public License 30f2783c15SPaul Mackerras * as published by the Free Software Foundation; either version 31f2783c15SPaul Mackerras * 2 of the License, or (at your option) any later version. 32f2783c15SPaul Mackerras */ 33f2783c15SPaul Mackerras 34f2783c15SPaul Mackerras #include <linux/errno.h> 354b16f8e2SPaul Gortmaker #include <linux/export.h> 36f2783c15SPaul Mackerras #include <linux/sched.h> 37f2783c15SPaul Mackerras #include <linux/kernel.h> 38f2783c15SPaul Mackerras #include <linux/param.h> 39f2783c15SPaul Mackerras #include <linux/string.h> 40f2783c15SPaul Mackerras #include <linux/mm.h> 41f2783c15SPaul Mackerras #include <linux/interrupt.h> 42f2783c15SPaul Mackerras #include <linux/timex.h> 43f2783c15SPaul Mackerras #include <linux/kernel_stat.h> 44f2783c15SPaul Mackerras #include <linux/time.h> 450d948730SPreeti U Murthy #include <linux/clockchips.h> 46f2783c15SPaul Mackerras #include <linux/init.h> 47f2783c15SPaul Mackerras #include <linux/profile.h> 48f2783c15SPaul Mackerras #include <linux/cpu.h> 49f2783c15SPaul Mackerras #include <linux/security.h> 50f2783c15SPaul Mackerras #include <linux/percpu.h> 51f2783c15SPaul Mackerras #include <linux/rtc.h> 52092b8f34SPaul Mackerras #include <linux/jiffies.h> 53c6622f63SPaul Mackerras #include <linux/posix-timers.h> 547d12e780SDavid Howells #include <linux/irq.h> 55177996e6SBenjamin Herrenschmidt #include <linux/delay.h> 56e360adbeSPeter Zijlstra #include <linux/irq_work.h> 57f0d37300SKevin Hao #include <linux/clk-provider.h> 587f92bc56SDaniel Axtens #include <linux/suspend.h> 59169047f4SArnd Bergmann #include <linux/rtc.h> 606795b85cSAnton Blanchard #include <asm/trace.h> 61f2783c15SPaul Mackerras 62f2783c15SPaul Mackerras #include <asm/io.h> 63f2783c15SPaul Mackerras #include <asm/processor.h> 64f2783c15SPaul Mackerras #include <asm/nvram.h> 65f2783c15SPaul Mackerras #include <asm/cache.h> 66f2783c15SPaul Mackerras #include <asm/machdep.h> 677c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 68f2783c15SPaul Mackerras #include <asm/time.h> 69f2783c15SPaul Mackerras #include <asm/prom.h> 70f2783c15SPaul Mackerras #include <asm/irq.h> 71f2783c15SPaul Mackerras #include <asm/div64.h> 722249ca9dSPaul Mackerras #include <asm/smp.h> 73a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h> 74f2783c15SPaul Mackerras #include <asm/firmware.h> 7506b8e878SMichael Neuling #include <asm/cputime.h> 760545d543SDaniel Axtens #include <asm/asm-prototypes.h> 77f2783c15SPaul Mackerras 784a4cfe38STony Breeds /* powerpc clocksource/clockevent code */ 794a4cfe38STony Breeds 80d831d0b8STony Breeds #include <linux/clockchips.h> 81189374aeSJohn Stultz #include <linux/timekeeper_internal.h> 824a4cfe38STony Breeds 83a5a1d1c2SThomas Gleixner static u64 rtc_read(struct clocksource *); 844a4cfe38STony Breeds static struct clocksource clocksource_rtc = { 854a4cfe38STony Breeds .name = "rtc", 864a4cfe38STony Breeds .rating = 400, 874a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 884a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 894a4cfe38STony Breeds .read = rtc_read, 904a4cfe38STony Breeds }; 914a4cfe38STony Breeds 92a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *); 934a4cfe38STony Breeds static struct clocksource clocksource_timebase = { 944a4cfe38STony Breeds .name = "timebase", 954a4cfe38STony Breeds .rating = 400, 964a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 974a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 984a4cfe38STony Breeds .read = timebase_read, 994a4cfe38STony Breeds }; 1004a4cfe38STony Breeds 10179901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 10279901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 103d831d0b8STony Breeds 104d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 105d831d0b8STony Breeds struct clock_event_device *dev); 10637a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt); 107d831d0b8STony Breeds 1086e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = { 109d831d0b8STony Breeds .name = "decrementer", 110d831d0b8STony Breeds .rating = 200, 111d831d0b8STony Breeds .irq = 0, 112d831d0b8STony Breeds .set_next_event = decrementer_set_next_event, 11337a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 11437a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 11537a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 11637a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 117d831d0b8STony Breeds }; 1186e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 119d831d0b8STony Breeds 1207df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb); 1217df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers); 122d831d0b8STony Breeds 123f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024) 124f2783c15SPaul Mackerras 125f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 126f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 127f2783c15SPaul Mackerras #else 128f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */ 129f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 130f2783c15SPaul Mackerras #endif 131f2783c15SPaul Mackerras 132f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy; 133f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */ 134f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec); 135f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec; 1362cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 137092b8f34SPaul Mackerras 138f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock); 139f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock); 140f2783c15SPaul Mackerras 141fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly; 142fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly; 143364a1246SHeiko Schocher static u64 boot_tb __read_mostly; 144f2783c15SPaul Mackerras 145f2783c15SPaul Mackerras extern struct timezone sys_tz; 146f2783c15SPaul Mackerras static long timezone_offset; 147f2783c15SPaul Mackerras 148f2783c15SPaul Mackerras unsigned long ppc_proc_freq; 14955ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq); 150f2783c15SPaul Mackerras unsigned long ppc_tb_freq; 15155ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq); 15296c44507SPaul Mackerras 153abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 154c6622f63SPaul Mackerras /* 155c6622f63SPaul Mackerras * Factors for converting from cputime_t (timebase ticks) to 1569f5072d4SAndreas Schwab * jiffies, microseconds, seconds, and clock_t (1/USER_HZ seconds). 157c6622f63SPaul Mackerras * These are all stored as 0.64 fixed-point binary fractions. 158c6622f63SPaul Mackerras */ 159c6622f63SPaul Mackerras u64 __cputime_jiffies_factor; 1602cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_jiffies_factor); 1619f5072d4SAndreas Schwab u64 __cputime_usec_factor; 1629f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor); 163c6622f63SPaul Mackerras u64 __cputime_sec_factor; 1642cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_sec_factor); 165c6622f63SPaul Mackerras u64 __cputime_clockt_factor; 1662cf82c02SPaul Mackerras EXPORT_SYMBOL(__cputime_clockt_factor); 167c6622f63SPaul Mackerras 168a42548a1SStanislaw Gruszka cputime_t cputime_one_jiffy; 169a42548a1SStanislaw Gruszka 170c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR 171872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64); 172c223c903SChristophe Leroy #endif 173c223c903SChristophe Leroy 174c223c903SChristophe Leroy #ifdef CONFIG_PPC64 175c223c903SChristophe Leroy #define get_accounting(tsk) (&get_paca()->accounting) 176c223c903SChristophe Leroy #else 177c223c903SChristophe Leroy #define get_accounting(tsk) (&task_thread_info(tsk)->accounting) 178c223c903SChristophe Leroy #endif 179872e439aSPaul Mackerras 180c6622f63SPaul Mackerras static void calc_cputime_factors(void) 181c6622f63SPaul Mackerras { 182c6622f63SPaul Mackerras struct div_result res; 183c6622f63SPaul Mackerras 184c6622f63SPaul Mackerras div128_by_32(HZ, 0, tb_ticks_per_sec, &res); 185c6622f63SPaul Mackerras __cputime_jiffies_factor = res.result_low; 1869f5072d4SAndreas Schwab div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 1879f5072d4SAndreas Schwab __cputime_usec_factor = res.result_low; 188c6622f63SPaul Mackerras div128_by_32(1, 0, tb_ticks_per_sec, &res); 189c6622f63SPaul Mackerras __cputime_sec_factor = res.result_low; 190c6622f63SPaul Mackerras div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res); 191c6622f63SPaul Mackerras __cputime_clockt_factor = res.result_low; 192c6622f63SPaul Mackerras } 193c6622f63SPaul Mackerras 194c6622f63SPaul Mackerras /* 195cf9efce0SPaul Mackerras * Read the SPURR on systems that have it, otherwise the PURR, 196cf9efce0SPaul Mackerras * or if that doesn't exist return the timebase value passed in. 197c6622f63SPaul Mackerras */ 198c223c903SChristophe Leroy static unsigned long read_spurr(unsigned long tb) 199c6622f63SPaul Mackerras { 200cf9efce0SPaul Mackerras if (cpu_has_feature(CPU_FTR_SPURR)) 201cf9efce0SPaul Mackerras return mfspr(SPRN_SPURR); 202c6622f63SPaul Mackerras if (cpu_has_feature(CPU_FTR_PURR)) 203c6622f63SPaul Mackerras return mfspr(SPRN_PURR); 204cf9efce0SPaul Mackerras return tb; 205cf9efce0SPaul Mackerras } 206cf9efce0SPaul Mackerras 207cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR 208cf9efce0SPaul Mackerras 209cf9efce0SPaul Mackerras /* 210cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 211cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 212cf9efce0SPaul Mackerras */ 213cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 214cf9efce0SPaul Mackerras { 215872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 216cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 217cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 218cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 219cf9efce0SPaul Mackerras u64 tb_delta; 220cf9efce0SPaul Mackerras u64 stolen = 0; 221cf9efce0SPaul Mackerras u64 dtb; 222cf9efce0SPaul Mackerras 22384ffae55SAnton Blanchard if (!dtl) 22484ffae55SAnton Blanchard return 0; 22584ffae55SAnton Blanchard 2267ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 227cf9efce0SPaul Mackerras return 0; 2287ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2297ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2307ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2317ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 232cf9efce0SPaul Mackerras barrier(); 2337ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 234cf9efce0SPaul Mackerras /* buffer has overflowed */ 2357ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 236cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 237cf9efce0SPaul Mackerras continue; 238cf9efce0SPaul Mackerras } 239cf9efce0SPaul Mackerras if (dtb > stop_tb) 240cf9efce0SPaul Mackerras break; 24184b07386SAnton Blanchard if (dtl_consumer) 24284b07386SAnton Blanchard dtl_consumer(dtl, i); 243cf9efce0SPaul Mackerras stolen += tb_delta; 244cf9efce0SPaul Mackerras ++i; 245cf9efce0SPaul Mackerras ++dtl; 246cf9efce0SPaul Mackerras if (dtl == dtl_end) 247cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 248cf9efce0SPaul Mackerras } 249cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 250cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 251cf9efce0SPaul Mackerras return stolen; 252c6622f63SPaul Mackerras } 253c6622f63SPaul Mackerras 254c6622f63SPaul Mackerras /* 255cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 256cf9efce0SPaul Mackerras * Called on entry from user mode. 2574603ac18SMichael Neuling */ 258cf9efce0SPaul Mackerras void accumulate_stolen_time(void) 2594603ac18SMichael Neuling { 260cf9efce0SPaul Mackerras u64 sst, ust; 261b18ae08dSTejun Heo u8 save_soft_enabled = local_paca->soft_enabled; 262c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 263b18ae08dSTejun Heo 264b18ae08dSTejun Heo /* We are called early in the exception entry, before 265b18ae08dSTejun Heo * soft/hard_enabled are sync'ed to the expected state 266b18ae08dSTejun Heo * for the exception. We are hard disabled but the PACA 267b18ae08dSTejun Heo * needs to reflect that so various debug stuff doesn't 268b18ae08dSTejun Heo * complain 269b18ae08dSTejun Heo */ 270b18ae08dSTejun Heo local_paca->soft_enabled = 0; 271b18ae08dSTejun Heo 272c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 273c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2748c8b73c4SFrederic Weisbecker acct->stime -= sst; 2758c8b73c4SFrederic Weisbecker acct->utime -= ust; 276f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 277b18ae08dSTejun Heo 278b18ae08dSTejun Heo local_paca->soft_enabled = save_soft_enabled; 2794603ac18SMichael Neuling } 2804603ac18SMichael Neuling 281cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 282cf9efce0SPaul Mackerras { 283*a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 284*a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 285cf9efce0SPaul Mackerras 286*a19ff1a2SFrederic Weisbecker return 0; 287cf9efce0SPaul Mackerras } 288cf9efce0SPaul Mackerras 289cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 290cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 291cf9efce0SPaul Mackerras { 292cf9efce0SPaul Mackerras return 0; 293cf9efce0SPaul Mackerras } 294cf9efce0SPaul Mackerras 295cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 296cf9efce0SPaul Mackerras 2974603ac18SMichael Neuling /* 298c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 299c6622f63SPaul Mackerras * or soft irq state. 300c6622f63SPaul Mackerras */ 301c223c903SChristophe Leroy static unsigned long vtime_delta(struct task_struct *tsk, 302*a19ff1a2SFrederic Weisbecker unsigned long *stime_scaled, 303*a19ff1a2SFrederic Weisbecker unsigned long *steal_time) 304c6622f63SPaul Mackerras { 305c223c903SChristophe Leroy unsigned long now, nowscaled, deltascaled; 306*a19ff1a2SFrederic Weisbecker unsigned long stime; 307*a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 308c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 309c6622f63SPaul Mackerras 3101b2852b1SFrederic Weisbecker WARN_ON_ONCE(!irqs_disabled()); 3111b2852b1SFrederic Weisbecker 312cf9efce0SPaul Mackerras now = mftb(); 3134603ac18SMichael Neuling nowscaled = read_spurr(now); 314*a19ff1a2SFrederic Weisbecker stime = now - acct->starttime; 315c223c903SChristophe Leroy acct->starttime = now; 316c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 317c223c903SChristophe Leroy acct->startspurr = nowscaled; 318cf9efce0SPaul Mackerras 319*a19ff1a2SFrederic Weisbecker *steal_time = calculate_stolen_time(now); 320cf9efce0SPaul Mackerras 321*a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 3228c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 323cf9efce0SPaul Mackerras 324cf9efce0SPaul Mackerras /* 325cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 326cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 327cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 328cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 329cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 330cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 331cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 332cf9efce0SPaul Mackerras * used by account_process_tick. 333cf9efce0SPaul Mackerras */ 334*a19ff1a2SFrederic Weisbecker *stime_scaled = stime; 335*a19ff1a2SFrederic Weisbecker utime_scaled = utime; 336*a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 337*a19ff1a2SFrederic Weisbecker if (utime) { 338*a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled * stime / (stime + utime); 339*a19ff1a2SFrederic Weisbecker utime_scaled = deltascaled - *stime_scaled; 340cf9efce0SPaul Mackerras } else { 341*a19ff1a2SFrederic Weisbecker *stime_scaled = deltascaled; 342c6622f63SPaul Mackerras } 343cf9efce0SPaul Mackerras } 344*a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 345cf9efce0SPaul Mackerras 346*a19ff1a2SFrederic Weisbecker return stime; 347a7e1a9e3SFrederic Weisbecker } 348a7e1a9e3SFrederic Weisbecker 349fd25b4c2SFrederic Weisbecker void vtime_account_system(struct task_struct *tsk) 350a7e1a9e3SFrederic Weisbecker { 351*a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 352*a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 353a7e1a9e3SFrederic Weisbecker 354*a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 355*a19ff1a2SFrederic Weisbecker 356*a19ff1a2SFrederic Weisbecker stime -= min(stime, steal_time); 357*a19ff1a2SFrederic Weisbecker acct->steal_time += steal_time; 358*a19ff1a2SFrederic Weisbecker 359*a19ff1a2SFrederic Weisbecker if ((tsk->flags & PF_VCPU) && !irq_count()) { 360*a19ff1a2SFrederic Weisbecker acct->gtime += stime; 361*a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 362*a19ff1a2SFrederic Weisbecker } else { 363*a19ff1a2SFrederic Weisbecker if (hardirq_count()) 364*a19ff1a2SFrederic Weisbecker acct->hardirq_time += stime; 365*a19ff1a2SFrederic Weisbecker else if (in_serving_softirq()) 366*a19ff1a2SFrederic Weisbecker acct->softirq_time += stime; 367*a19ff1a2SFrederic Weisbecker else 368*a19ff1a2SFrederic Weisbecker acct->stime += stime; 369*a19ff1a2SFrederic Weisbecker 370*a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 371*a19ff1a2SFrederic Weisbecker } 372a7e1a9e3SFrederic Weisbecker } 373c11f11fcSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_system); 374a7e1a9e3SFrederic Weisbecker 375fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 376a7e1a9e3SFrederic Weisbecker { 377*a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 378*a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 379a7e1a9e3SFrederic Weisbecker 380*a19ff1a2SFrederic Weisbecker stime = vtime_delta(tsk, &stime_scaled, &steal_time); 381*a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 382cf9efce0SPaul Mackerras } 383c6622f63SPaul Mackerras 384c6622f63SPaul Mackerras /* 385bcebdf84SFrederic Weisbecker * Transfer the user time accumulated in the paca 386bcebdf84SFrederic Weisbecker * by the exception entry and exit code to the generic 387bcebdf84SFrederic Weisbecker * process user time records. 388c6622f63SPaul Mackerras * Must be called with interrupts disabled. 389bcebdf84SFrederic Weisbecker * Assumes that vtime_account_system/idle() has been called 390bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 391cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 392c6622f63SPaul Mackerras */ 393bcebdf84SFrederic Weisbecker void vtime_account_user(struct task_struct *tsk) 394c6622f63SPaul Mackerras { 395c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 396c6622f63SPaul Mackerras 397*a19ff1a2SFrederic Weisbecker if (acct->utime) 398*a19ff1a2SFrederic Weisbecker account_user_time(tsk, acct->utime); 399*a19ff1a2SFrederic Weisbecker 400*a19ff1a2SFrederic Weisbecker if (acct->utime_scaled) 401*a19ff1a2SFrederic Weisbecker tsk->utimescaled += acct->utime_scaled; 402*a19ff1a2SFrederic Weisbecker 403*a19ff1a2SFrederic Weisbecker if (acct->gtime) 404*a19ff1a2SFrederic Weisbecker account_guest_time(tsk, acct->gtime); 405*a19ff1a2SFrederic Weisbecker 406*a19ff1a2SFrederic Weisbecker if (acct->steal_time) 407*a19ff1a2SFrederic Weisbecker account_steal_time(acct->steal_time); 408*a19ff1a2SFrederic Weisbecker 409*a19ff1a2SFrederic Weisbecker if (acct->idle_time) 410*a19ff1a2SFrederic Weisbecker account_idle_time(acct->idle_time); 411*a19ff1a2SFrederic Weisbecker 412*a19ff1a2SFrederic Weisbecker if (acct->stime) 413*a19ff1a2SFrederic Weisbecker account_system_index_time(tsk, acct->stime, CPUTIME_SYSTEM); 414*a19ff1a2SFrederic Weisbecker 415*a19ff1a2SFrederic Weisbecker if (acct->stime_scaled) 416*a19ff1a2SFrederic Weisbecker tsk->stimescaled += acct->stime_scaled; 417*a19ff1a2SFrederic Weisbecker 418*a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 419*a19ff1a2SFrederic Weisbecker account_system_index_time(tsk, acct->hardirq_time, CPUTIME_IRQ); 420*a19ff1a2SFrederic Weisbecker 421*a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 422*a19ff1a2SFrederic Weisbecker account_system_index_time(tsk, acct->softirq_time, CPUTIME_SOFTIRQ); 423*a19ff1a2SFrederic Weisbecker 4248c8b73c4SFrederic Weisbecker acct->utime = 0; 4258c8b73c4SFrederic Weisbecker acct->utime_scaled = 0; 426c223c903SChristophe Leroy acct->utime_sspurr = 0; 427*a19ff1a2SFrederic Weisbecker acct->gtime = 0; 428*a19ff1a2SFrederic Weisbecker acct->steal_time = 0; 429*a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 430*a19ff1a2SFrederic Weisbecker acct->stime = 0; 431*a19ff1a2SFrederic Weisbecker acct->stime_scaled = 0; 432*a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 433*a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 434c6622f63SPaul Mackerras } 435c6622f63SPaul Mackerras 436c223c903SChristophe Leroy #ifdef CONFIG_PPC32 437c223c903SChristophe Leroy /* 438c223c903SChristophe Leroy * Called from the context switch with interrupts disabled, to charge all 439c223c903SChristophe Leroy * accumulated times to the current process, and to prepare accounting on 440c223c903SChristophe Leroy * the next process. 441c223c903SChristophe Leroy */ 442c223c903SChristophe Leroy void arch_vtime_task_switch(struct task_struct *prev) 443c223c903SChristophe Leroy { 444c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(current); 445c223c903SChristophe Leroy 446c223c903SChristophe Leroy acct->starttime = get_accounting(prev)->starttime; 44790d08ba2SFrederic Weisbecker acct->startspurr = get_accounting(prev)->startspurr; 448c223c903SChristophe Leroy } 449c223c903SChristophe Leroy #endif /* CONFIG_PPC32 */ 450c223c903SChristophe Leroy 451abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 452c6622f63SPaul Mackerras #define calc_cputime_factors() 453c6622f63SPaul Mackerras #endif 454c6622f63SPaul Mackerras 4556defa38bSPaul Mackerras void __delay(unsigned long loops) 4566defa38bSPaul Mackerras { 4576defa38bSPaul Mackerras unsigned long start; 4586defa38bSPaul Mackerras int diff; 4596defa38bSPaul Mackerras 4606defa38bSPaul Mackerras if (__USE_RTC()) { 4616defa38bSPaul Mackerras start = get_rtcl(); 4626defa38bSPaul Mackerras do { 4636defa38bSPaul Mackerras /* the RTCL register wraps at 1000000000 */ 4646defa38bSPaul Mackerras diff = get_rtcl() - start; 4656defa38bSPaul Mackerras if (diff < 0) 4666defa38bSPaul Mackerras diff += 1000000000; 4676defa38bSPaul Mackerras } while (diff < loops); 4686defa38bSPaul Mackerras } else { 4696defa38bSPaul Mackerras start = get_tbl(); 4706defa38bSPaul Mackerras while (get_tbl() - start < loops) 4716defa38bSPaul Mackerras HMT_low(); 4726defa38bSPaul Mackerras HMT_medium(); 4736defa38bSPaul Mackerras } 4746defa38bSPaul Mackerras } 4756defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4766defa38bSPaul Mackerras 4776defa38bSPaul Mackerras void udelay(unsigned long usecs) 4786defa38bSPaul Mackerras { 4796defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4806defa38bSPaul Mackerras } 4816defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4826defa38bSPaul Mackerras 483f2783c15SPaul Mackerras #ifdef CONFIG_SMP 484f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 485f2783c15SPaul Mackerras { 486f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 487f2783c15SPaul Mackerras 488f2783c15SPaul Mackerras if (in_lock_functions(pc)) 489f2783c15SPaul Mackerras return regs->link; 490f2783c15SPaul Mackerras 491f2783c15SPaul Mackerras return pc; 492f2783c15SPaul Mackerras } 493f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 494f2783c15SPaul Mackerras #endif 495f2783c15SPaul Mackerras 496e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 497105988c0SPaul Mackerras 4980fe1ac48SPaul Mackerras /* 4990fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 5000fe1ac48SPaul Mackerras */ 5010fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 502e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void) 503105988c0SPaul Mackerras { 5040fe1ac48SPaul Mackerras unsigned long x; 5050fe1ac48SPaul Mackerras 5060fe1ac48SPaul Mackerras asm volatile("lbz %0,%1(13)" 5070fe1ac48SPaul Mackerras : "=r" (x) 508e360adbeSPeter Zijlstra : "i" (offsetof(struct paca_struct, irq_work_pending))); 5090fe1ac48SPaul Mackerras return x; 510105988c0SPaul Mackerras } 511105988c0SPaul Mackerras 512e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 5130fe1ac48SPaul Mackerras { 5140fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5150fe1ac48SPaul Mackerras "r" (1), 516e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5170fe1ac48SPaul Mackerras } 5180fe1ac48SPaul Mackerras 519e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 5200fe1ac48SPaul Mackerras { 5210fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5220fe1ac48SPaul Mackerras "r" (0), 523e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5240fe1ac48SPaul Mackerras } 5250fe1ac48SPaul Mackerras 5260fe1ac48SPaul Mackerras #else /* 32-bit */ 5270fe1ac48SPaul Mackerras 528e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5290fe1ac48SPaul Mackerras 53069111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 53169111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 53269111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 533105988c0SPaul Mackerras 5340fe1ac48SPaul Mackerras #endif /* 32 vs 64 bit */ 5350fe1ac48SPaul Mackerras 5364f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5370fe1ac48SPaul Mackerras { 5380fe1ac48SPaul Mackerras preempt_disable(); 539e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5400fe1ac48SPaul Mackerras set_dec(1); 5410fe1ac48SPaul Mackerras preempt_enable(); 5420fe1ac48SPaul Mackerras } 5430fe1ac48SPaul Mackerras 544e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 545105988c0SPaul Mackerras 546e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 547e360adbeSPeter Zijlstra #define clear_irq_work_pending() 548105988c0SPaul Mackerras 549e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 550105988c0SPaul Mackerras 551e51df2c1SAnton Blanchard static void __timer_interrupt(void) 552f2783c15SPaul Mackerras { 5531b783955SPreeti U Murthy struct pt_regs *regs = get_irq_regs(); 55469111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 55569111bacSChristoph Lameter struct clock_event_device *evt = this_cpu_ptr(&decrementers); 556860aed25SPaul Mackerras u64 now; 557d831d0b8STony Breeds 558e72bbbabSLi Zhong trace_timer_interrupt_entry(regs); 559e72bbbabSLi Zhong 560e360adbeSPeter Zijlstra if (test_irq_work_pending()) { 561e360adbeSPeter Zijlstra clear_irq_work_pending(); 562e360adbeSPeter Zijlstra irq_work_run(); 5630fe1ac48SPaul Mackerras } 5640fe1ac48SPaul Mackerras 565860aed25SPaul Mackerras now = get_tb_or_rtc(); 566860aed25SPaul Mackerras if (now >= *next_tb) { 5677df10275SAnton Blanchard *next_tb = ~(u64)0; 568d831d0b8STony Breeds if (evt->event_handler) 569d831d0b8STony Breeds evt->event_handler(evt); 57069111bacSChristoph Lameter __this_cpu_inc(irq_stat.timer_irqs_event); 571860aed25SPaul Mackerras } else { 572860aed25SPaul Mackerras now = *next_tb - now; 57379901024SOliver O'Halloran if (now <= decrementer_max) 57479901024SOliver O'Halloran set_dec(now); 5750215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 5760215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 5770215f7d8SBenjamin Herrenschmidt set_dec(1); 57869111bacSChristoph Lameter __this_cpu_inc(irq_stat.timer_irqs_others); 579860aed25SPaul Mackerras } 580f2783c15SPaul Mackerras 581f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 582f2783c15SPaul Mackerras /* collect purr register values often, for accurate calculations */ 583f2783c15SPaul Mackerras if (firmware_has_feature(FW_FEATURE_SPLPAR)) { 58469111bacSChristoph Lameter struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array); 585f2783c15SPaul Mackerras cu->current_tb = mfspr(SPRN_PURR); 586f2783c15SPaul Mackerras } 587f2783c15SPaul Mackerras #endif 588f2783c15SPaul Mackerras 589e72bbbabSLi Zhong trace_timer_interrupt_exit(regs); 5901b783955SPreeti U Murthy } 591e72bbbabSLi Zhong 5921b783955SPreeti U Murthy /* 5931b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 5941b783955SPreeti U Murthy * with interrupts disabled. 5951b783955SPreeti U Murthy */ 5961b783955SPreeti U Murthy void timer_interrupt(struct pt_regs * regs) 5971b783955SPreeti U Murthy { 5981b783955SPreeti U Murthy struct pt_regs *old_regs; 59969111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 6001b783955SPreeti U Murthy 6011b783955SPreeti U Murthy /* Ensure a positive value is written to the decrementer, or else 6021b783955SPreeti U Murthy * some CPUs will continue to take decrementer exceptions. 6031b783955SPreeti U Murthy */ 60479901024SOliver O'Halloran set_dec(decrementer_max); 6051b783955SPreeti U Murthy 6061b783955SPreeti U Murthy /* Some implementations of hotplug will get timer interrupts while 6071b783955SPreeti U Murthy * offline, just ignore these and we also need to set 6081b783955SPreeti U Murthy * decrementers_next_tb as MAX to make sure __check_irq_replay 6091b783955SPreeti U Murthy * don't replay timer interrupt when return, otherwise we'll trap 6101b783955SPreeti U Murthy * here infinitely :( 6111b783955SPreeti U Murthy */ 6121b783955SPreeti U Murthy if (!cpu_online(smp_processor_id())) { 6131b783955SPreeti U Murthy *next_tb = ~(u64)0; 6141b783955SPreeti U Murthy return; 6151b783955SPreeti U Murthy } 6161b783955SPreeti U Murthy 6171b783955SPreeti U Murthy /* Conditionally hard-enable interrupts now that the DEC has been 6181b783955SPreeti U Murthy * bumped to its maximum value 6191b783955SPreeti U Murthy */ 6201b783955SPreeti U Murthy may_hard_irq_enable(); 6211b783955SPreeti U Murthy 6221b783955SPreeti U Murthy 6236e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 6241b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 6251b783955SPreeti U Murthy do_IRQ(regs); 6261b783955SPreeti U Murthy #endif 6271b783955SPreeti U Murthy 6281b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 6291b783955SPreeti U Murthy irq_enter(); 6301b783955SPreeti U Murthy 6311b783955SPreeti U Murthy __timer_interrupt(); 632f2783c15SPaul Mackerras irq_exit(); 6337d12e780SDavid Howells set_irq_regs(old_regs); 634f2783c15SPaul Mackerras } 6359445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 636f2783c15SPaul Mackerras 637dabe859eSPaul Mackerras /* 638dabe859eSPaul Mackerras * Hypervisor decrementer interrupts shouldn't occur but are sometimes 639dabe859eSPaul Mackerras * left pending on exit from a KVM guest. We don't need to do anything 640dabe859eSPaul Mackerras * to clear them, as they are edge-triggered. 641dabe859eSPaul Mackerras */ 642dabe859eSPaul Mackerras void hdec_interrupt(struct pt_regs *regs) 643dabe859eSPaul Mackerras { 644dabe859eSPaul Mackerras } 645dabe859eSPaul Mackerras 6467ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 647d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void) 6487ac5dde9SScott Wood { 6497ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6507ac5dde9SScott Wood * with suspending. 6517ac5dde9SScott Wood */ 6527ac5dde9SScott Wood 65379901024SOliver O'Halloran set_dec(decrementer_max); 6547ac5dde9SScott Wood local_irq_disable(); 65579901024SOliver O'Halloran set_dec(decrementer_max); 6567ac5dde9SScott Wood } 6577ac5dde9SScott Wood 658d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void) 6597ac5dde9SScott Wood { 6607ac5dde9SScott Wood local_irq_enable(); 6617ac5dde9SScott Wood } 6627ac5dde9SScott Wood 6637ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6647ac5dde9SScott Wood void arch_suspend_disable_irqs(void) 6657ac5dde9SScott Wood { 6667ac5dde9SScott Wood if (ppc_md.suspend_disable_irqs) 6677ac5dde9SScott Wood ppc_md.suspend_disable_irqs(); 6687ac5dde9SScott Wood generic_suspend_disable_irqs(); 6697ac5dde9SScott Wood } 6707ac5dde9SScott Wood 6717ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6727ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 6737ac5dde9SScott Wood { 6747ac5dde9SScott Wood generic_suspend_enable_irqs(); 6757ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 6767ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 6777ac5dde9SScott Wood } 6787ac5dde9SScott Wood #endif 6797ac5dde9SScott Wood 680b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 681b6c295dfSPaul Mackerras { 682b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 683b6c295dfSPaul Mackerras } 684b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 685b6c295dfSPaul Mackerras 686f2783c15SPaul Mackerras /* 687f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 688f2783c15SPaul Mackerras * 689f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 690f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 691f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 692f2783c15SPaul Mackerras */ 693f2783c15SPaul Mackerras unsigned long long sched_clock(void) 694f2783c15SPaul Mackerras { 69596c44507SPaul Mackerras if (__USE_RTC()) 69696c44507SPaul Mackerras return get_rtc(); 697fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 698f2783c15SPaul Mackerras } 699f2783c15SPaul Mackerras 7004be1b297SCyril Bur 7014be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 7024be1b297SCyril Bur 7034be1b297SCyril Bur /* 7044be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 7054be1b297SCyril Bur * kernels. 7064be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 7074be1b297SCyril Bur */ 7084be1b297SCyril Bur unsigned long long running_clock(void) 7094be1b297SCyril Bur { 7104be1b297SCyril Bur /* 7114be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 7124be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 7134be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 7144be1b297SCyril Bur * 7154be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7164be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7174be1b297SCyril Bur */ 7184be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7194be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7204be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7214be1b297SCyril Bur 7224be1b297SCyril Bur /* 7234be1b297SCyril Bur * This is a next best approximation without a VTB. 7244be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7254be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7264be1b297SCyril Bur * VTB so it makes no difference anyway. 7274be1b297SCyril Bur */ 7284be1b297SCyril Bur return local_clock() - cputime_to_nsecs(kcpustat_this_cpu->cpustat[CPUTIME_STEAL]); 7294be1b297SCyril Bur } 7304be1b297SCyril Bur #endif 7314be1b297SCyril Bur 7320bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 733f2783c15SPaul Mackerras { 734f2783c15SPaul Mackerras struct device_node *cpu; 7356f7aba7bSAnton Blanchard const __be32 *fp; 7360bb474a4SAnton Blanchard int found = 0; 737f2783c15SPaul Mackerras 7380bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 739f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 740f2783c15SPaul Mackerras 741d8a8188dSOlaf Hering if (cpu) { 742e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 743d8a8188dSOlaf Hering if (fp) { 7440bb474a4SAnton Blanchard found = 1; 745a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 746f2783c15SPaul Mackerras } 7470bb474a4SAnton Blanchard 7480bb474a4SAnton Blanchard of_node_put(cpu); 749f2783c15SPaul Mackerras } 7500bb474a4SAnton Blanchard 7510bb474a4SAnton Blanchard return found; 7520bb474a4SAnton Blanchard } 7530bb474a4SAnton Blanchard 754e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 75577c0a700SBenjamin Herrenschmidt { 75677c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 75777c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 75877c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 75977c0a700SBenjamin Herrenschmidt 76077c0a700SBenjamin Herrenschmidt /* Enable decrementer interrupt */ 76177c0a700SBenjamin Herrenschmidt mtspr(SPRN_TCR, TCR_DIE); 76277c0a700SBenjamin Herrenschmidt #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */ 76377c0a700SBenjamin Herrenschmidt } 76477c0a700SBenjamin Herrenschmidt 7650bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 7660bb474a4SAnton Blanchard { 7670bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 7680bb474a4SAnton Blanchard 7690bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 7700bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 7710bb474a4SAnton Blanchard 772f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 773f2783c15SPaul Mackerras "(not found)\n"); 7740bb474a4SAnton Blanchard } 775f2783c15SPaul Mackerras 7760bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 7770bb474a4SAnton Blanchard 7780bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 7790bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 7800bb474a4SAnton Blanchard 7810bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 7820bb474a4SAnton Blanchard "(not found)\n"); 783f2783c15SPaul Mackerras } 784f2783c15SPaul Mackerras } 785f2783c15SPaul Mackerras 786aa3be5f3STony Breeds int update_persistent_clock(struct timespec now) 787f2783c15SPaul Mackerras { 788f2783c15SPaul Mackerras struct rtc_time tm; 789f2783c15SPaul Mackerras 790aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 791023f333aSJason Gunthorpe return -ENODEV; 792aa3be5f3STony Breeds 793aa3be5f3STony Breeds to_tm(now.tv_sec + 1 + timezone_offset, &tm); 794aa3be5f3STony Breeds tm.tm_year -= 1900; 795aa3be5f3STony Breeds tm.tm_mon -= 1; 796aa3be5f3STony Breeds 797aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 798aa3be5f3STony Breeds } 799aa3be5f3STony Breeds 800978d7eb3SBenjamin Herrenschmidt static void __read_persistent_clock(struct timespec *ts) 801aa3be5f3STony Breeds { 802aa3be5f3STony Breeds struct rtc_time tm; 803aa3be5f3STony Breeds static int first = 1; 804aa3be5f3STony Breeds 805d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 806aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 807aa3be5f3STony Breeds if (first) { 808aa3be5f3STony Breeds first = 0; 809aa3be5f3STony Breeds if (ppc_md.time_init) 810aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 811aa3be5f3STony Breeds 812aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 813d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 814d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 815d90246cdSMartin Schwidefsky return; 816aa3be5f3STony Breeds } 817d90246cdSMartin Schwidefsky } 818d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 819d90246cdSMartin Schwidefsky ts->tv_sec = 0; 820d90246cdSMartin Schwidefsky return; 821d90246cdSMartin Schwidefsky } 822f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 823978d7eb3SBenjamin Herrenschmidt 824d4f587c6SMartin Schwidefsky ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, 825f2783c15SPaul Mackerras tm.tm_hour, tm.tm_min, tm.tm_sec); 826f2783c15SPaul Mackerras } 827f2783c15SPaul Mackerras 828978d7eb3SBenjamin Herrenschmidt void read_persistent_clock(struct timespec *ts) 829978d7eb3SBenjamin Herrenschmidt { 830978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 831978d7eb3SBenjamin Herrenschmidt 832978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 833978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 834978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 835978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 836978d7eb3SBenjamin Herrenschmidt } 837978d7eb3SBenjamin Herrenschmidt 838978d7eb3SBenjamin Herrenschmidt } 839978d7eb3SBenjamin Herrenschmidt 8404a4cfe38STony Breeds /* clocksource code */ 841a5a1d1c2SThomas Gleixner static u64 rtc_read(struct clocksource *cs) 8424a4cfe38STony Breeds { 843a5a1d1c2SThomas Gleixner return (u64)get_rtc(); 8444a4cfe38STony Breeds } 8454a4cfe38STony Breeds 846a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *cs) 8474a4cfe38STony Breeds { 848a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8494a4cfe38STony Breeds } 8504a4cfe38STony Breeds 85170639421SJohn Stultz void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm, 852a5a1d1c2SThomas Gleixner struct clocksource *clock, u32 mult, u64 cycle_last) 8534a4cfe38STony Breeds { 854b0797b60SJohn Stultz u64 new_tb_to_xs, new_stamp_xsec; 8550e469db8SPaul Mackerras u32 frac_sec; 8564a4cfe38STony Breeds 8574a4cfe38STony Breeds if (clock != &clocksource_timebase) 8584a4cfe38STony Breeds return; 8594a4cfe38STony Breeds 8604a4cfe38STony Breeds /* Make userspace gettimeofday spin until we're done. */ 8614a4cfe38STony Breeds ++vdso_data->tb_update_count; 8624a4cfe38STony Breeds smp_mb(); 8634a4cfe38STony Breeds 86411b8633aSAnton Blanchard /* 19342813113834067 ~= 2^(20+64) / 1e9 */ 86511b8633aSAnton Blanchard new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift); 86606d518e3SJohn Stultz new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC; 867b0797b60SJohn Stultz do_div(new_stamp_xsec, 1000000000); 86806d518e3SJohn Stultz new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC; 869b0797b60SJohn Stultz 8700e469db8SPaul Mackerras BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC); 8710e469db8SPaul Mackerras /* this is tv_nsec / 1e9 as a 0.32 fraction */ 8720e469db8SPaul Mackerras frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32; 87347916be4SThomas Gleixner 874b0797b60SJohn Stultz /* 875b0797b60SJohn Stultz * tb_update_count is used to allow the userspace gettimeofday code 876b0797b60SJohn Stultz * to assure itself that it sees a consistent view of the tb_to_xs and 877b0797b60SJohn Stultz * stamp_xsec variables. It reads the tb_update_count, then reads 878b0797b60SJohn Stultz * tb_to_xs and stamp_xsec and then reads tb_update_count again. If 879b0797b60SJohn Stultz * the two values of tb_update_count match and are even then the 880b0797b60SJohn Stultz * tb_to_xs and stamp_xsec values are consistent. If not, then it 881b0797b60SJohn Stultz * loops back and reads them again until this criteria is met. 882b0797b60SJohn Stultz * We expect the caller to have done the first increment of 883b0797b60SJohn Stultz * vdso_data->tb_update_count already. 884b0797b60SJohn Stultz */ 8854a0e6377SThomas Gleixner vdso_data->tb_orig_stamp = cycle_last; 886b0797b60SJohn Stultz vdso_data->stamp_xsec = new_stamp_xsec; 887b0797b60SJohn Stultz vdso_data->tb_to_xs = new_tb_to_xs; 8887615856eSJohn Stultz vdso_data->wtom_clock_sec = wtm->tv_sec; 8897615856eSJohn Stultz vdso_data->wtom_clock_nsec = wtm->tv_nsec; 89006d518e3SJohn Stultz vdso_data->stamp_xtime = *wall_time; 89147916be4SThomas Gleixner vdso_data->stamp_sec_fraction = frac_sec; 892b0797b60SJohn Stultz smp_wmb(); 893b0797b60SJohn Stultz ++(vdso_data->tb_update_count); 8944a4cfe38STony Breeds } 8954a4cfe38STony Breeds 8964a4cfe38STony Breeds void update_vsyscall_tz(void) 8974a4cfe38STony Breeds { 8984a4cfe38STony Breeds vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; 8994a4cfe38STony Breeds vdso_data->tz_dsttime = sys_tz.tz_dsttime; 9004a4cfe38STony Breeds } 9014a4cfe38STony Breeds 9021c21a293SMichael Ellerman static void __init clocksource_init(void) 9034a4cfe38STony Breeds { 9044a4cfe38STony Breeds struct clocksource *clock; 9054a4cfe38STony Breeds 9064a4cfe38STony Breeds if (__USE_RTC()) 9074a4cfe38STony Breeds clock = &clocksource_rtc; 9084a4cfe38STony Breeds else 9094a4cfe38STony Breeds clock = &clocksource_timebase; 9104a4cfe38STony Breeds 91111b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 9124a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 9134a4cfe38STony Breeds clock->name); 9144a4cfe38STony Breeds return; 9154a4cfe38STony Breeds } 9164a4cfe38STony Breeds 9174a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 9184a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 9194a4cfe38STony Breeds } 9204a4cfe38STony Breeds 921d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 922d831d0b8STony Breeds struct clock_event_device *dev) 923d831d0b8STony Breeds { 92469111bacSChristoph Lameter __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt); 925d831d0b8STony Breeds set_dec(evt); 9260215f7d8SBenjamin Herrenschmidt 9270215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 9280215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 9290215f7d8SBenjamin Herrenschmidt set_dec(1); 9300215f7d8SBenjamin Herrenschmidt 931d831d0b8STony Breeds return 0; 932d831d0b8STony Breeds } 933d831d0b8STony Breeds 93437a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 935d831d0b8STony Breeds { 93679901024SOliver O'Halloran decrementer_set_next_event(decrementer_max, dev); 93737a13e78SViresh Kumar return 0; 938d831d0b8STony Breeds } 939d831d0b8STony Breeds 9401b67bee1SSrivatsa S. Bhat /* Interrupt handler for the timer broadcast IPI */ 9411b67bee1SSrivatsa S. Bhat void tick_broadcast_ipi_handler(void) 9421b67bee1SSrivatsa S. Bhat { 94369111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 9441b783955SPreeti U Murthy 9451b783955SPreeti U Murthy *next_tb = get_tb_or_rtc(); 9461b783955SPreeti U Murthy __timer_interrupt(); 9471b67bee1SSrivatsa S. Bhat } 9481b67bee1SSrivatsa S. Bhat 949d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 950d831d0b8STony Breeds { 9517df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 952d831d0b8STony Breeds 953d831d0b8STony Breeds *dec = decrementer_clockevent; 954320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 955d831d0b8STony Breeds 956b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 957d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 958d831d0b8STony Breeds 959d831d0b8STony Breeds clockevents_register_device(dec); 960d831d0b8STony Breeds } 961d831d0b8STony Breeds 96279901024SOliver O'Halloran static void enable_large_decrementer(void) 96379901024SOliver O'Halloran { 96479901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 96579901024SOliver O'Halloran return; 96679901024SOliver O'Halloran 96779901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 96879901024SOliver O'Halloran return; 96979901024SOliver O'Halloran 97079901024SOliver O'Halloran /* 97179901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 97279901024SOliver O'Halloran * otherwise firmware should have done it for us. 97379901024SOliver O'Halloran */ 97479901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 97579901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 97679901024SOliver O'Halloran } 97779901024SOliver O'Halloran 97879901024SOliver O'Halloran static void __init set_decrementer_max(void) 97979901024SOliver O'Halloran { 98079901024SOliver O'Halloran struct device_node *cpu; 98179901024SOliver O'Halloran u32 bits = 32; 98279901024SOliver O'Halloran 98379901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 98479901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 98579901024SOliver O'Halloran return; 98679901024SOliver O'Halloran 98779901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 98879901024SOliver O'Halloran 98979901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 99079901024SOliver O'Halloran if (bits > 64 || bits < 32) { 99179901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 99279901024SOliver O'Halloran bits = 32; 99379901024SOliver O'Halloran } 99479901024SOliver O'Halloran 99579901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 99679901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 99779901024SOliver O'Halloran } 99879901024SOliver O'Halloran 99979901024SOliver O'Halloran of_node_put(cpu); 100079901024SOliver O'Halloran 100179901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 100279901024SOliver O'Halloran bits, decrementer_max); 100379901024SOliver O'Halloran } 100479901024SOliver O'Halloran 1005c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 1006d831d0b8STony Breeds { 1007d831d0b8STony Breeds int cpu = smp_processor_id(); 1008d831d0b8STony Breeds 1009d8afc6fdSAnton Blanchard clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4); 1010d8afc6fdSAnton Blanchard 1011d831d0b8STony Breeds decrementer_clockevent.max_delta_ns = 101279901024SOliver O'Halloran clockevent_delta2ns(decrementer_max, &decrementer_clockevent); 101343875cc0SPaul Mackerras decrementer_clockevent.min_delta_ns = 101443875cc0SPaul Mackerras clockevent_delta2ns(2, &decrementer_clockevent); 1015d831d0b8STony Breeds 1016d831d0b8STony Breeds register_decrementer_clockevent(cpu); 1017d831d0b8STony Breeds } 1018d831d0b8STony Breeds 1019d831d0b8STony Breeds void secondary_cpu_time_init(void) 1020d831d0b8STony Breeds { 102179901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 102279901024SOliver O'Halloran enable_large_decrementer(); 102379901024SOliver O'Halloran 102477c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 102577c0a700SBenjamin Herrenschmidt * such as BookE 102677c0a700SBenjamin Herrenschmidt */ 102777c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 102877c0a700SBenjamin Herrenschmidt 1029d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 1030d831d0b8STony Breeds * call here ! */ 1031d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 1032d831d0b8STony Breeds } 1033d831d0b8STony Breeds 1034f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 1035f2783c15SPaul Mackerras void __init time_init(void) 1036f2783c15SPaul Mackerras { 1037f2783c15SPaul Mackerras struct div_result res; 1038d75d68cfSPaul Mackerras u64 scale; 1039f2783c15SPaul Mackerras unsigned shift; 1040f2783c15SPaul Mackerras 104196c44507SPaul Mackerras if (__USE_RTC()) { 104296c44507SPaul Mackerras /* 601 processor: dec counts down by 128 every 128ns */ 104396c44507SPaul Mackerras ppc_tb_freq = 1000000000; 104496c44507SPaul Mackerras } else { 104596c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 1046f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 1047224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 1048374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 1049224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 1050374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 105196c44507SPaul Mackerras } 1052374e99d4SPaul Mackerras 1053374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 1054092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 1055374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 1056c6622f63SPaul Mackerras calc_cputime_factors(); 1057a42548a1SStanislaw Gruszka setup_cputime_one_jiffy(); 1058092b8f34SPaul Mackerras 1059092b8f34SPaul Mackerras /* 1060f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 1061f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 1062f2783c15SPaul Mackerras * which is the timebase frequency. 1063f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 1064f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 1065f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 1066f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 1067f2783c15SPaul Mackerras * sched_clock(). 1068f2783c15SPaul Mackerras */ 1069f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1070f2783c15SPaul Mackerras scale = res.result_low; 1071f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1072f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1073f2783c15SPaul Mackerras res.result_high >>= 1; 1074f2783c15SPaul Mackerras } 1075f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1076f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1077fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 1078c27da339SBenjamin Herrenschmidt boot_tb = get_tb_or_rtc(); 1079f2783c15SPaul Mackerras 1080092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1081092b8f34SPaul Mackerras if (timezone_offset) { 1082092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1083092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1084092b8f34SPaul Mackerras } 1085092b8f34SPaul Mackerras 1086a7f290daSBenjamin Herrenschmidt vdso_data->tb_update_count = 0; 1087a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1088f2783c15SPaul Mackerras 108979901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 109079901024SOliver O'Halloran set_decrementer_max(); 109179901024SOliver O'Halloran enable_large_decrementer(); 109279901024SOliver O'Halloran 109377c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 109477c0a700SBenjamin Herrenschmidt * such as BookE 109577c0a700SBenjamin Herrenschmidt */ 109677c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 109777c0a700SBenjamin Herrenschmidt 1098f5339277SStephen Rothwell /* Register the clocksource */ 10994a4cfe38STony Breeds clocksource_init(); 11004a4cfe38STony Breeds 1101d831d0b8STony Breeds init_decrementer_clockevent(); 11020d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1103f0d37300SKevin Hao 1104f0d37300SKevin Hao #ifdef CONFIG_COMMON_CLK 1105f0d37300SKevin Hao of_clk_init(NULL); 1106f0d37300SKevin Hao #endif 1107f2783c15SPaul Mackerras } 1108f2783c15SPaul Mackerras 1109f2783c15SPaul Mackerras 1110f2783c15SPaul Mackerras #define FEBRUARY 2 1111f2783c15SPaul Mackerras #define STARTOFTIME 1970 1112f2783c15SPaul Mackerras #define SECDAY 86400L 1113f2783c15SPaul Mackerras #define SECYR (SECDAY * 365) 1114f2783c15SPaul Mackerras #define leapyear(year) ((year) % 4 == 0 && \ 1115f2783c15SPaul Mackerras ((year) % 100 != 0 || (year) % 400 == 0)) 1116f2783c15SPaul Mackerras #define days_in_year(a) (leapyear(a) ? 366 : 365) 1117f2783c15SPaul Mackerras #define days_in_month(a) (month_days[(a) - 1]) 1118f2783c15SPaul Mackerras 1119f2783c15SPaul Mackerras static int month_days[12] = { 1120f2783c15SPaul Mackerras 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 1121f2783c15SPaul Mackerras }; 1122f2783c15SPaul Mackerras 1123f2783c15SPaul Mackerras void to_tm(int tim, struct rtc_time * tm) 1124f2783c15SPaul Mackerras { 1125f2783c15SPaul Mackerras register int i; 1126f2783c15SPaul Mackerras register long hms, day; 1127f2783c15SPaul Mackerras 1128f2783c15SPaul Mackerras day = tim / SECDAY; 1129f2783c15SPaul Mackerras hms = tim % SECDAY; 1130f2783c15SPaul Mackerras 1131f2783c15SPaul Mackerras /* Hours, minutes, seconds are easy */ 1132f2783c15SPaul Mackerras tm->tm_hour = hms / 3600; 1133f2783c15SPaul Mackerras tm->tm_min = (hms % 3600) / 60; 1134f2783c15SPaul Mackerras tm->tm_sec = (hms % 3600) % 60; 1135f2783c15SPaul Mackerras 1136f2783c15SPaul Mackerras /* Number of years in days */ 1137f2783c15SPaul Mackerras for (i = STARTOFTIME; day >= days_in_year(i); i++) 1138f2783c15SPaul Mackerras day -= days_in_year(i); 1139f2783c15SPaul Mackerras tm->tm_year = i; 1140f2783c15SPaul Mackerras 1141f2783c15SPaul Mackerras /* Number of months in days left */ 1142f2783c15SPaul Mackerras if (leapyear(tm->tm_year)) 1143f2783c15SPaul Mackerras days_in_month(FEBRUARY) = 29; 1144f2783c15SPaul Mackerras for (i = 1; day >= days_in_month(i); i++) 1145f2783c15SPaul Mackerras day -= days_in_month(i); 1146f2783c15SPaul Mackerras days_in_month(FEBRUARY) = 28; 1147f2783c15SPaul Mackerras tm->tm_mon = i; 1148f2783c15SPaul Mackerras 1149f2783c15SPaul Mackerras /* Days are what is left over (+1) from all that. */ 1150f2783c15SPaul Mackerras tm->tm_mday = day + 1; 1151f2783c15SPaul Mackerras 1152f2783c15SPaul Mackerras /* 115300b912b0SDaniel Axtens * No-one uses the day of the week. 1154f2783c15SPaul Mackerras */ 115500b912b0SDaniel Axtens tm->tm_wday = -1; 1156f2783c15SPaul Mackerras } 1157e1802b06SAnton Blanchard EXPORT_SYMBOL(to_tm); 1158f2783c15SPaul Mackerras 1159f2783c15SPaul Mackerras /* 1160f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1161f2783c15SPaul Mackerras * result. 1162f2783c15SPaul Mackerras */ 1163f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1164f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1165f2783c15SPaul Mackerras { 1166f2783c15SPaul Mackerras unsigned long a, b, c, d; 1167f2783c15SPaul Mackerras unsigned long w, x, y, z; 1168f2783c15SPaul Mackerras u64 ra, rb, rc; 1169f2783c15SPaul Mackerras 1170f2783c15SPaul Mackerras a = dividend_high >> 32; 1171f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1172f2783c15SPaul Mackerras c = dividend_low >> 32; 1173f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1174f2783c15SPaul Mackerras 1175f2783c15SPaul Mackerras w = a / divisor; 1176f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1177f2783c15SPaul Mackerras 1178f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1179f2783c15SPaul Mackerras x = ra; 1180f2783c15SPaul Mackerras 1181f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1182f2783c15SPaul Mackerras y = rb; 1183f2783c15SPaul Mackerras 1184f2783c15SPaul Mackerras do_div(rc, divisor); 1185f2783c15SPaul Mackerras z = rc; 1186f2783c15SPaul Mackerras 1187f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1188f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1189f2783c15SPaul Mackerras 1190f2783c15SPaul Mackerras } 1191bcd68a70SGeert Uytterhoeven 1192177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1193177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1194177996e6SBenjamin Herrenschmidt { 1195177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1196177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1197177996e6SBenjamin Herrenschmidt */ 1198177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1199177996e6SBenjamin Herrenschmidt } 1200177996e6SBenjamin Herrenschmidt 1201169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1202169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1203169047f4SArnd Bergmann { 1204169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1205169047f4SArnd Bergmann return rtc_valid_tm(tm); 1206169047f4SArnd Bergmann } 1207169047f4SArnd Bergmann 1208169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1209169047f4SArnd Bergmann { 1210169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1211169047f4SArnd Bergmann return -EOPNOTSUPP; 1212169047f4SArnd Bergmann 1213169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1214169047f4SArnd Bergmann return -EOPNOTSUPP; 1215169047f4SArnd Bergmann 1216169047f4SArnd Bergmann return 0; 1217169047f4SArnd Bergmann } 1218169047f4SArnd Bergmann 1219169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1220169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1221169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1222169047f4SArnd Bergmann }; 1223169047f4SArnd Bergmann 1224bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1225bcd68a70SGeert Uytterhoeven { 1226bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1227bcd68a70SGeert Uytterhoeven 1228bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1229bcd68a70SGeert Uytterhoeven return -ENODEV; 1230bcd68a70SGeert Uytterhoeven 1231169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1232169047f4SArnd Bergmann &rtc_generic_ops, 1233169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1234bcd68a70SGeert Uytterhoeven 12358c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1236bcd68a70SGeert Uytterhoeven } 1237bcd68a70SGeert Uytterhoeven 12388f6b9512SPaul Gortmaker device_initcall(rtc_init); 1239169047f4SArnd Bergmann #endif 1240