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> 5360083063SGeert Uytterhoeven #include <linux/of_clk.h> 547f92bc56SDaniel Axtens #include <linux/suspend.h> 5532ef5517SIngo Molnar #include <linux/sched/cputime.h> 56*b709e32eSPingfan Liu #include <linux/sched/clock.h> 574e287e65SNicholas Piggin #include <linux/processor.h> 586795b85cSAnton Blanchard #include <asm/trace.h> 59f2783c15SPaul Mackerras 60f2783c15SPaul Mackerras #include <asm/io.h> 61f2783c15SPaul Mackerras #include <asm/nvram.h> 62f2783c15SPaul Mackerras #include <asm/cache.h> 63f2783c15SPaul Mackerras #include <asm/machdep.h> 647c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 65f2783c15SPaul Mackerras #include <asm/time.h> 66f2783c15SPaul Mackerras #include <asm/prom.h> 67f2783c15SPaul Mackerras #include <asm/irq.h> 68f2783c15SPaul Mackerras #include <asm/div64.h> 692249ca9dSPaul Mackerras #include <asm/smp.h> 70a7f290daSBenjamin Herrenschmidt #include <asm/vdso_datapage.h> 71f2783c15SPaul Mackerras #include <asm/firmware.h> 720545d543SDaniel Axtens #include <asm/asm-prototypes.h> 73f2783c15SPaul Mackerras 744a4cfe38STony Breeds /* powerpc clocksource/clockevent code */ 754a4cfe38STony Breeds 76d831d0b8STony Breeds #include <linux/clockchips.h> 77189374aeSJohn Stultz #include <linux/timekeeper_internal.h> 784a4cfe38STony Breeds 79a5a1d1c2SThomas Gleixner static u64 timebase_read(struct clocksource *); 804a4cfe38STony Breeds static struct clocksource clocksource_timebase = { 814a4cfe38STony Breeds .name = "timebase", 824a4cfe38STony Breeds .rating = 400, 834a4cfe38STony Breeds .flags = CLOCK_SOURCE_IS_CONTINUOUS, 844a4cfe38STony Breeds .mask = CLOCKSOURCE_MASK(64), 854a4cfe38STony Breeds .read = timebase_read, 86ab037dd8SChristophe Leroy .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER, 874a4cfe38STony Breeds }; 884a4cfe38STony Breeds 8979901024SOliver O'Halloran #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF 9079901024SOliver O'Halloran u64 decrementer_max = DECREMENTER_DEFAULT_MAX; 91d831d0b8STony Breeds 92d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 93d831d0b8STony Breeds struct clock_event_device *dev); 9437a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *evt); 95d831d0b8STony Breeds 966e35994dSBharat Bhushan struct clock_event_device decrementer_clockevent = { 97d831d0b8STony Breeds .name = "decrementer", 98d831d0b8STony Breeds .rating = 200, 99d831d0b8STony Breeds .irq = 0, 100d831d0b8STony Breeds .set_next_event = decrementer_set_next_event, 10181759360SAnton Blanchard .set_state_oneshot_stopped = decrementer_shutdown, 10237a13e78SViresh Kumar .set_state_shutdown = decrementer_shutdown, 10337a13e78SViresh Kumar .tick_resume = decrementer_shutdown, 10437a13e78SViresh Kumar .features = CLOCK_EVT_FEAT_ONESHOT | 10537a13e78SViresh Kumar CLOCK_EVT_FEAT_C3STOP, 106d831d0b8STony Breeds }; 1076e35994dSBharat Bhushan EXPORT_SYMBOL(decrementer_clockevent); 108d831d0b8STony Breeds 1097df10275SAnton Blanchard DEFINE_PER_CPU(u64, decrementers_next_tb); 1107df10275SAnton Blanchard static DEFINE_PER_CPU(struct clock_event_device, decrementers); 111d831d0b8STony Breeds 112f2783c15SPaul Mackerras #define XSEC_PER_SEC (1024*1024) 113f2783c15SPaul Mackerras 114f2783c15SPaul Mackerras #ifdef CONFIG_PPC64 115f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) 116f2783c15SPaul Mackerras #else 117f2783c15SPaul Mackerras /* compute ((xsec << 12) * max) >> 32 */ 118f2783c15SPaul Mackerras #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) 119f2783c15SPaul Mackerras #endif 120f2783c15SPaul Mackerras 121f2783c15SPaul Mackerras unsigned long tb_ticks_per_jiffy; 122f2783c15SPaul Mackerras unsigned long tb_ticks_per_usec = 100; /* sane default */ 123f2783c15SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_usec); 124f2783c15SPaul Mackerras unsigned long tb_ticks_per_sec; 1252cf82c02SPaul Mackerras EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ 126092b8f34SPaul Mackerras 127f2783c15SPaul Mackerras DEFINE_SPINLOCK(rtc_lock); 128f2783c15SPaul Mackerras EXPORT_SYMBOL_GPL(rtc_lock); 129f2783c15SPaul Mackerras 130fc9069feSTony Breeds static u64 tb_to_ns_scale __read_mostly; 131fc9069feSTony Breeds static unsigned tb_to_ns_shift __read_mostly; 132364a1246SHeiko Schocher static u64 boot_tb __read_mostly; 133f2783c15SPaul Mackerras 134f2783c15SPaul Mackerras extern struct timezone sys_tz; 135f2783c15SPaul Mackerras static long timezone_offset; 136f2783c15SPaul Mackerras 137f2783c15SPaul Mackerras unsigned long ppc_proc_freq; 13855ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_proc_freq); 139f2783c15SPaul Mackerras unsigned long ppc_tb_freq; 14055ec2fcaSTimur Tabi EXPORT_SYMBOL_GPL(ppc_tb_freq); 14196c44507SPaul Mackerras 142de269129SMahesh Salgaonkar bool tb_invalid; 143de269129SMahesh Salgaonkar 144abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 145c6622f63SPaul Mackerras /* 146e7f340caSFrederic Weisbecker * Factor for converting from cputime_t (timebase ticks) to 147e7f340caSFrederic Weisbecker * microseconds. This is stored as 0.64 fixed-point binary fraction. 148c6622f63SPaul Mackerras */ 1499f5072d4SAndreas Schwab u64 __cputime_usec_factor; 1509f5072d4SAndreas Schwab EXPORT_SYMBOL(__cputime_usec_factor); 151a42548a1SStanislaw Gruszka 152c223c903SChristophe Leroy #ifdef CONFIG_PPC_SPLPAR 153872e439aSPaul Mackerras void (*dtl_consumer)(struct dtl_entry *, u64); 154c223c903SChristophe Leroy #endif 155c223c903SChristophe Leroy 156c6622f63SPaul Mackerras static void calc_cputime_factors(void) 157c6622f63SPaul Mackerras { 158c6622f63SPaul Mackerras struct div_result res; 159c6622f63SPaul Mackerras 1609f5072d4SAndreas Schwab div128_by_32(1000000, 0, tb_ticks_per_sec, &res); 1619f5072d4SAndreas Schwab __cputime_usec_factor = res.result_low; 162c6622f63SPaul Mackerras } 163c6622f63SPaul Mackerras 164c6622f63SPaul Mackerras /* 165cf9efce0SPaul Mackerras * Read the SPURR on systems that have it, otherwise the PURR, 166cf9efce0SPaul Mackerras * or if that doesn't exist return the timebase value passed in. 167c6622f63SPaul Mackerras */ 168abcff86dSChristophe Leroy static inline unsigned long read_spurr(unsigned long tb) 169c6622f63SPaul Mackerras { 170cf9efce0SPaul Mackerras if (cpu_has_feature(CPU_FTR_SPURR)) 171cf9efce0SPaul Mackerras return mfspr(SPRN_SPURR); 172c6622f63SPaul Mackerras if (cpu_has_feature(CPU_FTR_PURR)) 173c6622f63SPaul Mackerras return mfspr(SPRN_PURR); 174cf9efce0SPaul Mackerras return tb; 175cf9efce0SPaul Mackerras } 176cf9efce0SPaul Mackerras 177cf9efce0SPaul Mackerras #ifdef CONFIG_PPC_SPLPAR 178cf9efce0SPaul Mackerras 179d6bdceb6SPeter Zijlstra #include <asm/dtl.h> 180d6bdceb6SPeter Zijlstra 181cf9efce0SPaul Mackerras /* 182cf9efce0SPaul Mackerras * Scan the dispatch trace log and count up the stolen time. 183cf9efce0SPaul Mackerras * Should be called with interrupts disabled. 184cf9efce0SPaul Mackerras */ 185cf9efce0SPaul Mackerras static u64 scan_dispatch_log(u64 stop_tb) 186cf9efce0SPaul Mackerras { 187872e439aSPaul Mackerras u64 i = local_paca->dtl_ridx; 188cf9efce0SPaul Mackerras struct dtl_entry *dtl = local_paca->dtl_curr; 189cf9efce0SPaul Mackerras struct dtl_entry *dtl_end = local_paca->dispatch_log_end; 190cf9efce0SPaul Mackerras struct lppaca *vpa = local_paca->lppaca_ptr; 191cf9efce0SPaul Mackerras u64 tb_delta; 192cf9efce0SPaul Mackerras u64 stolen = 0; 193cf9efce0SPaul Mackerras u64 dtb; 194cf9efce0SPaul Mackerras 19584ffae55SAnton Blanchard if (!dtl) 19684ffae55SAnton Blanchard return 0; 19784ffae55SAnton Blanchard 1987ffcf8ecSAnton Blanchard if (i == be64_to_cpu(vpa->dtl_idx)) 199cf9efce0SPaul Mackerras return 0; 2007ffcf8ecSAnton Blanchard while (i < be64_to_cpu(vpa->dtl_idx)) { 2017ffcf8ecSAnton Blanchard dtb = be64_to_cpu(dtl->timebase); 2027ffcf8ecSAnton Blanchard tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) + 2037ffcf8ecSAnton Blanchard be32_to_cpu(dtl->ready_to_enqueue_time); 204cf9efce0SPaul Mackerras barrier(); 2057ffcf8ecSAnton Blanchard if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) { 206cf9efce0SPaul Mackerras /* buffer has overflowed */ 2077ffcf8ecSAnton Blanchard i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG; 208cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); 209cf9efce0SPaul Mackerras continue; 210cf9efce0SPaul Mackerras } 211cf9efce0SPaul Mackerras if (dtb > stop_tb) 212cf9efce0SPaul Mackerras break; 21384b07386SAnton Blanchard if (dtl_consumer) 21484b07386SAnton Blanchard dtl_consumer(dtl, i); 215cf9efce0SPaul Mackerras stolen += tb_delta; 216cf9efce0SPaul Mackerras ++i; 217cf9efce0SPaul Mackerras ++dtl; 218cf9efce0SPaul Mackerras if (dtl == dtl_end) 219cf9efce0SPaul Mackerras dtl = local_paca->dispatch_log; 220cf9efce0SPaul Mackerras } 221cf9efce0SPaul Mackerras local_paca->dtl_ridx = i; 222cf9efce0SPaul Mackerras local_paca->dtl_curr = dtl; 223cf9efce0SPaul Mackerras return stolen; 224c6622f63SPaul Mackerras } 225c6622f63SPaul Mackerras 226c6622f63SPaul Mackerras /* 227cf9efce0SPaul Mackerras * Accumulate stolen time by scanning the dispatch trace log. 228cf9efce0SPaul Mackerras * Called on entry from user mode. 2294603ac18SMichael Neuling */ 230eb8e20f8SMichael Ellerman void notrace accumulate_stolen_time(void) 2314603ac18SMichael Neuling { 232cf9efce0SPaul Mackerras u64 sst, ust; 2334e26bc4aSMadhavan Srinivasan unsigned long save_irq_soft_mask = irq_soft_mask_return(); 234c223c903SChristophe Leroy struct cpu_accounting_data *acct = &local_paca->accounting; 235b18ae08dSTejun Heo 236b18ae08dSTejun Heo /* We are called early in the exception entry, before 237b18ae08dSTejun Heo * soft/hard_enabled are sync'ed to the expected state 238b18ae08dSTejun Heo * for the exception. We are hard disabled but the PACA 239b18ae08dSTejun Heo * needs to reflect that so various debug stuff doesn't 240b18ae08dSTejun Heo * complain 241b18ae08dSTejun Heo */ 2424e26bc4aSMadhavan Srinivasan irq_soft_mask_set(IRQS_DISABLED); 243b18ae08dSTejun Heo 244c223c903SChristophe Leroy sst = scan_dispatch_log(acct->starttime_user); 245c223c903SChristophe Leroy ust = scan_dispatch_log(acct->starttime); 2468c8b73c4SFrederic Weisbecker acct->stime -= sst; 2478c8b73c4SFrederic Weisbecker acct->utime -= ust; 248f828c3d0SFrederic Weisbecker acct->steal_time += ust + sst; 249b18ae08dSTejun Heo 2504e26bc4aSMadhavan Srinivasan irq_soft_mask_set(save_irq_soft_mask); 2514603ac18SMichael Neuling } 2524603ac18SMichael Neuling 253cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 254cf9efce0SPaul Mackerras { 255a6201da3SAneesh Kumar K.V if (!firmware_has_feature(FW_FEATURE_SPLPAR)) 256a6201da3SAneesh Kumar K.V return 0; 257a6201da3SAneesh Kumar K.V 258a19ff1a2SFrederic Weisbecker if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx)) 259a19ff1a2SFrederic Weisbecker return scan_dispatch_log(stop_tb); 260cf9efce0SPaul Mackerras 261a19ff1a2SFrederic Weisbecker return 0; 262cf9efce0SPaul Mackerras } 263cf9efce0SPaul Mackerras 264cf9efce0SPaul Mackerras #else /* CONFIG_PPC_SPLPAR */ 265cf9efce0SPaul Mackerras static inline u64 calculate_stolen_time(u64 stop_tb) 266cf9efce0SPaul Mackerras { 267cf9efce0SPaul Mackerras return 0; 268cf9efce0SPaul Mackerras } 269cf9efce0SPaul Mackerras 270cf9efce0SPaul Mackerras #endif /* CONFIG_PPC_SPLPAR */ 271cf9efce0SPaul Mackerras 2724603ac18SMichael Neuling /* 273c6622f63SPaul Mackerras * Account time for a transition between system, hard irq 274c6622f63SPaul Mackerras * or soft irq state. 275c6622f63SPaul Mackerras */ 276b38a181cSChristophe Leroy static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct, 277b38a181cSChristophe Leroy unsigned long now, unsigned long stime) 278c6622f63SPaul Mackerras { 279abcff86dSChristophe Leroy unsigned long stime_scaled = 0; 280abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 281b38a181cSChristophe Leroy unsigned long nowscaled, deltascaled; 282a19ff1a2SFrederic Weisbecker unsigned long utime, utime_scaled; 283c6622f63SPaul Mackerras 2844603ac18SMichael Neuling nowscaled = read_spurr(now); 285c223c903SChristophe Leroy deltascaled = nowscaled - acct->startspurr; 286c223c903SChristophe Leroy acct->startspurr = nowscaled; 287a19ff1a2SFrederic Weisbecker utime = acct->utime - acct->utime_sspurr; 2888c8b73c4SFrederic Weisbecker acct->utime_sspurr = acct->utime; 289cf9efce0SPaul Mackerras 290cf9efce0SPaul Mackerras /* 291cf9efce0SPaul Mackerras * Because we don't read the SPURR on every kernel entry/exit, 292cf9efce0SPaul Mackerras * deltascaled includes both user and system SPURR ticks. 293cf9efce0SPaul Mackerras * Apportion these ticks to system SPURR ticks and user 294cf9efce0SPaul Mackerras * SPURR ticks in the same ratio as the system time (delta) 295cf9efce0SPaul Mackerras * and user time (udelta) values obtained from the timebase 296cf9efce0SPaul Mackerras * over the same interval. The system ticks get accounted here; 297cf9efce0SPaul Mackerras * the user ticks get saved up in paca->user_time_scaled to be 298cf9efce0SPaul Mackerras * used by account_process_tick. 299cf9efce0SPaul Mackerras */ 300b38a181cSChristophe Leroy stime_scaled = stime; 301a19ff1a2SFrederic Weisbecker utime_scaled = utime; 302a19ff1a2SFrederic Weisbecker if (deltascaled != stime + utime) { 303a19ff1a2SFrederic Weisbecker if (utime) { 304b38a181cSChristophe Leroy stime_scaled = deltascaled * stime / (stime + utime); 305b38a181cSChristophe Leroy utime_scaled = deltascaled - stime_scaled; 306cf9efce0SPaul Mackerras } else { 307b38a181cSChristophe Leroy stime_scaled = deltascaled; 308c6622f63SPaul Mackerras } 309cf9efce0SPaul Mackerras } 310a19ff1a2SFrederic Weisbecker acct->utime_scaled += utime_scaled; 311abcff86dSChristophe Leroy #endif 312cf9efce0SPaul Mackerras 313b38a181cSChristophe Leroy return stime_scaled; 314b38a181cSChristophe Leroy } 315b38a181cSChristophe Leroy 3168a6a5920SFrederic Weisbecker static unsigned long vtime_delta(struct cpu_accounting_data *acct, 317b38a181cSChristophe Leroy unsigned long *stime_scaled, 318b38a181cSChristophe Leroy unsigned long *steal_time) 319b38a181cSChristophe Leroy { 320b38a181cSChristophe Leroy unsigned long now, stime; 321b38a181cSChristophe Leroy 322b38a181cSChristophe Leroy WARN_ON_ONCE(!irqs_disabled()); 323b38a181cSChristophe Leroy 324b38a181cSChristophe Leroy now = mftb(); 325b38a181cSChristophe Leroy stime = now - acct->starttime; 326b38a181cSChristophe Leroy acct->starttime = now; 327b38a181cSChristophe Leroy 328b38a181cSChristophe Leroy *stime_scaled = vtime_delta_scaled(acct, now, stime); 329b38a181cSChristophe Leroy 330b38a181cSChristophe Leroy *steal_time = calculate_stolen_time(now); 331b38a181cSChristophe Leroy 332a19ff1a2SFrederic Weisbecker return stime; 333a7e1a9e3SFrederic Weisbecker } 334a7e1a9e3SFrederic Weisbecker 3358a6a5920SFrederic Weisbecker static void vtime_delta_kernel(struct cpu_accounting_data *acct, 3368a6a5920SFrederic Weisbecker unsigned long *stime, unsigned long *stime_scaled) 3378a6a5920SFrederic Weisbecker { 3388a6a5920SFrederic Weisbecker unsigned long steal_time; 3398a6a5920SFrederic Weisbecker 3408a6a5920SFrederic Weisbecker *stime = vtime_delta(acct, stime_scaled, &steal_time); 3418a6a5920SFrederic Weisbecker *stime -= min(*stime, steal_time); 3428a6a5920SFrederic Weisbecker acct->steal_time += steal_time; 3438a6a5920SFrederic Weisbecker } 3448a6a5920SFrederic Weisbecker 345f83eeb1aSFrederic Weisbecker void vtime_account_kernel(struct task_struct *tsk) 346a7e1a9e3SFrederic Weisbecker { 347a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3488a6a5920SFrederic Weisbecker unsigned long stime, stime_scaled; 349a7e1a9e3SFrederic Weisbecker 3508a6a5920SFrederic Weisbecker vtime_delta_kernel(acct, &stime, &stime_scaled); 351a19ff1a2SFrederic Weisbecker 3528a6a5920SFrederic Weisbecker if (tsk->flags & PF_VCPU) { 353a19ff1a2SFrederic Weisbecker acct->gtime += stime; 354abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 355a19ff1a2SFrederic Weisbecker acct->utime_scaled += stime_scaled; 356abcff86dSChristophe Leroy #endif 357a19ff1a2SFrederic Weisbecker } else { 358a19ff1a2SFrederic Weisbecker acct->stime += stime; 359abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 360a19ff1a2SFrederic Weisbecker acct->stime_scaled += stime_scaled; 361abcff86dSChristophe Leroy #endif 362a19ff1a2SFrederic Weisbecker } 363a7e1a9e3SFrederic Weisbecker } 364f83eeb1aSFrederic Weisbecker EXPORT_SYMBOL_GPL(vtime_account_kernel); 365a7e1a9e3SFrederic Weisbecker 366fd25b4c2SFrederic Weisbecker void vtime_account_idle(struct task_struct *tsk) 367a7e1a9e3SFrederic Weisbecker { 368a19ff1a2SFrederic Weisbecker unsigned long stime, stime_scaled, steal_time; 369a19ff1a2SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 370a7e1a9e3SFrederic Weisbecker 3718a6a5920SFrederic Weisbecker stime = vtime_delta(acct, &stime_scaled, &steal_time); 372a19ff1a2SFrederic Weisbecker acct->idle_time += stime + steal_time; 373cf9efce0SPaul Mackerras } 374c6622f63SPaul Mackerras 3758a6a5920SFrederic Weisbecker static void vtime_account_irq_field(struct cpu_accounting_data *acct, 3768a6a5920SFrederic Weisbecker unsigned long *field) 3778a6a5920SFrederic Weisbecker { 3788a6a5920SFrederic Weisbecker unsigned long stime, stime_scaled; 3798a6a5920SFrederic Weisbecker 3808a6a5920SFrederic Weisbecker vtime_delta_kernel(acct, &stime, &stime_scaled); 3818a6a5920SFrederic Weisbecker *field += stime; 3828a6a5920SFrederic Weisbecker #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 3838a6a5920SFrederic Weisbecker acct->stime_scaled += stime_scaled; 3848a6a5920SFrederic Weisbecker #endif 3858a6a5920SFrederic Weisbecker } 3868a6a5920SFrederic Weisbecker 3878a6a5920SFrederic Weisbecker void vtime_account_softirq(struct task_struct *tsk) 3888a6a5920SFrederic Weisbecker { 3898a6a5920SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3908a6a5920SFrederic Weisbecker vtime_account_irq_field(acct, &acct->softirq_time); 3918a6a5920SFrederic Weisbecker } 3928a6a5920SFrederic Weisbecker 3938a6a5920SFrederic Weisbecker void vtime_account_hardirq(struct task_struct *tsk) 3948a6a5920SFrederic Weisbecker { 3958a6a5920SFrederic Weisbecker struct cpu_accounting_data *acct = get_accounting(tsk); 3968a6a5920SFrederic Weisbecker vtime_account_irq_field(acct, &acct->hardirq_time); 3978a6a5920SFrederic Weisbecker } 3988a6a5920SFrederic Weisbecker 399b38a181cSChristophe Leroy static void vtime_flush_scaled(struct task_struct *tsk, 400b38a181cSChristophe Leroy struct cpu_accounting_data *acct) 401b38a181cSChristophe Leroy { 402abcff86dSChristophe Leroy #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 403b38a181cSChristophe Leroy if (acct->utime_scaled) 404b38a181cSChristophe Leroy tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled); 405b38a181cSChristophe Leroy if (acct->stime_scaled) 406b38a181cSChristophe Leroy tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled); 407b38a181cSChristophe Leroy 408b38a181cSChristophe Leroy acct->utime_scaled = 0; 409b38a181cSChristophe Leroy acct->utime_sspurr = 0; 410b38a181cSChristophe Leroy acct->stime_scaled = 0; 411abcff86dSChristophe Leroy #endif 412b38a181cSChristophe Leroy } 413b38a181cSChristophe Leroy 414c6622f63SPaul Mackerras /* 415c8d7dabfSFrederic Weisbecker * Account the whole cputime accumulated in the paca 416c6622f63SPaul Mackerras * Must be called with interrupts disabled. 417f83eeb1aSFrederic Weisbecker * Assumes that vtime_account_kernel/idle() has been called 418bcebdf84SFrederic Weisbecker * recently (i.e. since the last entry from usermode) so that 419cf9efce0SPaul Mackerras * get_paca()->user_time_scaled is up to date. 420c6622f63SPaul Mackerras */ 421c8d7dabfSFrederic Weisbecker void vtime_flush(struct task_struct *tsk) 422c6622f63SPaul Mackerras { 423c223c903SChristophe Leroy struct cpu_accounting_data *acct = get_accounting(tsk); 424c6622f63SPaul Mackerras 425a19ff1a2SFrederic Weisbecker if (acct->utime) 42623244a5cSFrederic Weisbecker account_user_time(tsk, cputime_to_nsecs(acct->utime)); 427a19ff1a2SFrederic Weisbecker 428a19ff1a2SFrederic Weisbecker if (acct->gtime) 429fb8b049cSFrederic Weisbecker account_guest_time(tsk, cputime_to_nsecs(acct->gtime)); 430a19ff1a2SFrederic Weisbecker 43151eeef9eSChristophe Leroy if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) { 432be9095edSFrederic Weisbecker account_steal_time(cputime_to_nsecs(acct->steal_time)); 43351eeef9eSChristophe Leroy acct->steal_time = 0; 43451eeef9eSChristophe Leroy } 435a19ff1a2SFrederic Weisbecker 436a19ff1a2SFrederic Weisbecker if (acct->idle_time) 43718b43a9bSFrederic Weisbecker account_idle_time(cputime_to_nsecs(acct->idle_time)); 438a19ff1a2SFrederic Weisbecker 439a19ff1a2SFrederic Weisbecker if (acct->stime) 440fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->stime), 441fb8b049cSFrederic Weisbecker CPUTIME_SYSTEM); 442a19ff1a2SFrederic Weisbecker 443a19ff1a2SFrederic Weisbecker if (acct->hardirq_time) 444fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time), 445fb8b049cSFrederic Weisbecker CPUTIME_IRQ); 446a19ff1a2SFrederic Weisbecker if (acct->softirq_time) 447fb8b049cSFrederic Weisbecker account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time), 448fb8b049cSFrederic Weisbecker CPUTIME_SOFTIRQ); 449a19ff1a2SFrederic Weisbecker 450b38a181cSChristophe Leroy vtime_flush_scaled(tsk, acct); 451b38a181cSChristophe Leroy 4528c8b73c4SFrederic Weisbecker acct->utime = 0; 453a19ff1a2SFrederic Weisbecker acct->gtime = 0; 454a19ff1a2SFrederic Weisbecker acct->idle_time = 0; 455a19ff1a2SFrederic Weisbecker acct->stime = 0; 456a19ff1a2SFrederic Weisbecker acct->hardirq_time = 0; 457a19ff1a2SFrederic Weisbecker acct->softirq_time = 0; 458c6622f63SPaul Mackerras } 459c6622f63SPaul Mackerras 460abf917cdSFrederic Weisbecker #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ 461c6622f63SPaul Mackerras #define calc_cputime_factors() 462c6622f63SPaul Mackerras #endif 463c6622f63SPaul Mackerras 4646defa38bSPaul Mackerras void __delay(unsigned long loops) 4656defa38bSPaul Mackerras { 4666defa38bSPaul Mackerras unsigned long start; 4676defa38bSPaul Mackerras 4684e287e65SNicholas Piggin spin_begin(); 469a4c5a355SChristophe Leroy if (tb_invalid) { 470de269129SMahesh Salgaonkar /* 471de269129SMahesh Salgaonkar * TB is in error state and isn't ticking anymore. 472de269129SMahesh Salgaonkar * HMI handler was unable to recover from TB error. 473de269129SMahesh Salgaonkar * Return immediately, so that kernel won't get stuck here. 474de269129SMahesh Salgaonkar */ 475de269129SMahesh Salgaonkar spin_cpu_relax(); 4766defa38bSPaul Mackerras } else { 477942e8911SChristophe Leroy start = mftb(); 478942e8911SChristophe Leroy while (mftb() - start < loops) 4794e287e65SNicholas Piggin spin_cpu_relax(); 4806defa38bSPaul Mackerras } 4814e287e65SNicholas Piggin spin_end(); 4826defa38bSPaul Mackerras } 4836defa38bSPaul Mackerras EXPORT_SYMBOL(__delay); 4846defa38bSPaul Mackerras 4856defa38bSPaul Mackerras void udelay(unsigned long usecs) 4866defa38bSPaul Mackerras { 4876defa38bSPaul Mackerras __delay(tb_ticks_per_usec * usecs); 4886defa38bSPaul Mackerras } 4896defa38bSPaul Mackerras EXPORT_SYMBOL(udelay); 4906defa38bSPaul Mackerras 491f2783c15SPaul Mackerras #ifdef CONFIG_SMP 492f2783c15SPaul Mackerras unsigned long profile_pc(struct pt_regs *regs) 493f2783c15SPaul Mackerras { 494f2783c15SPaul Mackerras unsigned long pc = instruction_pointer(regs); 495f2783c15SPaul Mackerras 496f2783c15SPaul Mackerras if (in_lock_functions(pc)) 497f2783c15SPaul Mackerras return regs->link; 498f2783c15SPaul Mackerras 499f2783c15SPaul Mackerras return pc; 500f2783c15SPaul Mackerras } 501f2783c15SPaul Mackerras EXPORT_SYMBOL(profile_pc); 502f2783c15SPaul Mackerras #endif 503f2783c15SPaul Mackerras 504e360adbeSPeter Zijlstra #ifdef CONFIG_IRQ_WORK 505105988c0SPaul Mackerras 5060fe1ac48SPaul Mackerras /* 5070fe1ac48SPaul Mackerras * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... 5080fe1ac48SPaul Mackerras */ 5090fe1ac48SPaul Mackerras #ifdef CONFIG_PPC64 510e360adbeSPeter Zijlstra static inline unsigned long test_irq_work_pending(void) 511105988c0SPaul Mackerras { 5120fe1ac48SPaul Mackerras unsigned long x; 5130fe1ac48SPaul Mackerras 5140fe1ac48SPaul Mackerras asm volatile("lbz %0,%1(13)" 5150fe1ac48SPaul Mackerras : "=r" (x) 516e360adbeSPeter Zijlstra : "i" (offsetof(struct paca_struct, irq_work_pending))); 5170fe1ac48SPaul Mackerras return x; 518105988c0SPaul Mackerras } 519105988c0SPaul Mackerras 520e360adbeSPeter Zijlstra static inline void set_irq_work_pending_flag(void) 5210fe1ac48SPaul Mackerras { 5220fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5230fe1ac48SPaul Mackerras "r" (1), 524e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5250fe1ac48SPaul Mackerras } 5260fe1ac48SPaul Mackerras 527e360adbeSPeter Zijlstra static inline void clear_irq_work_pending(void) 5280fe1ac48SPaul Mackerras { 5290fe1ac48SPaul Mackerras asm volatile("stb %0,%1(13)" : : 5300fe1ac48SPaul Mackerras "r" (0), 531e360adbeSPeter Zijlstra "i" (offsetof(struct paca_struct, irq_work_pending))); 5320fe1ac48SPaul Mackerras } 5330fe1ac48SPaul Mackerras 5340fe1ac48SPaul Mackerras #else /* 32-bit */ 5350fe1ac48SPaul Mackerras 536e360adbeSPeter Zijlstra DEFINE_PER_CPU(u8, irq_work_pending); 5370fe1ac48SPaul Mackerras 53869111bacSChristoph Lameter #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1) 53969111bacSChristoph Lameter #define test_irq_work_pending() __this_cpu_read(irq_work_pending) 54069111bacSChristoph Lameter #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0) 541105988c0SPaul Mackerras 542abc3fce7SNicholas Piggin #endif /* 32 vs 64 bit */ 543abc3fce7SNicholas Piggin 5444f8b50bbSPeter Zijlstra void arch_irq_work_raise(void) 5450fe1ac48SPaul Mackerras { 546abc3fce7SNicholas Piggin /* 547abc3fce7SNicholas Piggin * 64-bit code that uses irq soft-mask can just cause an immediate 548abc3fce7SNicholas Piggin * interrupt here that gets soft masked, if this is called under 549abc3fce7SNicholas Piggin * local_irq_disable(). It might be possible to prevent that happening 550abc3fce7SNicholas Piggin * by noticing interrupts are disabled and setting decrementer pending 551abc3fce7SNicholas Piggin * to be replayed when irqs are enabled. The problem there is that 552abc3fce7SNicholas Piggin * tracing can call irq_work_raise, including in code that does low 553abc3fce7SNicholas Piggin * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on) 554abc3fce7SNicholas Piggin * which could get tangled up if we're messing with the same state 555abc3fce7SNicholas Piggin * here. 556abc3fce7SNicholas Piggin */ 5570fe1ac48SPaul Mackerras preempt_disable(); 558e360adbeSPeter Zijlstra set_irq_work_pending_flag(); 5590fe1ac48SPaul Mackerras set_dec(1); 5600fe1ac48SPaul Mackerras preempt_enable(); 5610fe1ac48SPaul Mackerras } 5620fe1ac48SPaul Mackerras 563e360adbeSPeter Zijlstra #else /* CONFIG_IRQ_WORK */ 564105988c0SPaul Mackerras 565e360adbeSPeter Zijlstra #define test_irq_work_pending() 0 566e360adbeSPeter Zijlstra #define clear_irq_work_pending() 567105988c0SPaul Mackerras 568e360adbeSPeter Zijlstra #endif /* CONFIG_IRQ_WORK */ 569105988c0SPaul Mackerras 5701b783955SPreeti U Murthy /* 5711b783955SPreeti U Murthy * timer_interrupt - gets called when the decrementer overflows, 5721b783955SPreeti U Murthy * with interrupts disabled. 5731b783955SPreeti U Murthy */ 5741b783955SPreeti U Murthy void timer_interrupt(struct pt_regs *regs) 5751b783955SPreeti U Murthy { 5763f984620SNicholas Piggin struct clock_event_device *evt = this_cpu_ptr(&decrementers); 57769111bacSChristoph Lameter u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 5783f984620SNicholas Piggin struct pt_regs *old_regs; 5793f984620SNicholas Piggin u64 now; 5801b783955SPreeti U Murthy 58159d512e4SNicholas Piggin /* 58259d512e4SNicholas Piggin * Some implementations of hotplug will get timer interrupts while 58359d512e4SNicholas Piggin * offline, just ignore these. 5841b783955SPreeti U Murthy */ 585a7cba02dSNicholas Piggin if (unlikely(!cpu_online(smp_processor_id()))) { 586a7cba02dSNicholas Piggin set_dec(decrementer_max); 5871b783955SPreeti U Murthy return; 5881b783955SPreeti U Murthy } 5891b783955SPreeti U Murthy 590a7cba02dSNicholas Piggin /* Ensure a positive value is written to the decrementer, or else 591a7cba02dSNicholas Piggin * some CPUs will continue to take decrementer exceptions. When the 592a7cba02dSNicholas Piggin * PPC_WATCHDOG (decrementer based) is configured, keep this at most 593a7cba02dSNicholas Piggin * 31 bits, which is about 4 seconds on most systems, which gives 594a7cba02dSNicholas Piggin * the watchdog a chance of catching timer interrupt hard lockups. 595a7cba02dSNicholas Piggin */ 596a7cba02dSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_WATCHDOG)) 597a7cba02dSNicholas Piggin set_dec(0x7fffffff); 598a7cba02dSNicholas Piggin else 599a7cba02dSNicholas Piggin set_dec(decrementer_max); 600a7cba02dSNicholas Piggin 6011b783955SPreeti U Murthy /* Conditionally hard-enable interrupts now that the DEC has been 6021b783955SPreeti U Murthy * bumped to its maximum value 6031b783955SPreeti U Murthy */ 6041b783955SPreeti U Murthy may_hard_irq_enable(); 6051b783955SPreeti U Murthy 6061b783955SPreeti U Murthy 6076e0fdf9aSPaul Bolle #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC) 6081b783955SPreeti U Murthy if (atomic_read(&ppc_n_lost_interrupts) != 0) 6091b783955SPreeti U Murthy do_IRQ(regs); 6101b783955SPreeti U Murthy #endif 6111b783955SPreeti U Murthy 6121b783955SPreeti U Murthy old_regs = set_irq_regs(regs); 6131b783955SPreeti U Murthy irq_enter(); 6143f984620SNicholas Piggin trace_timer_interrupt_entry(regs); 6151b783955SPreeti U Murthy 6163f984620SNicholas Piggin if (test_irq_work_pending()) { 6173f984620SNicholas Piggin clear_irq_work_pending(); 6183f984620SNicholas Piggin irq_work_run(); 6193f984620SNicholas Piggin } 6203f984620SNicholas Piggin 6216601ec1cSChristophe Leroy now = get_tb(); 6223f984620SNicholas Piggin if (now >= *next_tb) { 6233f984620SNicholas Piggin *next_tb = ~(u64)0; 6243f984620SNicholas Piggin if (evt->event_handler) 6253f984620SNicholas Piggin evt->event_handler(evt); 6263f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_event); 6273f984620SNicholas Piggin } else { 6283f984620SNicholas Piggin now = *next_tb - now; 6293f984620SNicholas Piggin if (now <= decrementer_max) 6303f984620SNicholas Piggin set_dec(now); 6313f984620SNicholas Piggin /* We may have raced with new irq work */ 6323f984620SNicholas Piggin if (test_irq_work_pending()) 6333f984620SNicholas Piggin set_dec(1); 6343f984620SNicholas Piggin __this_cpu_inc(irq_stat.timer_irqs_others); 6353f984620SNicholas Piggin } 6363f984620SNicholas Piggin 6373f984620SNicholas Piggin trace_timer_interrupt_exit(regs); 638f2783c15SPaul Mackerras irq_exit(); 6397d12e780SDavid Howells set_irq_regs(old_regs); 640f2783c15SPaul Mackerras } 6419445aa1aSAl Viro EXPORT_SYMBOL(timer_interrupt); 642f2783c15SPaul Mackerras 643bc907113SNicholas Piggin #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 6443f984620SNicholas Piggin void timer_broadcast_interrupt(void) 6453f984620SNicholas Piggin { 6463f984620SNicholas Piggin u64 *next_tb = this_cpu_ptr(&decrementers_next_tb); 6473f984620SNicholas Piggin 6483f984620SNicholas Piggin *next_tb = ~(u64)0; 6493f984620SNicholas Piggin tick_receive_broadcast(); 650e360cd37SNicholas Piggin __this_cpu_inc(irq_stat.broadcast_irqs_event); 6513f984620SNicholas Piggin } 652bc907113SNicholas Piggin #endif 6533f984620SNicholas Piggin 6547ac5dde9SScott Wood #ifdef CONFIG_SUSPEND 655d75d68cfSPaul Mackerras static void generic_suspend_disable_irqs(void) 6567ac5dde9SScott Wood { 6577ac5dde9SScott Wood /* Disable the decrementer, so that it doesn't interfere 6587ac5dde9SScott Wood * with suspending. 6597ac5dde9SScott Wood */ 6607ac5dde9SScott Wood 66179901024SOliver O'Halloran set_dec(decrementer_max); 6627ac5dde9SScott Wood local_irq_disable(); 66379901024SOliver O'Halloran set_dec(decrementer_max); 6647ac5dde9SScott Wood } 6657ac5dde9SScott Wood 666d75d68cfSPaul Mackerras static void generic_suspend_enable_irqs(void) 6677ac5dde9SScott Wood { 6687ac5dde9SScott Wood local_irq_enable(); 6697ac5dde9SScott Wood } 6707ac5dde9SScott Wood 6717ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6727ac5dde9SScott Wood void arch_suspend_disable_irqs(void) 6737ac5dde9SScott Wood { 6747ac5dde9SScott Wood if (ppc_md.suspend_disable_irqs) 6757ac5dde9SScott Wood ppc_md.suspend_disable_irqs(); 6767ac5dde9SScott Wood generic_suspend_disable_irqs(); 6777ac5dde9SScott Wood } 6787ac5dde9SScott Wood 6797ac5dde9SScott Wood /* Overrides the weak version in kernel/power/main.c */ 6807ac5dde9SScott Wood void arch_suspend_enable_irqs(void) 6817ac5dde9SScott Wood { 6827ac5dde9SScott Wood generic_suspend_enable_irqs(); 6837ac5dde9SScott Wood if (ppc_md.suspend_enable_irqs) 6847ac5dde9SScott Wood ppc_md.suspend_enable_irqs(); 6857ac5dde9SScott Wood } 6867ac5dde9SScott Wood #endif 6877ac5dde9SScott Wood 688b6c295dfSPaul Mackerras unsigned long long tb_to_ns(unsigned long long ticks) 689b6c295dfSPaul Mackerras { 690b6c295dfSPaul Mackerras return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift; 691b6c295dfSPaul Mackerras } 692b6c295dfSPaul Mackerras EXPORT_SYMBOL_GPL(tb_to_ns); 693b6c295dfSPaul Mackerras 694f2783c15SPaul Mackerras /* 695f2783c15SPaul Mackerras * Scheduler clock - returns current time in nanosec units. 696f2783c15SPaul Mackerras * 697f2783c15SPaul Mackerras * Note: mulhdu(a, b) (multiply high double unsigned) returns 698f2783c15SPaul Mackerras * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b 699f2783c15SPaul Mackerras * are 64-bit unsigned numbers. 700f2783c15SPaul Mackerras */ 7016b847d79SSantosh Sivaraj notrace unsigned long long sched_clock(void) 702f2783c15SPaul Mackerras { 703fc9069feSTony Breeds return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 704f2783c15SPaul Mackerras } 705f2783c15SPaul Mackerras 7064be1b297SCyril Bur 7074be1b297SCyril Bur #ifdef CONFIG_PPC_PSERIES 7084be1b297SCyril Bur 7094be1b297SCyril Bur /* 7104be1b297SCyril Bur * Running clock - attempts to give a view of time passing for a virtualised 7114be1b297SCyril Bur * kernels. 7124be1b297SCyril Bur * Uses the VTB register if available otherwise a next best guess. 7134be1b297SCyril Bur */ 7144be1b297SCyril Bur unsigned long long running_clock(void) 7154be1b297SCyril Bur { 7164be1b297SCyril Bur /* 7174be1b297SCyril Bur * Don't read the VTB as a host since KVM does not switch in host 7184be1b297SCyril Bur * timebase into the VTB when it takes a guest off the CPU, reading the 7194be1b297SCyril Bur * VTB would result in reading 'last switched out' guest VTB. 7204be1b297SCyril Bur * 7214be1b297SCyril Bur * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it 7224be1b297SCyril Bur * would be unsafe to rely only on the #ifdef above. 7234be1b297SCyril Bur */ 7244be1b297SCyril Bur if (firmware_has_feature(FW_FEATURE_LPAR) && 7254be1b297SCyril Bur cpu_has_feature(CPU_FTR_ARCH_207S)) 7264be1b297SCyril Bur return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; 7274be1b297SCyril Bur 7284be1b297SCyril Bur /* 7294be1b297SCyril Bur * This is a next best approximation without a VTB. 7304be1b297SCyril Bur * On a host which is running bare metal there should never be any stolen 7314be1b297SCyril Bur * time and on a host which doesn't do any virtualisation TB *should* equal 7324be1b297SCyril Bur * VTB so it makes no difference anyway. 7334be1b297SCyril Bur */ 7349f3768e0SFrédéric Weisbecker return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL]; 7354be1b297SCyril Bur } 7364be1b297SCyril Bur #endif 7374be1b297SCyril Bur 7380bb474a4SAnton Blanchard static int __init get_freq(char *name, int cells, unsigned long *val) 739f2783c15SPaul Mackerras { 740f2783c15SPaul Mackerras struct device_node *cpu; 7416f7aba7bSAnton Blanchard const __be32 *fp; 7420bb474a4SAnton Blanchard int found = 0; 743f2783c15SPaul Mackerras 7440bb474a4SAnton Blanchard /* The cpu node should have timebase and clock frequency properties */ 745f2783c15SPaul Mackerras cpu = of_find_node_by_type(NULL, "cpu"); 746f2783c15SPaul Mackerras 747d8a8188dSOlaf Hering if (cpu) { 748e2eb6392SStephen Rothwell fp = of_get_property(cpu, name, NULL); 749d8a8188dSOlaf Hering if (fp) { 7500bb474a4SAnton Blanchard found = 1; 751a4dc7ff0SPaul Mackerras *val = of_read_ulong(fp, cells); 752f2783c15SPaul Mackerras } 7530bb474a4SAnton Blanchard 7540bb474a4SAnton Blanchard of_node_put(cpu); 755f2783c15SPaul Mackerras } 7560bb474a4SAnton Blanchard 7570bb474a4SAnton Blanchard return found; 7580bb474a4SAnton Blanchard } 7590bb474a4SAnton Blanchard 760e51df2c1SAnton Blanchard static void start_cpu_decrementer(void) 76177c0a700SBenjamin Herrenschmidt { 76277c0a700SBenjamin Herrenschmidt #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) 7636e2f03e2SIvan Mikhaylov unsigned int tcr; 7646e2f03e2SIvan Mikhaylov 76577c0a700SBenjamin Herrenschmidt /* Clear any pending timer interrupts */ 76677c0a700SBenjamin Herrenschmidt mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); 76777c0a700SBenjamin Herrenschmidt 7686e2f03e2SIvan Mikhaylov tcr = mfspr(SPRN_TCR); 7696e2f03e2SIvan Mikhaylov /* 7706e2f03e2SIvan Mikhaylov * The watchdog may have already been enabled by u-boot. So leave 7716e2f03e2SIvan Mikhaylov * TRC[WP] (Watchdog Period) alone. 7726e2f03e2SIvan Mikhaylov */ 7736e2f03e2SIvan Mikhaylov tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */ 7746e2f03e2SIvan Mikhaylov tcr |= TCR_DIE; /* Enable decrementer */ 7756e2f03e2SIvan Mikhaylov mtspr(SPRN_TCR, tcr); 7766e2f03e2SIvan Mikhaylov #endif 77777c0a700SBenjamin Herrenschmidt } 77877c0a700SBenjamin Herrenschmidt 7790bb474a4SAnton Blanchard void __init generic_calibrate_decr(void) 7800bb474a4SAnton Blanchard { 7810bb474a4SAnton Blanchard ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ 7820bb474a4SAnton Blanchard 7830bb474a4SAnton Blanchard if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && 7840bb474a4SAnton Blanchard !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { 7850bb474a4SAnton Blanchard 786f2783c15SPaul Mackerras printk(KERN_ERR "WARNING: Estimating decrementer frequency " 787f2783c15SPaul Mackerras "(not found)\n"); 7880bb474a4SAnton Blanchard } 789f2783c15SPaul Mackerras 7900bb474a4SAnton Blanchard ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ 7910bb474a4SAnton Blanchard 7920bb474a4SAnton Blanchard if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && 7930bb474a4SAnton Blanchard !get_freq("clock-frequency", 1, &ppc_proc_freq)) { 7940bb474a4SAnton Blanchard 7950bb474a4SAnton Blanchard printk(KERN_ERR "WARNING: Estimating processor frequency " 7960bb474a4SAnton Blanchard "(not found)\n"); 797f2783c15SPaul Mackerras } 798f2783c15SPaul Mackerras } 799f2783c15SPaul Mackerras 8005235afa8SArnd Bergmann int update_persistent_clock64(struct timespec64 now) 801f2783c15SPaul Mackerras { 802f2783c15SPaul Mackerras struct rtc_time tm; 803f2783c15SPaul Mackerras 804aa3be5f3STony Breeds if (!ppc_md.set_rtc_time) 805023f333aSJason Gunthorpe return -ENODEV; 806aa3be5f3STony Breeds 8075235afa8SArnd Bergmann rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm); 808aa3be5f3STony Breeds 809aa3be5f3STony Breeds return ppc_md.set_rtc_time(&tm); 810aa3be5f3STony Breeds } 811aa3be5f3STony Breeds 8125bfd6435SArnd Bergmann static void __read_persistent_clock(struct timespec64 *ts) 813aa3be5f3STony Breeds { 814aa3be5f3STony Breeds struct rtc_time tm; 815aa3be5f3STony Breeds static int first = 1; 816aa3be5f3STony Breeds 817d90246cdSMartin Schwidefsky ts->tv_nsec = 0; 818aa3be5f3STony Breeds /* XXX this is a litle fragile but will work okay in the short term */ 819aa3be5f3STony Breeds if (first) { 820aa3be5f3STony Breeds first = 0; 821aa3be5f3STony Breeds if (ppc_md.time_init) 822aa3be5f3STony Breeds timezone_offset = ppc_md.time_init(); 823aa3be5f3STony Breeds 824aa3be5f3STony Breeds /* get_boot_time() isn't guaranteed to be safe to call late */ 825d90246cdSMartin Schwidefsky if (ppc_md.get_boot_time) { 826d90246cdSMartin Schwidefsky ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; 827d90246cdSMartin Schwidefsky return; 828aa3be5f3STony Breeds } 829d90246cdSMartin Schwidefsky } 830d90246cdSMartin Schwidefsky if (!ppc_md.get_rtc_time) { 831d90246cdSMartin Schwidefsky ts->tv_sec = 0; 832d90246cdSMartin Schwidefsky return; 833d90246cdSMartin Schwidefsky } 834f2783c15SPaul Mackerras ppc_md.get_rtc_time(&tm); 835978d7eb3SBenjamin Herrenschmidt 8365bfd6435SArnd Bergmann ts->tv_sec = rtc_tm_to_time64(&tm); 837f2783c15SPaul Mackerras } 838f2783c15SPaul Mackerras 8395bfd6435SArnd Bergmann void read_persistent_clock64(struct timespec64 *ts) 840978d7eb3SBenjamin Herrenschmidt { 841978d7eb3SBenjamin Herrenschmidt __read_persistent_clock(ts); 842978d7eb3SBenjamin Herrenschmidt 843978d7eb3SBenjamin Herrenschmidt /* Sanitize it in case real time clock is set below EPOCH */ 844978d7eb3SBenjamin Herrenschmidt if (ts->tv_sec < 0) { 845978d7eb3SBenjamin Herrenschmidt ts->tv_sec = 0; 846978d7eb3SBenjamin Herrenschmidt ts->tv_nsec = 0; 847978d7eb3SBenjamin Herrenschmidt } 848978d7eb3SBenjamin Herrenschmidt 849978d7eb3SBenjamin Herrenschmidt } 850978d7eb3SBenjamin Herrenschmidt 8514a4cfe38STony Breeds /* clocksource code */ 8526b847d79SSantosh Sivaraj static notrace u64 timebase_read(struct clocksource *cs) 8534a4cfe38STony Breeds { 854a5a1d1c2SThomas Gleixner return (u64)get_tb(); 8554a4cfe38STony Breeds } 8564a4cfe38STony Breeds 8571c21a293SMichael Ellerman static void __init clocksource_init(void) 8584a4cfe38STony Breeds { 859a4c5a355SChristophe Leroy struct clocksource *clock = &clocksource_timebase; 8604a4cfe38STony Breeds 86111b8633aSAnton Blanchard if (clocksource_register_hz(clock, tb_ticks_per_sec)) { 8624a4cfe38STony Breeds printk(KERN_ERR "clocksource: %s is already registered\n", 8634a4cfe38STony Breeds clock->name); 8644a4cfe38STony Breeds return; 8654a4cfe38STony Breeds } 8664a4cfe38STony Breeds 8674a4cfe38STony Breeds printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", 8684a4cfe38STony Breeds clock->name, clock->mult, clock->shift); 8694a4cfe38STony Breeds } 8704a4cfe38STony Breeds 871d831d0b8STony Breeds static int decrementer_set_next_event(unsigned long evt, 872d831d0b8STony Breeds struct clock_event_device *dev) 873d831d0b8STony Breeds { 8746601ec1cSChristophe Leroy __this_cpu_write(decrementers_next_tb, get_tb() + evt); 875d831d0b8STony Breeds set_dec(evt); 8760215f7d8SBenjamin Herrenschmidt 8770215f7d8SBenjamin Herrenschmidt /* We may have raced with new irq work */ 8780215f7d8SBenjamin Herrenschmidt if (test_irq_work_pending()) 8790215f7d8SBenjamin Herrenschmidt set_dec(1); 8800215f7d8SBenjamin Herrenschmidt 881d831d0b8STony Breeds return 0; 882d831d0b8STony Breeds } 883d831d0b8STony Breeds 88437a13e78SViresh Kumar static int decrementer_shutdown(struct clock_event_device *dev) 885d831d0b8STony Breeds { 88679901024SOliver O'Halloran decrementer_set_next_event(decrementer_max, dev); 88737a13e78SViresh Kumar return 0; 888d831d0b8STony Breeds } 889d831d0b8STony Breeds 890d831d0b8STony Breeds static void register_decrementer_clockevent(int cpu) 891d831d0b8STony Breeds { 8927df10275SAnton Blanchard struct clock_event_device *dec = &per_cpu(decrementers, cpu); 893d831d0b8STony Breeds 894d831d0b8STony Breeds *dec = decrementer_clockevent; 895320ab2b0SRusty Russell dec->cpumask = cpumask_of(cpu); 896d831d0b8STony Breeds 8978b78fdb0SAnton Blanchard clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max); 8988b78fdb0SAnton Blanchard 899b919ee82SAnton Blanchard printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", 900d831d0b8STony Breeds dec->name, dec->mult, dec->shift, cpu); 901b4d16ab5SMichael Ellerman 902b4d16ab5SMichael Ellerman /* Set values for KVM, see kvm_emulate_dec() */ 903b4d16ab5SMichael Ellerman decrementer_clockevent.mult = dec->mult; 904b4d16ab5SMichael Ellerman decrementer_clockevent.shift = dec->shift; 905d831d0b8STony Breeds } 906d831d0b8STony Breeds 90779901024SOliver O'Halloran static void enable_large_decrementer(void) 90879901024SOliver O'Halloran { 90979901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 91079901024SOliver O'Halloran return; 91179901024SOliver O'Halloran 91279901024SOliver O'Halloran if (decrementer_max <= DECREMENTER_DEFAULT_MAX) 91379901024SOliver O'Halloran return; 91479901024SOliver O'Halloran 91579901024SOliver O'Halloran /* 91679901024SOliver O'Halloran * If we're running as the hypervisor we need to enable the LD manually 91779901024SOliver O'Halloran * otherwise firmware should have done it for us. 91879901024SOliver O'Halloran */ 91979901024SOliver O'Halloran if (cpu_has_feature(CPU_FTR_HVMODE)) 92079901024SOliver O'Halloran mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD); 92179901024SOliver O'Halloran } 92279901024SOliver O'Halloran 92379901024SOliver O'Halloran static void __init set_decrementer_max(void) 92479901024SOliver O'Halloran { 92579901024SOliver O'Halloran struct device_node *cpu; 92679901024SOliver O'Halloran u32 bits = 32; 92779901024SOliver O'Halloran 92879901024SOliver O'Halloran /* Prior to ISAv3 the decrementer is always 32 bit */ 92979901024SOliver O'Halloran if (!cpu_has_feature(CPU_FTR_ARCH_300)) 93079901024SOliver O'Halloran return; 93179901024SOliver O'Halloran 93279901024SOliver O'Halloran cpu = of_find_node_by_type(NULL, "cpu"); 93379901024SOliver O'Halloran 93479901024SOliver O'Halloran if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) { 93579901024SOliver O'Halloran if (bits > 64 || bits < 32) { 93679901024SOliver O'Halloran pr_warn("time_init: firmware supplied invalid ibm,dec-bits"); 93779901024SOliver O'Halloran bits = 32; 93879901024SOliver O'Halloran } 93979901024SOliver O'Halloran 94079901024SOliver O'Halloran /* calculate the signed maximum given this many bits */ 94179901024SOliver O'Halloran decrementer_max = (1ul << (bits - 1)) - 1; 94279901024SOliver O'Halloran } 94379901024SOliver O'Halloran 94479901024SOliver O'Halloran of_node_put(cpu); 94579901024SOliver O'Halloran 94679901024SOliver O'Halloran pr_info("time_init: %u bit decrementer (max: %llx)\n", 94779901024SOliver O'Halloran bits, decrementer_max); 94879901024SOliver O'Halloran } 94979901024SOliver O'Halloran 950c481887fSMilton Miller static void __init init_decrementer_clockevent(void) 951d831d0b8STony Breeds { 9528b78fdb0SAnton Blanchard register_decrementer_clockevent(smp_processor_id()); 953d831d0b8STony Breeds } 954d831d0b8STony Breeds 955d831d0b8STony Breeds void secondary_cpu_time_init(void) 956d831d0b8STony Breeds { 95779901024SOliver O'Halloran /* Enable and test the large decrementer for this cpu */ 95879901024SOliver O'Halloran enable_large_decrementer(); 95979901024SOliver O'Halloran 96077c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 96177c0a700SBenjamin Herrenschmidt * such as BookE 96277c0a700SBenjamin Herrenschmidt */ 96377c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 96477c0a700SBenjamin Herrenschmidt 965d831d0b8STony Breeds /* FIME: Should make unrelatred change to move snapshot_timebase 966d831d0b8STony Breeds * call here ! */ 967d831d0b8STony Breeds register_decrementer_clockevent(smp_processor_id()); 968d831d0b8STony Breeds } 969d831d0b8STony Breeds 970f2783c15SPaul Mackerras /* This function is only called on the boot processor */ 971f2783c15SPaul Mackerras void __init time_init(void) 972f2783c15SPaul Mackerras { 973f2783c15SPaul Mackerras struct div_result res; 974d75d68cfSPaul Mackerras u64 scale; 975f2783c15SPaul Mackerras unsigned shift; 976f2783c15SPaul Mackerras 97796c44507SPaul Mackerras /* Normal PowerPC with timebase register */ 978f2783c15SPaul Mackerras ppc_md.calibrate_decr(); 979224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", 980374e99d4SPaul Mackerras ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); 981224ad80aSOlof Johansson printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", 982374e99d4SPaul Mackerras ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); 983374e99d4SPaul Mackerras 984374e99d4SPaul Mackerras tb_ticks_per_jiffy = ppc_tb_freq / HZ; 985092b8f34SPaul Mackerras tb_ticks_per_sec = ppc_tb_freq; 986374e99d4SPaul Mackerras tb_ticks_per_usec = ppc_tb_freq / 1000000; 987c6622f63SPaul Mackerras calc_cputime_factors(); 988092b8f34SPaul Mackerras 989092b8f34SPaul Mackerras /* 990f2783c15SPaul Mackerras * Compute scale factor for sched_clock. 991f2783c15SPaul Mackerras * The calibrate_decr() function has set tb_ticks_per_sec, 992f2783c15SPaul Mackerras * which is the timebase frequency. 993f2783c15SPaul Mackerras * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret 994f2783c15SPaul Mackerras * the 128-bit result as a 64.64 fixed-point number. 995f2783c15SPaul Mackerras * We then shift that number right until it is less than 1.0, 996f2783c15SPaul Mackerras * giving us the scale factor and shift count to use in 997f2783c15SPaul Mackerras * sched_clock(). 998f2783c15SPaul Mackerras */ 999f2783c15SPaul Mackerras div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); 1000f2783c15SPaul Mackerras scale = res.result_low; 1001f2783c15SPaul Mackerras for (shift = 0; res.result_high != 0; ++shift) { 1002f2783c15SPaul Mackerras scale = (scale >> 1) | (res.result_high << 63); 1003f2783c15SPaul Mackerras res.result_high >>= 1; 1004f2783c15SPaul Mackerras } 1005f2783c15SPaul Mackerras tb_to_ns_scale = scale; 1006f2783c15SPaul Mackerras tb_to_ns_shift = shift; 1007fc9069feSTony Breeds /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ 10086601ec1cSChristophe Leroy boot_tb = get_tb(); 1009f2783c15SPaul Mackerras 1010092b8f34SPaul Mackerras /* If platform provided a timezone (pmac), we correct the time */ 1011092b8f34SPaul Mackerras if (timezone_offset) { 1012092b8f34SPaul Mackerras sys_tz.tz_minuteswest = -timezone_offset / 60; 1013092b8f34SPaul Mackerras sys_tz.tz_dsttime = 0; 1014092b8f34SPaul Mackerras } 1015092b8f34SPaul Mackerras 1016a7f290daSBenjamin Herrenschmidt vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 1017f2783c15SPaul Mackerras 101879901024SOliver O'Halloran /* initialise and enable the large decrementer (if we have one) */ 101979901024SOliver O'Halloran set_decrementer_max(); 102079901024SOliver O'Halloran enable_large_decrementer(); 102179901024SOliver O'Halloran 102277c0a700SBenjamin Herrenschmidt /* Start the decrementer on CPUs that have manual control 102377c0a700SBenjamin Herrenschmidt * such as BookE 102477c0a700SBenjamin Herrenschmidt */ 102577c0a700SBenjamin Herrenschmidt start_cpu_decrementer(); 102677c0a700SBenjamin Herrenschmidt 1027f5339277SStephen Rothwell /* Register the clocksource */ 10284a4cfe38STony Breeds clocksource_init(); 10294a4cfe38STony Breeds 1030d831d0b8STony Breeds init_decrementer_clockevent(); 10310d948730SPreeti U Murthy tick_setup_hrtimer_broadcast(); 1032f0d37300SKevin Hao 1033f0d37300SKevin Hao of_clk_init(NULL); 1034*b709e32eSPingfan Liu enable_sched_clock_irqtime(); 1035f2783c15SPaul Mackerras } 1036f2783c15SPaul Mackerras 1037f2783c15SPaul Mackerras /* 1038f2783c15SPaul Mackerras * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit 1039f2783c15SPaul Mackerras * result. 1040f2783c15SPaul Mackerras */ 1041f2783c15SPaul Mackerras void div128_by_32(u64 dividend_high, u64 dividend_low, 1042f2783c15SPaul Mackerras unsigned divisor, struct div_result *dr) 1043f2783c15SPaul Mackerras { 1044f2783c15SPaul Mackerras unsigned long a, b, c, d; 1045f2783c15SPaul Mackerras unsigned long w, x, y, z; 1046f2783c15SPaul Mackerras u64 ra, rb, rc; 1047f2783c15SPaul Mackerras 1048f2783c15SPaul Mackerras a = dividend_high >> 32; 1049f2783c15SPaul Mackerras b = dividend_high & 0xffffffff; 1050f2783c15SPaul Mackerras c = dividend_low >> 32; 1051f2783c15SPaul Mackerras d = dividend_low & 0xffffffff; 1052f2783c15SPaul Mackerras 1053f2783c15SPaul Mackerras w = a / divisor; 1054f2783c15SPaul Mackerras ra = ((u64)(a - (w * divisor)) << 32) + b; 1055f2783c15SPaul Mackerras 1056f2783c15SPaul Mackerras rb = ((u64) do_div(ra, divisor) << 32) + c; 1057f2783c15SPaul Mackerras x = ra; 1058f2783c15SPaul Mackerras 1059f2783c15SPaul Mackerras rc = ((u64) do_div(rb, divisor) << 32) + d; 1060f2783c15SPaul Mackerras y = rb; 1061f2783c15SPaul Mackerras 1062f2783c15SPaul Mackerras do_div(rc, divisor); 1063f2783c15SPaul Mackerras z = rc; 1064f2783c15SPaul Mackerras 1065f2783c15SPaul Mackerras dr->result_high = ((u64)w << 32) + x; 1066f2783c15SPaul Mackerras dr->result_low = ((u64)y << 32) + z; 1067f2783c15SPaul Mackerras 1068f2783c15SPaul Mackerras } 1069bcd68a70SGeert Uytterhoeven 1070177996e6SBenjamin Herrenschmidt /* We don't need to calibrate delay, we use the CPU timebase for that */ 1071177996e6SBenjamin Herrenschmidt void calibrate_delay(void) 1072177996e6SBenjamin Herrenschmidt { 1073177996e6SBenjamin Herrenschmidt /* Some generic code (such as spinlock debug) use loops_per_jiffy 1074177996e6SBenjamin Herrenschmidt * as the number of __delay(1) in a jiffy, so make it so 1075177996e6SBenjamin Herrenschmidt */ 1076177996e6SBenjamin Herrenschmidt loops_per_jiffy = tb_ticks_per_jiffy; 1077177996e6SBenjamin Herrenschmidt } 1078177996e6SBenjamin Herrenschmidt 1079169047f4SArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC) 1080169047f4SArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm) 1081169047f4SArnd Bergmann { 1082169047f4SArnd Bergmann ppc_md.get_rtc_time(tm); 1083890ae797SAlexandre Belloni return 0; 1084169047f4SArnd Bergmann } 1085169047f4SArnd Bergmann 1086169047f4SArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm) 1087169047f4SArnd Bergmann { 1088169047f4SArnd Bergmann if (!ppc_md.set_rtc_time) 1089169047f4SArnd Bergmann return -EOPNOTSUPP; 1090169047f4SArnd Bergmann 1091169047f4SArnd Bergmann if (ppc_md.set_rtc_time(tm) < 0) 1092169047f4SArnd Bergmann return -EOPNOTSUPP; 1093169047f4SArnd Bergmann 1094169047f4SArnd Bergmann return 0; 1095169047f4SArnd Bergmann } 1096169047f4SArnd Bergmann 1097169047f4SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = { 1098169047f4SArnd Bergmann .read_time = rtc_generic_get_time, 1099169047f4SArnd Bergmann .set_time = rtc_generic_set_time, 1100169047f4SArnd Bergmann }; 1101169047f4SArnd Bergmann 1102bcd68a70SGeert Uytterhoeven static int __init rtc_init(void) 1103bcd68a70SGeert Uytterhoeven { 1104bcd68a70SGeert Uytterhoeven struct platform_device *pdev; 1105bcd68a70SGeert Uytterhoeven 1106bcd68a70SGeert Uytterhoeven if (!ppc_md.get_rtc_time) 1107bcd68a70SGeert Uytterhoeven return -ENODEV; 1108bcd68a70SGeert Uytterhoeven 1109169047f4SArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1, 1110169047f4SArnd Bergmann &rtc_generic_ops, 1111169047f4SArnd Bergmann sizeof(rtc_generic_ops)); 1112bcd68a70SGeert Uytterhoeven 11138c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev); 1114bcd68a70SGeert Uytterhoeven } 1115bcd68a70SGeert Uytterhoeven 11168f6b9512SPaul Gortmaker device_initcall(rtc_init); 1117169047f4SArnd Bergmann #endif 1118