1fd534e9bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27af192c9SGerd Hoffmann /* paravirtual clock -- common code used by kvm/xen
37af192c9SGerd Hoffmann
47af192c9SGerd Hoffmann */
57af192c9SGerd Hoffmann
67ac87074SVincenzo Frascino #include <linux/clocksource.h>
77af192c9SGerd Hoffmann #include <linux/kernel.h>
87af192c9SGerd Hoffmann #include <linux/percpu.h>
971056ae2SMarcelo Tosatti #include <linux/notifier.h>
1071056ae2SMarcelo Tosatti #include <linux/sched.h>
1171056ae2SMarcelo Tosatti #include <linux/gfp.h>
1257c8a661SMike Rapoport #include <linux/memblock.h>
1338b8d208SIngo Molnar #include <linux/nmi.h>
1438b8d208SIngo Molnar
1571056ae2SMarcelo Tosatti #include <asm/fixmap.h>
167af192c9SGerd Hoffmann #include <asm/pvclock.h>
179f08890aSJoao Martins #include <asm/vgtod.h>
187af192c9SGerd Hoffmann
19424c32f1SGlauber Costa static u8 valid_flags __read_mostly = 0;
209f08890aSJoao Martins static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly;
21424c32f1SGlauber Costa
pvclock_set_flags(u8 flags)22424c32f1SGlauber Costa void pvclock_set_flags(u8 flags)
23424c32f1SGlauber Costa {
24424c32f1SGlauber Costa valid_flags = flags;
25424c32f1SGlauber Costa }
26424c32f1SGlauber Costa
pvclock_tsc_khz(struct pvclock_vcpu_time_info * src)273807f345SGlauber Costa unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
283807f345SGlauber Costa {
29a0854600SHarvey Harrison u64 pv_tsc_khz = 1000000ULL << 32;
303807f345SGlauber Costa
31a0854600SHarvey Harrison do_div(pv_tsc_khz, src->tsc_to_system_mul);
323807f345SGlauber Costa if (src->tsc_shift < 0)
33a0854600SHarvey Harrison pv_tsc_khz <<= -src->tsc_shift;
343807f345SGlauber Costa else
35a0854600SHarvey Harrison pv_tsc_khz >>= src->tsc_shift;
36a0854600SHarvey Harrison return pv_tsc_khz;
373807f345SGlauber Costa }
383807f345SGlauber Costa
pvclock_touch_watchdogs(void)39d63285e9SMarcelo Tosatti void pvclock_touch_watchdogs(void)
40d63285e9SMarcelo Tosatti {
41d63285e9SMarcelo Tosatti touch_softlockup_watchdog_sync();
42d63285e9SMarcelo Tosatti clocksource_touch_watchdog();
43d63285e9SMarcelo Tosatti rcu_cpu_stall_reset();
448b414521SMarcelo Tosatti reset_hung_task_detector();
45d63285e9SMarcelo Tosatti }
46d63285e9SMarcelo Tosatti
47489fb490SGlauber Costa static atomic64_t last_value = ATOMIC64_INIT(0);
48489fb490SGlauber Costa
pvclock_resume(void)49e7a3481cSJeremy Fitzhardinge void pvclock_resume(void)
50e7a3481cSJeremy Fitzhardinge {
51e7a3481cSJeremy Fitzhardinge atomic64_set(&last_value, 0);
52e7a3481cSJeremy Fitzhardinge }
53e7a3481cSJeremy Fitzhardinge
pvclock_read_flags(struct pvclock_vcpu_time_info * src)542697902bSMarcelo Tosatti u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
552697902bSMarcelo Tosatti {
562697902bSMarcelo Tosatti unsigned version;
572697902bSMarcelo Tosatti u8 flags;
582697902bSMarcelo Tosatti
592697902bSMarcelo Tosatti do {
603aed64f6SPaolo Bonzini version = pvclock_read_begin(src);
61ed911b43SMinfei Huang flags = src->flags;
623aed64f6SPaolo Bonzini } while (pvclock_read_retry(src, version));
632697902bSMarcelo Tosatti
642697902bSMarcelo Tosatti return flags & valid_flags;
652697902bSMarcelo Tosatti }
662697902bSMarcelo Tosatti
678739c681SPeter Zijlstra static __always_inline
__pvclock_clocksource_read(struct pvclock_vcpu_time_info * src,bool dowd)688739c681SPeter Zijlstra u64 __pvclock_clocksource_read(struct pvclock_vcpu_time_info *src, bool dowd)
697af192c9SGerd Hoffmann {
707af192c9SGerd Hoffmann unsigned version;
71a5a1d1c2SThomas Gleixner u64 ret;
72489fb490SGlauber Costa u64 last;
7342b5637dSMarcelo Tosatti u8 flags;
747af192c9SGerd Hoffmann
757af192c9SGerd Hoffmann do {
763aed64f6SPaolo Bonzini version = pvclock_read_begin(src);
77108b249cSPaolo Bonzini ret = __pvclock_read_cycles(src, rdtsc_ordered());
783aed64f6SPaolo Bonzini flags = src->flags;
793aed64f6SPaolo Bonzini } while (pvclock_read_retry(src, version));
807af192c9SGerd Hoffmann
818739c681SPeter Zijlstra if (dowd && unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
82d63285e9SMarcelo Tosatti src->flags &= ~PVCLOCK_GUEST_STOPPED;
83d63285e9SMarcelo Tosatti pvclock_touch_watchdogs();
84d63285e9SMarcelo Tosatti }
85d63285e9SMarcelo Tosatti
863a0d7256SGlauber Costa if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
8742b5637dSMarcelo Tosatti (flags & PVCLOCK_TSC_STABLE_BIT))
883a0d7256SGlauber Costa return ret;
893a0d7256SGlauber Costa
90489fb490SGlauber Costa /*
91489fb490SGlauber Costa * Assumption here is that last_value, a global accumulator, always goes
92489fb490SGlauber Costa * forward. If we are less than that, we should not be much smaller.
93d9f6e12fSIngo Molnar * We assume there is an error margin we're inside, and then the correction
94489fb490SGlauber Costa * does not sacrifice accuracy.
95489fb490SGlauber Costa *
96489fb490SGlauber Costa * For reads: global may have changed between test and return,
97489fb490SGlauber Costa * but this means someone else updated poked the clock at a later time.
98489fb490SGlauber Costa * We just need to make sure we are not seeing a backwards event.
99489fb490SGlauber Costa *
100489fb490SGlauber Costa * For updates: last_value = ret is not enough, since two vcpus could be
101489fb490SGlauber Costa * updating at the same time, and one of them could be slightly behind,
102489fb490SGlauber Costa * making the assumption that last_value always go forward fail to hold.
103489fb490SGlauber Costa */
104*0f613bfaSMark Rutland last = raw_atomic64_read(&last_value);
105489fb490SGlauber Costa do {
1065c9da9feSUros Bizjak if (ret <= last)
107489fb490SGlauber Costa return last;
108*0f613bfaSMark Rutland } while (!raw_atomic64_try_cmpxchg(&last_value, &last, ret));
109489fb490SGlauber Costa
1107af192c9SGerd Hoffmann return ret;
1117af192c9SGerd Hoffmann }
1127af192c9SGerd Hoffmann
pvclock_clocksource_read(struct pvclock_vcpu_time_info * src)1138739c681SPeter Zijlstra u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
1148739c681SPeter Zijlstra {
1158739c681SPeter Zijlstra return __pvclock_clocksource_read(src, true);
1168739c681SPeter Zijlstra }
1178739c681SPeter Zijlstra
pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info * src)1188739c681SPeter Zijlstra noinstr u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src)
1198739c681SPeter Zijlstra {
1208739c681SPeter Zijlstra return __pvclock_clocksource_read(src, false);
1218739c681SPeter Zijlstra }
1228739c681SPeter Zijlstra
pvclock_read_wallclock(struct pvclock_wall_clock * wall_clock,struct pvclock_vcpu_time_info * vcpu_time,struct timespec64 * ts)1237af192c9SGerd Hoffmann void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
1247af192c9SGerd Hoffmann struct pvclock_vcpu_time_info *vcpu_time,
125e27c4929SArnd Bergmann struct timespec64 *ts)
1267af192c9SGerd Hoffmann {
1277af192c9SGerd Hoffmann u32 version;
1287af192c9SGerd Hoffmann u64 delta;
129e27c4929SArnd Bergmann struct timespec64 now;
1307af192c9SGerd Hoffmann
1317af192c9SGerd Hoffmann /* get wallclock at system boot */
1327af192c9SGerd Hoffmann do {
1337af192c9SGerd Hoffmann version = wall_clock->version;
1347af192c9SGerd Hoffmann rmb(); /* fetch version before time */
135e27c4929SArnd Bergmann /*
136e27c4929SArnd Bergmann * Note: wall_clock->sec is a u32 value, so it can
137e27c4929SArnd Bergmann * only store dates between 1970 and 2106. To allow
138e27c4929SArnd Bergmann * times beyond that, we need to create a new hypercall
139e27c4929SArnd Bergmann * interface with an extended pvclock_wall_clock structure
140e27c4929SArnd Bergmann * like ARM has.
141e27c4929SArnd Bergmann */
1427af192c9SGerd Hoffmann now.tv_sec = wall_clock->sec;
1437af192c9SGerd Hoffmann now.tv_nsec = wall_clock->nsec;
1447af192c9SGerd Hoffmann rmb(); /* fetch time before checking version */
1457af192c9SGerd Hoffmann } while ((wall_clock->version & 1) || (version != wall_clock->version));
1467af192c9SGerd Hoffmann
1477af192c9SGerd Hoffmann delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
148e27c4929SArnd Bergmann delta += now.tv_sec * NSEC_PER_SEC + now.tv_nsec;
1497af192c9SGerd Hoffmann
1507af192c9SGerd Hoffmann now.tv_nsec = do_div(delta, NSEC_PER_SEC);
1517af192c9SGerd Hoffmann now.tv_sec = delta;
1527af192c9SGerd Hoffmann
153e27c4929SArnd Bergmann set_normalized_timespec64(ts, now.tv_sec, now.tv_nsec);
1547af192c9SGerd Hoffmann }
1559f08890aSJoao Martins
pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info * pvti)1569f08890aSJoao Martins void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti)
1579f08890aSJoao Martins {
158b95a8a27SThomas Gleixner WARN_ON(vclock_was_used(VDSO_CLOCKMODE_PVCLOCK));
1599f08890aSJoao Martins pvti_cpu0_va = pvti;
1609f08890aSJoao Martins }
1619f08890aSJoao Martins
pvclock_get_pvti_cpu0_va(void)1629f08890aSJoao Martins struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
1639f08890aSJoao Martins {
1649f08890aSJoao Martins return pvti_cpu0_va;
1659f08890aSJoao Martins }
1669f08890aSJoao Martins EXPORT_SYMBOL_GPL(pvclock_get_pvti_cpu0_va);
167