1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PVCLOCK_H
3 #define _ASM_X86_PVCLOCK_H
5 #include <asm/clocksource.h>
6 #include <asm/pvclock-abi.h>
8 /* some helper functions for xen and kvm pv clock sources */
9 u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
10 u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);
11 u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
12 void pvclock_set_flags(u8 flags);
13 unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
14 void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
15 struct pvclock_vcpu_time_info *vcpu,
16 struct timespec64 *ts);
17 void pvclock_resume(void);
19 void pvclock_touch_watchdogs(void);
21 static __always_inline
22 unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
24 unsigned version = src->version & ~1;
25 /* Make sure that the version is read before the data. */
30 static __always_inline
31 bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
34 /* Make sure that the version is re-read after the data. */
36 return unlikely(version != src->version);
40 * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
41 * yielding a 64-bit result.
43 static __always_inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
66 : "=A" (product), "=r" (tmp1), "=r" (tmp2)
67 : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
68 #elif defined(__x86_64__)
70 "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
74 [mul_frac]"rm"((u64)mul_frac));
82 static __always_inline
83 u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc)
85 u64 delta = tsc - src->tsc_timestamp;
86 u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
88 return src->system_time + offset;
91 struct pvclock_vsyscall_time_info {
92 struct pvclock_vcpu_time_info pvti;
93 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
95 #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
97 #ifdef CONFIG_PARAVIRT_CLOCK
98 void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
99 struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
101 static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
107 #endif /* _ASM_X86_PVCLOCK_H */