1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_PVCLOCK_H
31965aae3SH. Peter Anvin #define _ASM_X86_PVCLOCK_H
4bb898558SAl Viro
57ac87074SVincenzo Frascino #include <asm/clocksource.h>
6bb898558SAl Viro #include <asm/pvclock-abi.h>
7bb898558SAl Viro
8*7f053812SChristophe Leroy struct timespec64;
9bb898558SAl Viro /* some helper functions for xen and kvm pv clock sources */
10a5a1d1c2SThomas Gleixner u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
118739c681SPeter Zijlstra u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);
122697902bSMarcelo Tosatti u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
13424c32f1SGlauber Costa void pvclock_set_flags(u8 flags);
14bb898558SAl Viro unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src);
15bb898558SAl Viro void pvclock_read_wallclock(struct pvclock_wall_clock *wall,
16bb898558SAl Viro struct pvclock_vcpu_time_info *vcpu,
17e27c4929SArnd Bergmann struct timespec64 *ts);
18e7a3481cSJeremy Fitzhardinge void pvclock_resume(void);
19bb898558SAl Viro
20d63285e9SMarcelo Tosatti void pvclock_touch_watchdogs(void);
21d63285e9SMarcelo Tosatti
223aed64f6SPaolo Bonzini static __always_inline
pvclock_read_begin(const struct pvclock_vcpu_time_info * src)233aed64f6SPaolo Bonzini unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
243aed64f6SPaolo Bonzini {
253aed64f6SPaolo Bonzini unsigned version = src->version & ~1;
263aed64f6SPaolo Bonzini /* Make sure that the version is read before the data. */
273aed64f6SPaolo Bonzini virt_rmb();
283aed64f6SPaolo Bonzini return version;
293aed64f6SPaolo Bonzini }
303aed64f6SPaolo Bonzini
313aed64f6SPaolo Bonzini static __always_inline
pvclock_read_retry(const struct pvclock_vcpu_time_info * src,unsigned version)323aed64f6SPaolo Bonzini bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
333aed64f6SPaolo Bonzini unsigned version)
343aed64f6SPaolo Bonzini {
353aed64f6SPaolo Bonzini /* Make sure that the version is re-read after the data. */
363aed64f6SPaolo Bonzini virt_rmb();
373aed64f6SPaolo Bonzini return unlikely(version != src->version);
383aed64f6SPaolo Bonzini }
393aed64f6SPaolo Bonzini
40347bb444SZachary Amsden /*
41347bb444SZachary Amsden * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
42347bb444SZachary Amsden * yielding a 64-bit result.
43347bb444SZachary Amsden */
pvclock_scale_delta(u64 delta,u32 mul_frac,int shift)448739c681SPeter Zijlstra static __always_inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
45347bb444SZachary Amsden {
46347bb444SZachary Amsden u64 product;
47347bb444SZachary Amsden #ifdef __i386__
48347bb444SZachary Amsden u32 tmp1, tmp2;
49de2d1a52SZachary Amsden #else
50de2d1a52SZachary Amsden ulong tmp;
51347bb444SZachary Amsden #endif
52347bb444SZachary Amsden
53347bb444SZachary Amsden if (shift < 0)
54347bb444SZachary Amsden delta >>= -shift;
55347bb444SZachary Amsden else
56347bb444SZachary Amsden delta <<= shift;
57347bb444SZachary Amsden
58347bb444SZachary Amsden #ifdef __i386__
59347bb444SZachary Amsden __asm__ (
60347bb444SZachary Amsden "mul %5 ; "
61347bb444SZachary Amsden "mov %4,%%eax ; "
62347bb444SZachary Amsden "mov %%edx,%4 ; "
63347bb444SZachary Amsden "mul %5 ; "
64347bb444SZachary Amsden "xor %5,%5 ; "
65347bb444SZachary Amsden "add %4,%%eax ; "
66347bb444SZachary Amsden "adc %5,%%edx ; "
67347bb444SZachary Amsden : "=A" (product), "=r" (tmp1), "=r" (tmp2)
68347bb444SZachary Amsden : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
69347bb444SZachary Amsden #elif defined(__x86_64__)
70347bb444SZachary Amsden __asm__ (
713b217116SDuncan Sands "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
72de2d1a52SZachary Amsden : [lo]"=a"(product),
73de2d1a52SZachary Amsden [hi]"=d"(tmp)
74de2d1a52SZachary Amsden : "0"(delta),
75de2d1a52SZachary Amsden [mul_frac]"rm"((u64)mul_frac));
76347bb444SZachary Amsden #else
77347bb444SZachary Amsden #error implement me!
78347bb444SZachary Amsden #endif
79347bb444SZachary Amsden
80347bb444SZachary Amsden return product;
81347bb444SZachary Amsden }
82347bb444SZachary Amsden
83dce2db0aSMarcelo Tosatti static __always_inline
__pvclock_read_cycles(const struct pvclock_vcpu_time_info * src,u64 tsc)84a5a1d1c2SThomas Gleixner u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc)
85dce2db0aSMarcelo Tosatti {
86108b249cSPaolo Bonzini u64 delta = tsc - src->tsc_timestamp;
87a5a1d1c2SThomas Gleixner u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
88f7550d07SMinfei Huang src->tsc_shift);
893aed64f6SPaolo Bonzini return src->system_time + offset;
90dce2db0aSMarcelo Tosatti }
91dce2db0aSMarcelo Tosatti
9271056ae2SMarcelo Tosatti struct pvclock_vsyscall_time_info {
9371056ae2SMarcelo Tosatti struct pvclock_vcpu_time_info pvti;
9471056ae2SMarcelo Tosatti } __attribute__((__aligned__(SMP_CACHE_BYTES)));
9571056ae2SMarcelo Tosatti
9671056ae2SMarcelo Tosatti #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
9771056ae2SMarcelo Tosatti
989f08890aSJoao Martins #ifdef CONFIG_PARAVIRT_CLOCK
999f08890aSJoao Martins void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
1009f08890aSJoao Martins struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
1019f08890aSJoao Martins #else
pvclock_get_pvti_cpu0_va(void)1029f08890aSJoao Martins static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
1039f08890aSJoao Martins {
1049f08890aSJoao Martins return NULL;
1059f08890aSJoao Martins }
1069f08890aSJoao Martins #endif
1079f08890aSJoao Martins
1081965aae3SH. Peter Anvin #endif /* _ASM_X86_PVCLOCK_H */
109