H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_PVCLOCK_H |
| 2 | #define _ASM_X86_PVCLOCK_H |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 3 | |
| 4 | #include <linux/clocksource.h> |
| 5 | #include <asm/pvclock-abi.h> |
| 6 | |
Andy Lutomirski | 8705d60 | 2015-12-29 20:12:18 -0800 | [diff] [blame] | 7 | #ifdef CONFIG_KVM_GUEST |
Andy Lutomirski | dac16fb | 2015-12-10 19:20:20 -0800 | [diff] [blame] | 8 | extern struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void); |
| 9 | #else |
| 10 | static inline struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void) |
| 11 | { |
| 12 | return NULL; |
| 13 | } |
| 14 | #endif |
| 15 | |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 16 | /* some helper functions for xen and kvm pv clock sources */ |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 17 | u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src); |
Marcelo Tosatti | 2697902 | 2012-11-27 23:28:52 -0200 | [diff] [blame] | 18 | u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src); |
Glauber Costa | 424c32f | 2010-05-11 12:17:39 -0400 | [diff] [blame] | 19 | void pvclock_set_flags(u8 flags); |
Glauber Costa | 3807f34 | 2008-07-28 11:47:52 -0300 | [diff] [blame] | 20 | unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src); |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 21 | void pvclock_read_wallclock(struct pvclock_wall_clock *wall, |
| 22 | struct pvclock_vcpu_time_info *vcpu, |
| 23 | struct timespec *ts); |
Jeremy Fitzhardinge | e7a3481c | 2010-10-25 16:53:46 -0700 | [diff] [blame] | 24 | void pvclock_resume(void); |
Gerd Hoffmann | 7af192c | 2008-06-03 16:17:29 +0200 | [diff] [blame] | 25 | |
Marcelo Tosatti | d63285e | 2013-10-11 21:39:25 -0300 | [diff] [blame] | 26 | void pvclock_touch_watchdogs(void); |
| 27 | |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 28 | static __always_inline |
| 29 | unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src) |
| 30 | { |
| 31 | unsigned version = src->version & ~1; |
| 32 | /* Make sure that the version is read before the data. */ |
| 33 | virt_rmb(); |
| 34 | return version; |
| 35 | } |
| 36 | |
| 37 | static __always_inline |
| 38 | bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src, |
| 39 | unsigned version) |
| 40 | { |
| 41 | /* Make sure that the version is re-read after the data. */ |
| 42 | virt_rmb(); |
| 43 | return unlikely(version != src->version); |
| 44 | } |
| 45 | |
Zachary Amsden | 347bb44 | 2010-08-19 22:07:29 -1000 | [diff] [blame] | 46 | /* |
| 47 | * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction, |
| 48 | * yielding a 64-bit result. |
| 49 | */ |
| 50 | static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift) |
| 51 | { |
| 52 | u64 product; |
| 53 | #ifdef __i386__ |
| 54 | u32 tmp1, tmp2; |
Zachary Amsden | de2d1a5 | 2011-06-15 20:50:04 -0700 | [diff] [blame] | 55 | #else |
| 56 | ulong tmp; |
Zachary Amsden | 347bb44 | 2010-08-19 22:07:29 -1000 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | if (shift < 0) |
| 60 | delta >>= -shift; |
| 61 | else |
| 62 | delta <<= shift; |
| 63 | |
| 64 | #ifdef __i386__ |
| 65 | __asm__ ( |
| 66 | "mul %5 ; " |
| 67 | "mov %4,%%eax ; " |
| 68 | "mov %%edx,%4 ; " |
| 69 | "mul %5 ; " |
| 70 | "xor %5,%5 ; " |
| 71 | "add %4,%%eax ; " |
| 72 | "adc %5,%%edx ; " |
| 73 | : "=A" (product), "=r" (tmp1), "=r" (tmp2) |
| 74 | : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) ); |
| 75 | #elif defined(__x86_64__) |
| 76 | __asm__ ( |
Duncan Sands | 3b21711 | 2011-08-30 10:58:22 +0200 | [diff] [blame] | 77 | "mulq %[mul_frac] ; shrd $32, %[hi], %[lo]" |
Zachary Amsden | de2d1a5 | 2011-06-15 20:50:04 -0700 | [diff] [blame] | 78 | : [lo]"=a"(product), |
| 79 | [hi]"=d"(tmp) |
| 80 | : "0"(delta), |
| 81 | [mul_frac]"rm"((u64)mul_frac)); |
Zachary Amsden | 347bb44 | 2010-08-19 22:07:29 -1000 | [diff] [blame] | 82 | #else |
| 83 | #error implement me! |
| 84 | #endif |
| 85 | |
| 86 | return product; |
| 87 | } |
| 88 | |
Marcelo Tosatti | dce2db0 | 2012-11-27 23:28:51 -0200 | [diff] [blame] | 89 | static __always_inline |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 90 | u64 __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src, u64 tsc) |
Marcelo Tosatti | dce2db0 | 2012-11-27 23:28:51 -0200 | [diff] [blame] | 91 | { |
Paolo Bonzini | 108b249 | 2016-09-01 14:21:03 +0200 | [diff] [blame] | 92 | u64 delta = tsc - src->tsc_timestamp; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 93 | u64 offset = pvclock_scale_delta(delta, src->tsc_to_system_mul, |
Paolo Bonzini | 3aed64f | 2016-06-09 13:06:08 +0200 | [diff] [blame] | 94 | src->tsc_shift); |
| 95 | return src->system_time + offset; |
Marcelo Tosatti | dce2db0 | 2012-11-27 23:28:51 -0200 | [diff] [blame] | 96 | } |
| 97 | |
Marcelo Tosatti | 71056ae | 2012-11-27 23:28:55 -0200 | [diff] [blame] | 98 | struct pvclock_vsyscall_time_info { |
| 99 | struct pvclock_vcpu_time_info pvti; |
Marcelo Tosatti | 71056ae | 2012-11-27 23:28:55 -0200 | [diff] [blame] | 100 | } __attribute__((__aligned__(SMP_CACHE_BYTES))); |
| 101 | |
| 102 | #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info) |
Marcelo Tosatti | 71056ae | 2012-11-27 23:28:55 -0200 | [diff] [blame] | 103 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 104 | #endif /* _ASM_X86_PVCLOCK_H */ |