11965aae3SH. Peter Anvin #ifndef _ASM_X86_PVCLOCK_H 21965aae3SH. Peter Anvin #define _ASM_X86_PVCLOCK_H 3bb898558SAl Viro 4bb898558SAl Viro #include <linux/clocksource.h> 5bb898558SAl Viro #include <asm/pvclock-abi.h> 6bb898558SAl Viro 7bb898558SAl Viro /* some helper functions for xen and kvm pv clock sources */ 8a5a1d1c2SThomas Gleixner u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src); 92697902bSMarcelo Tosatti u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src); 10424c32f1SGlauber Costa void pvclock_set_flags(u8 flags); 11bb898558SAl Viro unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src); 12bb898558SAl Viro void pvclock_read_wallclock(struct pvclock_wall_clock *wall, 13bb898558SAl Viro struct pvclock_vcpu_time_info *vcpu, 14bb898558SAl Viro struct timespec *ts); 15e7a3481cSJeremy Fitzhardinge void pvclock_resume(void); 16bb898558SAl Viro 17d63285e9SMarcelo Tosatti void pvclock_touch_watchdogs(void); 18d63285e9SMarcelo Tosatti 193aed64f6SPaolo Bonzini static __always_inline 203aed64f6SPaolo Bonzini unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src) 213aed64f6SPaolo Bonzini { 223aed64f6SPaolo Bonzini unsigned version = src->version & ~1; 233aed64f6SPaolo Bonzini /* Make sure that the version is read before the data. */ 243aed64f6SPaolo Bonzini virt_rmb(); 253aed64f6SPaolo Bonzini return version; 263aed64f6SPaolo Bonzini } 273aed64f6SPaolo Bonzini 283aed64f6SPaolo Bonzini static __always_inline 293aed64f6SPaolo Bonzini bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src, 303aed64f6SPaolo Bonzini unsigned version) 313aed64f6SPaolo Bonzini { 323aed64f6SPaolo Bonzini /* Make sure that the version is re-read after the data. */ 333aed64f6SPaolo Bonzini virt_rmb(); 343aed64f6SPaolo Bonzini return unlikely(version != src->version); 353aed64f6SPaolo Bonzini } 363aed64f6SPaolo Bonzini 37347bb444SZachary Amsden /* 38347bb444SZachary Amsden * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction, 39347bb444SZachary Amsden * yielding a 64-bit result. 40347bb444SZachary Amsden */ 41347bb444SZachary Amsden static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift) 42347bb444SZachary Amsden { 43347bb444SZachary Amsden u64 product; 44347bb444SZachary Amsden #ifdef __i386__ 45347bb444SZachary Amsden u32 tmp1, tmp2; 46de2d1a52SZachary Amsden #else 47de2d1a52SZachary Amsden ulong tmp; 48347bb444SZachary Amsden #endif 49347bb444SZachary Amsden 50347bb444SZachary Amsden if (shift < 0) 51347bb444SZachary Amsden delta >>= -shift; 52347bb444SZachary Amsden else 53347bb444SZachary Amsden delta <<= shift; 54347bb444SZachary Amsden 55347bb444SZachary Amsden #ifdef __i386__ 56347bb444SZachary Amsden __asm__ ( 57347bb444SZachary Amsden "mul %5 ; " 58347bb444SZachary Amsden "mov %4,%%eax ; " 59347bb444SZachary Amsden "mov %%edx,%4 ; " 60347bb444SZachary Amsden "mul %5 ; " 61347bb444SZachary Amsden "xor %5,%5 ; " 62347bb444SZachary Amsden "add %4,%%eax ; " 63347bb444SZachary Amsden "adc %5,%%edx ; " 64347bb444SZachary Amsden : "=A" (product), "=r" (tmp1), "=r" (tmp2) 65347bb444SZachary Amsden : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) ); 66347bb444SZachary Amsden #elif defined(__x86_64__) 67347bb444SZachary Amsden __asm__ ( 683b217116SDuncan Sands "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]" 69de2d1a52SZachary Amsden : [lo]"=a"(product), 70de2d1a52SZachary Amsden [hi]"=d"(tmp) 71de2d1a52SZachary Amsden : "0"(delta), 72de2d1a52SZachary Amsden [mul_frac]"rm"((u64)mul_frac)); 73347bb444SZachary Amsden #else 74347bb444SZachary Amsden #error implement me! 75347bb444SZachary Amsden #endif 76347bb444SZachary Amsden 77347bb444SZachary Amsden return product; 78347bb444SZachary Amsden } 79347bb444SZachary Amsden 80dce2db0aSMarcelo Tosatti static __always_inline 81a5a1d1c2SThomas Gleixner u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc) 82dce2db0aSMarcelo Tosatti { 83108b249cSPaolo Bonzini u64 delta = tsc - src->tsc_timestamp; 84a5a1d1c2SThomas Gleixner u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul, 85f7550d07SMinfei Huang src->tsc_shift); 863aed64f6SPaolo Bonzini return src->system_time + offset; 87dce2db0aSMarcelo Tosatti } 88dce2db0aSMarcelo Tosatti 8971056ae2SMarcelo Tosatti struct pvclock_vsyscall_time_info { 9071056ae2SMarcelo Tosatti struct pvclock_vcpu_time_info pvti; 9171056ae2SMarcelo Tosatti } __attribute__((__aligned__(SMP_CACHE_BYTES))); 9271056ae2SMarcelo Tosatti 9371056ae2SMarcelo Tosatti #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info) 9471056ae2SMarcelo Tosatti 95*9f08890aSJoao Martins #ifdef CONFIG_PARAVIRT_CLOCK 96*9f08890aSJoao Martins void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti); 97*9f08890aSJoao Martins struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void); 98*9f08890aSJoao Martins #else 99*9f08890aSJoao Martins static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void) 100*9f08890aSJoao Martins { 101*9f08890aSJoao Martins return NULL; 102*9f08890aSJoao Martins } 103*9f08890aSJoao Martins #endif 104*9f08890aSJoao Martins 1051965aae3SH. Peter Anvin #endif /* _ASM_X86_PVCLOCK_H */ 106